@bodhiapp/bodhi-browser-types 0.0.1
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/bodhiext.ts +255 -0
- package/common.ts +99 -0
- package/index.ts +88 -0
- package/package.json +26 -0
- package/protocol.ts +315 -0
package/bodhiext.ts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API types for window.bodhiext interface
|
|
3
|
+
*
|
|
4
|
+
* This file defines the contract web pages use to communicate with the Bodhi browser extension.
|
|
5
|
+
* Used primarily by inject.ts to create the window.bodhiext API surface.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { OpenAiApiError, PingResponse, CreateChatCompletionRequest, CreateChatCompletionResponse, CreateChatCompletionStreamResponse } from '@bodhiapp/ts-client';
|
|
9
|
+
import { isOperationErrorStructure } from './protocol';
|
|
10
|
+
|
|
11
|
+
//-----------------------------------------------------------------------------------
|
|
12
|
+
// HTTP RESPONSE TYPES
|
|
13
|
+
//-----------------------------------------------------------------------------------
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* HTTP response wrapper - body can be success type OR error type
|
|
17
|
+
* Use isApiErrorResponse() to narrow the type based on status
|
|
18
|
+
*/
|
|
19
|
+
export interface ApiResponse<T = unknown> {
|
|
20
|
+
body: T | OpenAiApiError;
|
|
21
|
+
status: number;
|
|
22
|
+
headers: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Stream chunk returned by sendStreamRequest
|
|
27
|
+
*
|
|
28
|
+
* This is the chunk structure yielded by the ReadableStream from sendStreamRequest.
|
|
29
|
+
* Different from StreamChunkMessage which is the internal message wrapper.
|
|
30
|
+
*/
|
|
31
|
+
export interface StreamChunk {
|
|
32
|
+
body: unknown;
|
|
33
|
+
headers?: Record<string, string>;
|
|
34
|
+
status?: number;
|
|
35
|
+
done?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Server state information returned by /bodhi/v1/info endpoint
|
|
40
|
+
*/
|
|
41
|
+
export interface ServerStateInfo {
|
|
42
|
+
/** Current application status */
|
|
43
|
+
status: 'setup' | 'ready' | 'resource-admin' | 'error' | 'unreachable';
|
|
44
|
+
/** Application version */
|
|
45
|
+
version?: string;
|
|
46
|
+
/** Server URL (added by extension) */
|
|
47
|
+
url?: string;
|
|
48
|
+
/** Error details if status is 'error' or 'unreachable' */
|
|
49
|
+
error?: {
|
|
50
|
+
message: string;
|
|
51
|
+
type?: string;
|
|
52
|
+
code?: string;
|
|
53
|
+
param?: string;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//-----------------------------------------------------------------------------------
|
|
58
|
+
// ERROR TYPES (Thrown by extension)
|
|
59
|
+
//-----------------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* API error thrown when server returns HTTP 4xx/5xx
|
|
63
|
+
* Used for streaming responses (non-streaming returns ApiResponse)
|
|
64
|
+
*/
|
|
65
|
+
export interface ApiError extends Error {
|
|
66
|
+
response: {
|
|
67
|
+
status: number;
|
|
68
|
+
body: OpenAiApiError;
|
|
69
|
+
headers?: Record<string, string>;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Operation error thrown when HTTP request couldn't complete
|
|
75
|
+
* (network unreachable, timeout, extension error)
|
|
76
|
+
*/
|
|
77
|
+
export interface OperationError extends Error {
|
|
78
|
+
error: {
|
|
79
|
+
message: string;
|
|
80
|
+
type: string; // Relaxed: any string allowed for custom error types
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Union of all extension-thrown errors
|
|
86
|
+
*/
|
|
87
|
+
export type ExtensionError = ApiError | OperationError;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Type guard: API error (has response field)
|
|
91
|
+
*/
|
|
92
|
+
export function isApiError(err: unknown): err is ApiError {
|
|
93
|
+
return (
|
|
94
|
+
err instanceof Error &&
|
|
95
|
+
'response' in err &&
|
|
96
|
+
typeof (err as ApiError).response === 'object' &&
|
|
97
|
+
(err as ApiError).response !== null &&
|
|
98
|
+
typeof (err as ApiError).response.status === 'number' &&
|
|
99
|
+
'body' in (err as ApiError).response
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Type guard: Operation error (has error field, no response)
|
|
105
|
+
*/
|
|
106
|
+
export function isOperationError(err: unknown): err is OperationError {
|
|
107
|
+
return err instanceof Error && 'error' in err && !('response' in err) && isOperationErrorStructure((err as OperationError).error);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
//-----------------------------------------------------------------------------------
|
|
111
|
+
// CHAT API TYPES (OpenAI-compatible from @bodhiapp/ts-client)
|
|
112
|
+
//-----------------------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Chat API uses types from @bodhiapp/ts-client.
|
|
116
|
+
* Consumers should import these types directly from @bodhiapp/ts-client:
|
|
117
|
+
* - ChatCompletionRequestMessage
|
|
118
|
+
* - ChatCompletionResponseMessage
|
|
119
|
+
* - CreateChatCompletionRequest
|
|
120
|
+
* - CreateChatCompletionResponse
|
|
121
|
+
* - CreateChatCompletionStreamResponse
|
|
122
|
+
* - ChatChoice
|
|
123
|
+
* - ChatChoiceStream
|
|
124
|
+
* - ChatCompletionStreamResponseDelta
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Chat completions API interface
|
|
129
|
+
*/
|
|
130
|
+
export interface ChatCompletionsApi {
|
|
131
|
+
/**
|
|
132
|
+
* Create a chat completion
|
|
133
|
+
*
|
|
134
|
+
* Non-streaming: Returns ApiResponse - caller checks status for success/error
|
|
135
|
+
* Streaming: Yields chunks via AsyncIterable - throws ApiError or OperationError on error
|
|
136
|
+
*
|
|
137
|
+
* @param params - Chat completion parameters
|
|
138
|
+
* @returns Non-streaming: Promise<ApiResponse<CreateChatCompletionResponse>>, Streaming: AsyncIterable<CreateChatCompletionStreamResponse>
|
|
139
|
+
*/
|
|
140
|
+
create(params: CreateChatCompletionRequest & { stream?: false }): Promise<ApiResponse<CreateChatCompletionResponse>>;
|
|
141
|
+
create(params: CreateChatCompletionRequest & { stream: true }): AsyncIterable<CreateChatCompletionStreamResponse>;
|
|
142
|
+
create(params: CreateChatCompletionRequest): Promise<ApiResponse<CreateChatCompletionResponse>> | AsyncIterable<CreateChatCompletionStreamResponse>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Chat API namespace
|
|
147
|
+
*/
|
|
148
|
+
export interface ChatApi {
|
|
149
|
+
completions: ChatCompletionsApi;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
//-----------------------------------------------------------------------------------
|
|
153
|
+
// PUBLIC API INTERFACE
|
|
154
|
+
//-----------------------------------------------------------------------------------
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Public window.bodhiext interface
|
|
158
|
+
*
|
|
159
|
+
* This interface defines all methods available to web pages through window.bodhiext.
|
|
160
|
+
* The extension creates this interface in inject.ts and attaches it to the window object.
|
|
161
|
+
*/
|
|
162
|
+
export interface BodhiExtPublicApi {
|
|
163
|
+
/**
|
|
164
|
+
* Send a generic API request through the extension to the backend server.
|
|
165
|
+
*
|
|
166
|
+
* @template TReq - Request body type (inferred from body parameter)
|
|
167
|
+
* @template TRes - Response body type (must be specified explicitly)
|
|
168
|
+
* @param method - HTTP method (GET, POST, PUT, DELETE, etc.)
|
|
169
|
+
* @param endpoint - API endpoint path (e.g., '/v1/chat/completions')
|
|
170
|
+
* @param body - Optional request body (will be JSON stringified)
|
|
171
|
+
* @param headers - Optional additional headers
|
|
172
|
+
* @returns Promise resolving to ApiResponse with body, headers, and status
|
|
173
|
+
*/
|
|
174
|
+
sendApiRequest<TReq = unknown, TRes = unknown>(method: string, endpoint: string, body?: TReq, headers?: Record<string, string>): Promise<ApiResponse<TRes>>;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Send a streaming API request through the extension.
|
|
178
|
+
*
|
|
179
|
+
* Used for SSE (Server-Sent Events) endpoints like streaming chat completions.
|
|
180
|
+
* Returns a ReadableStream that yields StreamChunk objects.
|
|
181
|
+
*
|
|
182
|
+
* @template TReq - Request body type (inferred from body parameter)
|
|
183
|
+
* @param method - HTTP method (typically POST for streaming)
|
|
184
|
+
* @param endpoint - API endpoint path (e.g., '/v1/chat/completions')
|
|
185
|
+
* @param body - Optional request body
|
|
186
|
+
* @param headers - Optional additional headers
|
|
187
|
+
* @returns ReadableStream yielding StreamChunk objects
|
|
188
|
+
*/
|
|
189
|
+
sendStreamRequest<TReq = unknown>(method: string, endpoint: string, body?: TReq, headers?: Record<string, string>): ReadableStream<StreamChunk>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Send a generic extension request.
|
|
193
|
+
*
|
|
194
|
+
* Used for extension-specific operations like test_connection, get_extension_id, etc.
|
|
195
|
+
* This is a low-level API for extension capabilities.
|
|
196
|
+
*
|
|
197
|
+
* @param action - Extension action name (e.g., 'test_connection')
|
|
198
|
+
* @param params - Optional action parameters
|
|
199
|
+
* @returns Promise resolving to action-specific response
|
|
200
|
+
*/
|
|
201
|
+
sendExtRequest(action: string, params?: any): Promise<any>;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Simple health check to verify extension connectivity.
|
|
205
|
+
*
|
|
206
|
+
* Returns ApiResponse - caller should check status to determine success/error.
|
|
207
|
+
* On success (2xx), body is PingResponse. On error (4xx/5xx), body is OpenAiApiError.
|
|
208
|
+
*
|
|
209
|
+
* @returns Promise resolving to ApiResponse<PingResponse>
|
|
210
|
+
*/
|
|
211
|
+
ping(): Promise<ApiResponse<PingResponse>>;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Get server state information from /bodhi/v1/info endpoint.
|
|
215
|
+
*
|
|
216
|
+
* Returns the current status of the backend server including
|
|
217
|
+
* whether it's in setup mode, ready, or has errors.
|
|
218
|
+
*
|
|
219
|
+
* @returns Promise resolving to ServerStateInfo
|
|
220
|
+
*/
|
|
221
|
+
serverState(): Promise<ServerStateInfo>;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* OpenAI-compatible chat API.
|
|
225
|
+
*
|
|
226
|
+
* Provides chat completion functionality compatible with OpenAI's API structure.
|
|
227
|
+
*/
|
|
228
|
+
chat: ChatApi;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Get the extension ID.
|
|
232
|
+
*
|
|
233
|
+
* The extension ID is fetched asynchronously during initialization.
|
|
234
|
+
* This method returns a promise that resolves to the extension ID once available.
|
|
235
|
+
*
|
|
236
|
+
* @returns Promise resolving to the extension ID string
|
|
237
|
+
*/
|
|
238
|
+
getExtensionId(): Promise<string>;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
//-----------------------------------------------------------------------------------
|
|
242
|
+
// WINDOW AUGMENTATION
|
|
243
|
+
//-----------------------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Window augmentation for TypeScript
|
|
247
|
+
*
|
|
248
|
+
* Declares the optional bodhiext property on the Window interface.
|
|
249
|
+
* This allows TypeScript code to access window.bodhiext with proper typing.
|
|
250
|
+
*/
|
|
251
|
+
declare global {
|
|
252
|
+
interface Window {
|
|
253
|
+
bodhiext?: BodhiExtPublicApi;
|
|
254
|
+
}
|
|
255
|
+
}
|
package/common.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared constants for Bodhi Browser Extension
|
|
3
|
+
*
|
|
4
|
+
* Constants used by both inject.ts and background/content.ts scripts,
|
|
5
|
+
* or common to the entire extension.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
//-----------------------------------------------------------------------------------
|
|
9
|
+
// HTTP CONSTANTS
|
|
10
|
+
//-----------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
export const CONTENT_TYPE_JSON = 'application/json';
|
|
13
|
+
export const CONTENT_TYPE_EVENT_STREAM = 'text/event-stream';
|
|
14
|
+
export const CONTENT_TYPE_HEADER = 'Content-Type';
|
|
15
|
+
export const HTTP_METHOD_GET = 'GET';
|
|
16
|
+
export const HTTP_METHOD_POST = 'POST';
|
|
17
|
+
|
|
18
|
+
//-----------------------------------------------------------------------------------
|
|
19
|
+
// API ENDPOINTS
|
|
20
|
+
//-----------------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
export const ENDPOINT_PING = '/ping';
|
|
23
|
+
export const ENDPOINT_CHAT_COMPLETIONS = '/v1/chat/completions';
|
|
24
|
+
|
|
25
|
+
//-----------------------------------------------------------------------------------
|
|
26
|
+
// DEFAULT VALUES
|
|
27
|
+
//-----------------------------------------------------------------------------------
|
|
28
|
+
|
|
29
|
+
export const DEFAULT_API_BASE_URL = 'http://localhost:1135';
|
|
30
|
+
export const DEFAULT_API_TIMEOUT = 10000; // 10 seconds for API requests
|
|
31
|
+
export const DEFAULT_STREAM_TIMEOUT = 60000; // 60 seconds for streaming
|
|
32
|
+
|
|
33
|
+
//-----------------------------------------------------------------------------------
|
|
34
|
+
// STORAGE
|
|
35
|
+
//-----------------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
export const STORAGE_KEY_BACKEND_URL = 'backendUrl';
|
|
38
|
+
|
|
39
|
+
//-----------------------------------------------------------------------------------
|
|
40
|
+
// SSE (Server-Sent Events)
|
|
41
|
+
//-----------------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
export const SSE_DONE_MARKER = '[DONE]';
|
|
44
|
+
export const SSE_DATA_PREFIX = 'data: ';
|
|
45
|
+
export const SSE_CHUNK_DELIMITER = '\n\n';
|
|
46
|
+
|
|
47
|
+
//-----------------------------------------------------------------------------------
|
|
48
|
+
// EXTENSION ACTIONS
|
|
49
|
+
//-----------------------------------------------------------------------------------
|
|
50
|
+
|
|
51
|
+
export const EXT_ACTIONS = {
|
|
52
|
+
GET_EXTENSION_ID: 'get_extension_id',
|
|
53
|
+
TEST_CONNECTION: 'test_connection',
|
|
54
|
+
} as const;
|
|
55
|
+
|
|
56
|
+
//-----------------------------------------------------------------------------------
|
|
57
|
+
// ERROR TYPES
|
|
58
|
+
//-----------------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
export const ERROR_TYPES = {
|
|
61
|
+
NETWORK_ERROR: 'network_error',
|
|
62
|
+
EXTENSION_ERROR: 'extension_error',
|
|
63
|
+
TIMEOUT_ERROR: 'timeout_error',
|
|
64
|
+
AUTH_ERROR: 'auth_error',
|
|
65
|
+
} as const;
|
|
66
|
+
|
|
67
|
+
// Type union for connection error types
|
|
68
|
+
export type ConnectionErrorType = (typeof ERROR_TYPES)[keyof typeof ERROR_TYPES];
|
|
69
|
+
|
|
70
|
+
//-----------------------------------------------------------------------------------
|
|
71
|
+
// DOCUMENT STATES
|
|
72
|
+
//-----------------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
export const DOCUMENT_STATE_COMPLETE = 'complete';
|
|
75
|
+
|
|
76
|
+
//-----------------------------------------------------------------------------------
|
|
77
|
+
// EVENT NAMES
|
|
78
|
+
//-----------------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
export const EVENT_INITIALIZED = 'bodhiext:initialized';
|
|
81
|
+
|
|
82
|
+
//-----------------------------------------------------------------------------------
|
|
83
|
+
// PORT NAMES
|
|
84
|
+
//-----------------------------------------------------------------------------------
|
|
85
|
+
|
|
86
|
+
export const BODHI_STREAM_PORT = 'BODHI_STREAM_PORT';
|
|
87
|
+
|
|
88
|
+
//-----------------------------------------------------------------------------------
|
|
89
|
+
// FALLBACKS
|
|
90
|
+
//-----------------------------------------------------------------------------------
|
|
91
|
+
|
|
92
|
+
export const ORIGIN_WILDCARD = '*';
|
|
93
|
+
|
|
94
|
+
//-----------------------------------------------------------------------------------
|
|
95
|
+
// ERROR MESSAGES
|
|
96
|
+
//-----------------------------------------------------------------------------------
|
|
97
|
+
|
|
98
|
+
export const ERROR_MISSING_REQUEST_ID = 'Invalid message format: missing requestId or request';
|
|
99
|
+
export const ERROR_CONNECTION_CLOSED = 'Connection closed unexpectedly';
|
package/index.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Bodhi Browser Extension - Shared Types and Constants
|
|
2
|
+
// This package provides the shared protocol definitions for communication
|
|
3
|
+
// between web pages, external extensions, and bodhi-browser-ext
|
|
4
|
+
//
|
|
5
|
+
// NOTE: OpenAI types (CreateChatCompletionRequest, PingResponse, etc.)
|
|
6
|
+
// are NOT re-exported. Import them directly from '@bodhiapp/ts-client' when needed.
|
|
7
|
+
|
|
8
|
+
//-----------------------------------------------------------------------------------
|
|
9
|
+
// PUBLIC API TYPES (window.bodhiext)
|
|
10
|
+
//-----------------------------------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
export type { ApiResponse, StreamChunk, ServerStateInfo, ApiError, OperationError, ExtensionError, ChatCompletionsApi, ChatApi, BodhiExtPublicApi } from './bodhiext';
|
|
13
|
+
|
|
14
|
+
export { isApiError, isOperationError } from './bodhiext';
|
|
15
|
+
|
|
16
|
+
//-----------------------------------------------------------------------------------
|
|
17
|
+
// INTERNAL PROTOCOL TYPES
|
|
18
|
+
//-----------------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
export type {
|
|
21
|
+
ApiRequest,
|
|
22
|
+
ApiRequestMessage,
|
|
23
|
+
OperationErrorResponse,
|
|
24
|
+
ApiResponseSuccessMessage,
|
|
25
|
+
OperationErrorResponseMessage,
|
|
26
|
+
ApiResponseMessage,
|
|
27
|
+
ErrorMessage,
|
|
28
|
+
StreamChunkMessage,
|
|
29
|
+
StreamApiErrorMessage,
|
|
30
|
+
StreamErrorMessage,
|
|
31
|
+
StreamMessage,
|
|
32
|
+
StreamController,
|
|
33
|
+
SSEChunk,
|
|
34
|
+
ExtRequest,
|
|
35
|
+
ExtRequestMessage,
|
|
36
|
+
ExtError,
|
|
37
|
+
ExtErrorResponse,
|
|
38
|
+
ExtResponse,
|
|
39
|
+
ExtResponseMessage,
|
|
40
|
+
GetExtensionIdRequest,
|
|
41
|
+
GetExtensionIdResponse,
|
|
42
|
+
TestConnectionRequest,
|
|
43
|
+
TestConnectionResponse,
|
|
44
|
+
} from './protocol';
|
|
45
|
+
|
|
46
|
+
export {
|
|
47
|
+
MESSAGE_TYPES,
|
|
48
|
+
isOperationErrorResponse,
|
|
49
|
+
isApiErrorResponse,
|
|
50
|
+
isApiSuccessResponse,
|
|
51
|
+
isStreamChunk,
|
|
52
|
+
isStreamApiError,
|
|
53
|
+
isStreamError,
|
|
54
|
+
isExtError,
|
|
55
|
+
isOpenAiApiErrorBody,
|
|
56
|
+
isOperationErrorStructure,
|
|
57
|
+
} from './protocol';
|
|
58
|
+
|
|
59
|
+
//-----------------------------------------------------------------------------------
|
|
60
|
+
// COMMON CONSTANTS
|
|
61
|
+
//-----------------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
export {
|
|
64
|
+
CONTENT_TYPE_JSON,
|
|
65
|
+
CONTENT_TYPE_EVENT_STREAM,
|
|
66
|
+
CONTENT_TYPE_HEADER,
|
|
67
|
+
HTTP_METHOD_GET,
|
|
68
|
+
HTTP_METHOD_POST,
|
|
69
|
+
ENDPOINT_PING,
|
|
70
|
+
ENDPOINT_CHAT_COMPLETIONS,
|
|
71
|
+
DEFAULT_API_BASE_URL,
|
|
72
|
+
DEFAULT_API_TIMEOUT,
|
|
73
|
+
DEFAULT_STREAM_TIMEOUT,
|
|
74
|
+
STORAGE_KEY_BACKEND_URL,
|
|
75
|
+
SSE_DONE_MARKER,
|
|
76
|
+
SSE_DATA_PREFIX,
|
|
77
|
+
SSE_CHUNK_DELIMITER,
|
|
78
|
+
EXT_ACTIONS,
|
|
79
|
+
ERROR_TYPES,
|
|
80
|
+
DOCUMENT_STATE_COMPLETE,
|
|
81
|
+
EVENT_INITIALIZED,
|
|
82
|
+
BODHI_STREAM_PORT,
|
|
83
|
+
ORIGIN_WILDCARD,
|
|
84
|
+
ERROR_MISSING_REQUEST_ID,
|
|
85
|
+
ERROR_CONNECTION_CLOSED,
|
|
86
|
+
} from './common';
|
|
87
|
+
|
|
88
|
+
export type { ConnectionErrorType } from './common';
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bodhiapp/bodhi-browser-types",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "TypeScript types for Bodhi Browser Extension",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "index.ts",
|
|
7
|
+
"types": "index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.ts",
|
|
11
|
+
"import": "./index.ts",
|
|
12
|
+
"require": "./index.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": ["*.ts"],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/BodhiSearch/bodhi-browser.git",
|
|
19
|
+
"directory": "bodhi-browser-ext/src/types"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "BodhiSearch",
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/protocol.ts
ADDED
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal extension protocol types
|
|
3
|
+
*
|
|
4
|
+
* Message passing types and constants for communication between
|
|
5
|
+
* inject.ts, content.ts, and background.ts (service worker).
|
|
6
|
+
* Used primarily by background/*, content.ts files.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { OpenAiApiError, ErrorBody } from '@bodhiapp/ts-client';
|
|
10
|
+
import type { ApiResponse, ServerStateInfo } from './bodhiext';
|
|
11
|
+
|
|
12
|
+
//-----------------------------------------------------------------------------------
|
|
13
|
+
// VALIDATION HELPERS
|
|
14
|
+
//-----------------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Private helper: Check if value is non-null object
|
|
18
|
+
* Not exported - implementation detail for other helpers
|
|
19
|
+
*/
|
|
20
|
+
function isNonNullObject(value: unknown): value is Record<string, unknown> {
|
|
21
|
+
return value !== null && typeof value === 'object';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Validate OpenAI API error body structure
|
|
26
|
+
* { error: { message: string, type: string } }
|
|
27
|
+
*/
|
|
28
|
+
export function isOpenAiApiErrorBody(body: unknown): body is OpenAiApiError {
|
|
29
|
+
return (
|
|
30
|
+
isNonNullObject(body) &&
|
|
31
|
+
'error' in body &&
|
|
32
|
+
isNonNullObject(body.error) &&
|
|
33
|
+
'message' in body.error &&
|
|
34
|
+
typeof body.error.message === 'string' &&
|
|
35
|
+
'type' in body.error &&
|
|
36
|
+
typeof body.error.type === 'string'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validate OperationErrorResponse structure
|
|
42
|
+
* { message: string, type: string }
|
|
43
|
+
*/
|
|
44
|
+
export function isOperationErrorStructure(obj: unknown): obj is OperationErrorResponse {
|
|
45
|
+
return isNonNullObject(obj) && 'message' in obj && typeof obj.message === 'string' && 'type' in obj && typeof obj.type === 'string';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
//-----------------------------------------------------------------------------------
|
|
49
|
+
// MESSAGE TYPE CONSTANTS
|
|
50
|
+
//-----------------------------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
export const MESSAGE_TYPES = {
|
|
53
|
+
API_REQUEST: 'BODHI_API_REQUEST',
|
|
54
|
+
API_RESPONSE: 'BODHI_API_RESPONSE',
|
|
55
|
+
STREAM_REQUEST: 'BODHI_STREAM_REQUEST',
|
|
56
|
+
STREAM_CHUNK: 'BODHI_STREAM_CHUNK',
|
|
57
|
+
STREAM_ERROR: 'BODHI_STREAM_ERROR',
|
|
58
|
+
STREAM_API_ERROR: 'BODHI_STREAM_API_ERROR',
|
|
59
|
+
ERROR: 'BODHI_ERROR',
|
|
60
|
+
EXT_REQUEST: 'BODHI_EXT_REQUEST',
|
|
61
|
+
EXT_RESPONSE: 'BODHI_EXT_RESPONSE',
|
|
62
|
+
} as const;
|
|
63
|
+
|
|
64
|
+
//-----------------------------------------------------------------------------------
|
|
65
|
+
// API REQUEST/RESPONSE TYPES
|
|
66
|
+
//-----------------------------------------------------------------------------------
|
|
67
|
+
|
|
68
|
+
export interface ApiRequest<T = unknown> {
|
|
69
|
+
method: string;
|
|
70
|
+
endpoint: string;
|
|
71
|
+
body?: T;
|
|
72
|
+
headers?: Record<string, string>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface ApiRequestMessage<TReq = unknown> {
|
|
76
|
+
type: string;
|
|
77
|
+
requestId: string;
|
|
78
|
+
request: ApiRequest<TReq>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Operation-level error response (network unreachable, timeout, extension error)
|
|
83
|
+
* NOT an API error (those come through ApiResponse with OpenAiApiError body)
|
|
84
|
+
* This is a response type, not a thrown error
|
|
85
|
+
*/
|
|
86
|
+
export interface OperationErrorResponse {
|
|
87
|
+
message: string;
|
|
88
|
+
type: string; // Relaxed: any string allowed for custom error types
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Success API response message (HTTP request completed, regardless of status code)
|
|
93
|
+
*/
|
|
94
|
+
export interface ApiResponseSuccessMessage<T = unknown> {
|
|
95
|
+
type: string;
|
|
96
|
+
requestId: string;
|
|
97
|
+
response: ApiResponse<T>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Operation error response message - HTTP request couldn't complete
|
|
102
|
+
*/
|
|
103
|
+
export interface OperationErrorResponseMessage {
|
|
104
|
+
type: string;
|
|
105
|
+
requestId: string;
|
|
106
|
+
error: OperationErrorResponse;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* API response message - discriminated union
|
|
111
|
+
*/
|
|
112
|
+
export type ApiResponseMessage<T = unknown> = ApiResponseSuccessMessage<T> | OperationErrorResponseMessage;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Type guard for operation error response
|
|
116
|
+
*/
|
|
117
|
+
export function isOperationErrorResponse(msg: ApiResponseMessage): msg is OperationErrorResponseMessage {
|
|
118
|
+
return isNonNullObject(msg) && 'error' in msg && isOperationErrorStructure(msg.error);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Type guard to check if response is an API error (4xx/5xx)
|
|
123
|
+
* Narrows body type to OpenAiApiError
|
|
124
|
+
*/
|
|
125
|
+
export function isApiErrorResponse<T>(response: ApiResponse<T>): response is ApiResponse<T> & { body: OpenAiApiError; status: number } {
|
|
126
|
+
return isNonNullObject(response) && typeof response.status === 'number' && response.status >= 400 && isOpenAiApiErrorBody(response.body);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Type guard to check if response is successful (2xx)
|
|
131
|
+
* Narrows body type to T
|
|
132
|
+
*/
|
|
133
|
+
export function isApiSuccessResponse<T>(response: ApiResponse<T>): response is ApiResponse<T> & { body: T; status: number } {
|
|
134
|
+
return response !== null && typeof response === 'object' && typeof response.status === 'number' && response.status >= 200 && response.status < 300 && 'body' in response;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface ErrorMessage {
|
|
138
|
+
type: string;
|
|
139
|
+
requestId: string;
|
|
140
|
+
response: {
|
|
141
|
+
body: ErrorBody;
|
|
142
|
+
status: number;
|
|
143
|
+
headers: Record<string, string>;
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//-----------------------------------------------------------------------------------
|
|
148
|
+
// STREAMING MESSAGE TYPES (Discriminated Union)
|
|
149
|
+
//-----------------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Stream chunk message - successful SSE chunk received
|
|
153
|
+
* Uses ApiResponse<T> wrapper for consistency with non-streaming pattern
|
|
154
|
+
*/
|
|
155
|
+
export interface StreamChunkMessage<T = unknown> {
|
|
156
|
+
type: typeof MESSAGE_TYPES.STREAM_CHUNK;
|
|
157
|
+
requestId: string;
|
|
158
|
+
response: ApiResponse<T>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Stream API error message - server returned error response (not SSE)
|
|
163
|
+
* E.g., 400/401/500 JSON error instead of SSE stream
|
|
164
|
+
*/
|
|
165
|
+
export interface StreamApiErrorMessage {
|
|
166
|
+
type: typeof MESSAGE_TYPES.STREAM_API_ERROR;
|
|
167
|
+
requestId: string;
|
|
168
|
+
response: ApiResponse<OpenAiApiError>;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Stream error message - network/extension level error
|
|
173
|
+
* E.g., connection refused, timeout, extension error
|
|
174
|
+
*/
|
|
175
|
+
export interface StreamErrorMessage {
|
|
176
|
+
type: typeof MESSAGE_TYPES.STREAM_ERROR;
|
|
177
|
+
requestId: string;
|
|
178
|
+
error: OperationErrorResponse;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Union type for all streaming messages
|
|
183
|
+
*/
|
|
184
|
+
export type StreamMessage<T = unknown> = StreamChunkMessage<T> | StreamApiErrorMessage | StreamErrorMessage;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Type guard for stream chunk message
|
|
188
|
+
*/
|
|
189
|
+
export function isStreamChunk<T>(msg: StreamMessage<T>): msg is StreamChunkMessage<T> {
|
|
190
|
+
return msg !== null && typeof msg === 'object' && msg.type === MESSAGE_TYPES.STREAM_CHUNK;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Type guard for stream API error
|
|
195
|
+
*/
|
|
196
|
+
export function isStreamApiError(msg: StreamMessage): msg is StreamApiErrorMessage {
|
|
197
|
+
return msg !== null && typeof msg === 'object' && msg.type === MESSAGE_TYPES.STREAM_API_ERROR;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Type guard for stream error
|
|
202
|
+
*/
|
|
203
|
+
export function isStreamError(msg: StreamMessage): msg is StreamErrorMessage {
|
|
204
|
+
return msg !== null && typeof msg === 'object' && msg.type === MESSAGE_TYPES.STREAM_ERROR;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Interface for stream controller to handle SSE responses
|
|
209
|
+
*/
|
|
210
|
+
export interface StreamController {
|
|
211
|
+
enqueue: (chunk: any) => void;
|
|
212
|
+
error: (err: Error) => void;
|
|
213
|
+
complete: () => void;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Interface for SSE data chunk
|
|
218
|
+
*/
|
|
219
|
+
export interface SSEChunk {
|
|
220
|
+
done?: boolean;
|
|
221
|
+
[key: string]: any;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
//-----------------------------------------------------------------------------------
|
|
225
|
+
// GENERIC EXTENSION REQUEST/RESPONSE TYPES
|
|
226
|
+
//-----------------------------------------------------------------------------------
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Generic extension request interface
|
|
230
|
+
*/
|
|
231
|
+
export interface ExtRequest {
|
|
232
|
+
action: string;
|
|
233
|
+
params?: any;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Generic extension request message
|
|
238
|
+
*/
|
|
239
|
+
export interface ExtRequestMessage {
|
|
240
|
+
type: 'BODHI_EXT_REQUEST';
|
|
241
|
+
requestId: string;
|
|
242
|
+
request: ExtRequest;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Extension-level error structure
|
|
247
|
+
*/
|
|
248
|
+
export interface ExtError {
|
|
249
|
+
message: string;
|
|
250
|
+
type?: string;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Error response for extension operations
|
|
255
|
+
*/
|
|
256
|
+
export interface ExtErrorResponse {
|
|
257
|
+
error: ExtError;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Union type for extension response (flattened - no wrapper for success)
|
|
262
|
+
* Success: T (the actual response data)
|
|
263
|
+
* Error: { error: ExtError }
|
|
264
|
+
*/
|
|
265
|
+
export type ExtResponse<T = unknown> = T | ExtErrorResponse;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Type guard to check if extension response is an error
|
|
269
|
+
*/
|
|
270
|
+
export function isExtError<T>(res: ExtResponse<T>): res is ExtErrorResponse {
|
|
271
|
+
return res !== null && typeof res === 'object' && 'error' in res;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Generic extension response message
|
|
276
|
+
*/
|
|
277
|
+
export interface ExtResponseMessage<T = unknown> {
|
|
278
|
+
type: 'BODHI_EXT_RESPONSE';
|
|
279
|
+
requestId: string;
|
|
280
|
+
response: ExtResponse<T>;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
//-----------------------------------------------------------------------------------
|
|
284
|
+
// ACTION-SPECIFIC TYPES
|
|
285
|
+
//-----------------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Get Extension ID request (no params needed)
|
|
289
|
+
*/
|
|
290
|
+
export interface GetExtensionIdRequest extends ExtRequest {
|
|
291
|
+
action: 'get_extension_id';
|
|
292
|
+
params?: undefined;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Get Extension ID response body
|
|
297
|
+
*/
|
|
298
|
+
export interface GetExtensionIdResponse {
|
|
299
|
+
extension_id: string;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Test Connection request
|
|
304
|
+
*/
|
|
305
|
+
export interface TestConnectionRequest extends ExtRequest {
|
|
306
|
+
action: 'test_connection';
|
|
307
|
+
params: {
|
|
308
|
+
url: string;
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Test Connection response body (reuses ServerStateInfo)
|
|
314
|
+
*/
|
|
315
|
+
export type TestConnectionResponse = ServerStateInfo;
|