- Added telemetry utility to capture application events and metrics. - Integrated PostHog for event tracking with distinct user identification. - Implemented telemetry initialization, event capturing, and shutdown procedures. feat: add UV environment setup for Python management - Created utilities to manage Python installation and configuration. - Implemented network optimization checks for Python installation mirrors. - Added functions to set up managed Python environments with error handling. feat: enhance host API communication with token management - Introduced host API token retrieval and management for secure requests. - Updated host API fetch functions to include token in headers. - Added support for creating event sources with authentication. test: add comprehensive tests for gateway protocol and startup helpers - Implemented unit tests for gateway protocol helpers, event dispatching, and state management. - Added tests for startup recovery strategies and process policies. - Ensured coverage for connection monitoring and restart governance logic.
137 lines
2.7 KiB
TypeScript
137 lines
2.7 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
|
|
export interface JsonRpcRequest {
|
|
jsonrpc: '2.0';
|
|
id: string | number;
|
|
method: string;
|
|
params?: unknown;
|
|
}
|
|
|
|
export interface JsonRpcResponse<T = unknown> {
|
|
jsonrpc: '2.0';
|
|
id: string | number;
|
|
result?: T;
|
|
error?: JsonRpcError;
|
|
}
|
|
|
|
export interface JsonRpcError {
|
|
code: number;
|
|
message: string;
|
|
data?: unknown;
|
|
}
|
|
|
|
export interface JsonRpcNotification {
|
|
jsonrpc: '2.0';
|
|
method: string;
|
|
params?: unknown;
|
|
}
|
|
|
|
export enum JsonRpcErrorCode {
|
|
PARSE_ERROR = -32700,
|
|
INVALID_REQUEST = -32600,
|
|
METHOD_NOT_FOUND = -32601,
|
|
INVALID_PARAMS = -32602,
|
|
INTERNAL_ERROR = -32603,
|
|
SERVER_ERROR = -32000,
|
|
}
|
|
|
|
export enum GatewayErrorCode {
|
|
NOT_CONNECTED = -32001,
|
|
AUTH_REQUIRED = -32002,
|
|
PERMISSION_DENIED = -32003,
|
|
NOT_FOUND = -32004,
|
|
TIMEOUT = -32005,
|
|
RATE_LIMITED = -32006,
|
|
}
|
|
|
|
export enum GatewayEventType {
|
|
STATUS_CHANGED = 'gateway.status_changed',
|
|
CHANNEL_STATUS_CHANGED = 'channel.status_changed',
|
|
MESSAGE_RECEIVED = 'chat.message_received',
|
|
MESSAGE_SENT = 'chat.message_sent',
|
|
TOOL_CALL_STARTED = 'tool.call_started',
|
|
TOOL_CALL_COMPLETED = 'tool.call_completed',
|
|
ERROR = 'error',
|
|
}
|
|
|
|
export interface GatewayProtocolEvent<T = unknown> {
|
|
type: GatewayEventType;
|
|
timestamp: string;
|
|
data: T;
|
|
}
|
|
|
|
export function createRequest(
|
|
method: string,
|
|
params?: unknown,
|
|
id?: string | number,
|
|
): JsonRpcRequest {
|
|
return {
|
|
jsonrpc: '2.0',
|
|
id: id ?? randomUUID(),
|
|
method,
|
|
params,
|
|
};
|
|
}
|
|
|
|
export function createSuccessResponse<T>(
|
|
id: string | number,
|
|
result: T,
|
|
): JsonRpcResponse<T> {
|
|
return {
|
|
jsonrpc: '2.0',
|
|
id,
|
|
result,
|
|
};
|
|
}
|
|
|
|
export function createErrorResponse(
|
|
id: string | number,
|
|
code: number,
|
|
message: string,
|
|
data?: unknown,
|
|
): JsonRpcResponse {
|
|
return {
|
|
jsonrpc: '2.0',
|
|
id,
|
|
error: {
|
|
code,
|
|
message,
|
|
data,
|
|
},
|
|
};
|
|
}
|
|
|
|
export function isRequest(message: unknown): message is JsonRpcRequest {
|
|
return (
|
|
typeof message === 'object' &&
|
|
message !== null &&
|
|
'jsonrpc' in message &&
|
|
message.jsonrpc === '2.0' &&
|
|
'method' in message &&
|
|
typeof message.method === 'string' &&
|
|
'id' in message
|
|
);
|
|
}
|
|
|
|
export function isResponse(message: unknown): message is JsonRpcResponse {
|
|
return (
|
|
typeof message === 'object' &&
|
|
message !== null &&
|
|
'jsonrpc' in message &&
|
|
message.jsonrpc === '2.0' &&
|
|
'id' in message &&
|
|
('result' in message || 'error' in message)
|
|
);
|
|
}
|
|
|
|
export function isNotification(message: unknown): message is JsonRpcNotification {
|
|
return (
|
|
typeof message === 'object' &&
|
|
message !== null &&
|
|
'jsonrpc' in message &&
|
|
message.jsonrpc === '2.0' &&
|
|
'method' in message &&
|
|
!('id' in message)
|
|
);
|
|
}
|