@redonvn/event-ws-cliproxyapi-sdk 0.1.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.
Files changed (62) hide show
  1. package/README.md +428 -0
  2. package/dist/claude/client.d.ts +8 -0
  3. package/dist/claude/client.js +16 -0
  4. package/dist/claude/index.d.ts +2 -0
  5. package/dist/claude/index.js +1 -0
  6. package/dist/client.d.ts +19 -0
  7. package/dist/client.js +96 -0
  8. package/dist/cliproxy/client.d.ts +41 -0
  9. package/dist/cliproxy/client.js +43 -0
  10. package/dist/cliproxy/index.d.ts +2 -0
  11. package/dist/cliproxy/index.js +1 -0
  12. package/dist/codec.d.ts +8 -0
  13. package/dist/codec.js +79 -0
  14. package/dist/errors/index.d.ts +13 -0
  15. package/dist/errors/index.js +26 -0
  16. package/dist/gemini/client.d.ts +9 -0
  17. package/dist/gemini/client.js +19 -0
  18. package/dist/gemini/index.d.ts +2 -0
  19. package/dist/gemini/index.js +1 -0
  20. package/dist/http/client.d.ts +260 -0
  21. package/dist/http/client.js +591 -0
  22. package/dist/http/index.d.ts +2 -0
  23. package/dist/http/index.js +2 -0
  24. package/dist/http/types.d.ts +783 -0
  25. package/dist/http/types.js +1 -0
  26. package/dist/http-client.d.ts +259 -0
  27. package/dist/http-client.js +557 -0
  28. package/dist/http-types.d.ts +677 -0
  29. package/dist/http-types.js +1 -0
  30. package/dist/index.d.ts +7 -0
  31. package/dist/index.js +7 -0
  32. package/dist/management/client.d.ts +187 -0
  33. package/dist/management/client.js +399 -0
  34. package/dist/management/index.d.ts +2 -0
  35. package/dist/management/index.js +1 -0
  36. package/dist/openai/client.d.ts +10 -0
  37. package/dist/openai/client.js +23 -0
  38. package/dist/openai/index.d.ts +2 -0
  39. package/dist/openai/index.js +1 -0
  40. package/dist/shared/errors.d.ts +13 -0
  41. package/dist/shared/errors.js +26 -0
  42. package/dist/shared/http.d.ts +29 -0
  43. package/dist/shared/http.js +125 -0
  44. package/dist/shared/index.d.ts +2 -0
  45. package/dist/shared/index.js +2 -0
  46. package/dist/shared/types.d.ts +789 -0
  47. package/dist/shared/types.js +1 -0
  48. package/dist/types/index.d.ts +2 -0
  49. package/dist/types/index.js +2 -0
  50. package/dist/types.d.ts +101 -0
  51. package/dist/types.js +1 -0
  52. package/dist/utils/index.d.ts +0 -0
  53. package/dist/utils/index.js +2 -0
  54. package/dist/ws/client.d.ts +20 -0
  55. package/dist/ws/client.js +114 -0
  56. package/dist/ws/codec.d.ts +8 -0
  57. package/dist/ws/codec.js +100 -0
  58. package/dist/ws/index.d.ts +3 -0
  59. package/dist/ws/index.js +3 -0
  60. package/dist/ws/types.d.ts +101 -0
  61. package/dist/ws/types.js +1 -0
  62. package/package.json +29 -0
@@ -0,0 +1,13 @@
1
+ import type { OpenAIErrorResponse, ErrorResponse, StatusResponse, JsonObject } from './types.js';
2
+ export type ManagementErrorResponse = ErrorResponse;
3
+ export type ApiErrorPayload = OpenAIErrorResponse | ManagementErrorResponse | StatusResponse | JsonObject | {
4
+ error: string;
5
+ };
6
+ export declare class APIError extends Error {
7
+ readonly status: number;
8
+ readonly payload?: ApiErrorPayload;
9
+ constructor(message: string, status: number, payload?: ApiErrorPayload);
10
+ }
11
+ export declare function isOpenAIError(payload: unknown): payload is OpenAIErrorResponse;
12
+ export declare function isStatusError(payload: unknown): payload is StatusResponse;
13
+ export declare function isManagementError(payload: unknown): payload is ManagementErrorResponse;
@@ -0,0 +1,26 @@
1
+ export class APIError extends Error {
2
+ constructor(message, status, payload) {
3
+ super(message);
4
+ this.name = 'APIError';
5
+ this.status = status;
6
+ this.payload = payload;
7
+ }
8
+ }
9
+ export function isOpenAIError(payload) {
10
+ if (!payload || typeof payload !== 'object')
11
+ return false;
12
+ const obj = payload;
13
+ return !!obj.error && typeof obj.error.message === 'string' && typeof obj.error.type === 'string';
14
+ }
15
+ export function isStatusError(payload) {
16
+ if (!payload || typeof payload !== 'object')
17
+ return false;
18
+ const obj = payload;
19
+ return typeof obj.status === 'string' && (obj.status === 'error' || obj.status === 'wait' || obj.status === 'ok');
20
+ }
21
+ export function isManagementError(payload) {
22
+ if (!payload || typeof payload !== 'object')
23
+ return false;
24
+ const obj = payload;
25
+ return typeof obj.error === 'string';
26
+ }
@@ -0,0 +1,29 @@
1
+ export interface HttpClientOptions {
2
+ baseUrl: string;
3
+ accessKey?: string;
4
+ managementKey?: string;
5
+ localPassword?: string;
6
+ fetch?: typeof fetch;
7
+ }
8
+ export interface RequestOptions {
9
+ query?: Record<string, string | number | boolean | undefined>;
10
+ headers?: Record<string, string>;
11
+ }
12
+ export declare class BaseHttpClient {
13
+ readonly baseUrl: string;
14
+ protected accessKey?: string;
15
+ protected managementKey?: string;
16
+ protected localPassword?: string;
17
+ protected fetchImpl: typeof fetch;
18
+ constructor(options: HttpClientOptions);
19
+ setAccessKey(key?: string): void;
20
+ setManagementKey(key?: string): void;
21
+ setLocalPassword(value?: string): void;
22
+ getWebsocketUrl(path?: string): string;
23
+ protected buildUrl(path: string, query?: Record<string, string | number | boolean | undefined>): string;
24
+ protected requestJson<T>(method: string, path: string, body?: unknown, options?: RequestOptions, auth?: 'access' | 'management' | 'local'): Promise<T>;
25
+ protected requestText(method: string, path: string, body?: string, options?: RequestOptions, auth?: 'access' | 'management' | 'local'): Promise<string>;
26
+ protected requestRaw(method: string, path: string, body?: BodyInit, options?: RequestOptions, auth?: 'access' | 'management' | 'local'): Promise<Response>;
27
+ protected applyAuth(headers: Record<string, string>, auth?: 'access' | 'management' | 'local'): void;
28
+ protected throwApiError(res: Response): Promise<never>;
29
+ }
@@ -0,0 +1,125 @@
1
+ import { APIError } from './errors.js';
2
+ export class BaseHttpClient {
3
+ constructor(options) {
4
+ this.baseUrl = options.baseUrl.replace(/\/+$/, '');
5
+ this.accessKey = options.accessKey;
6
+ this.managementKey = options.managementKey;
7
+ this.localPassword = options.localPassword;
8
+ this.fetchImpl = options.fetch ?? globalThis.fetch;
9
+ if (!this.fetchImpl) {
10
+ throw new Error('fetch is not available; pass options.fetch');
11
+ }
12
+ }
13
+ setAccessKey(key) {
14
+ this.accessKey = key;
15
+ }
16
+ setManagementKey(key) {
17
+ this.managementKey = key;
18
+ }
19
+ setLocalPassword(value) {
20
+ this.localPassword = value;
21
+ }
22
+ getWebsocketUrl(path = '/v1/ws') {
23
+ const base = this.baseUrl.replace(/\/+$/, '');
24
+ const wsBase = base.replace(/^http/, 'ws');
25
+ const normalized = path.startsWith('/') ? path : `/${path}`;
26
+ return `${wsBase}${normalized}`;
27
+ }
28
+ buildUrl(path, query) {
29
+ const url = new URL(path, this.baseUrl);
30
+ if (query) {
31
+ for (const [key, value] of Object.entries(query)) {
32
+ if (value === undefined)
33
+ continue;
34
+ url.searchParams.set(key, String(value));
35
+ }
36
+ }
37
+ return url.toString();
38
+ }
39
+ async requestJson(method, path, body, options, auth) {
40
+ const headers = {
41
+ 'Content-Type': 'application/json',
42
+ ...(options?.headers ?? {})
43
+ };
44
+ this.applyAuth(headers, auth);
45
+ const res = await this.fetchImpl(this.buildUrl(path, options?.query), {
46
+ method,
47
+ headers,
48
+ body: body === undefined ? undefined : JSON.stringify(body)
49
+ });
50
+ if (!res.ok) {
51
+ await this.throwApiError(res);
52
+ }
53
+ const text = await res.text();
54
+ if (!text)
55
+ return undefined;
56
+ return JSON.parse(text);
57
+ }
58
+ async requestText(method, path, body, options, auth) {
59
+ const headers = {
60
+ ...(options?.headers ?? {})
61
+ };
62
+ this.applyAuth(headers, auth);
63
+ const res = await this.fetchImpl(this.buildUrl(path, options?.query), {
64
+ method,
65
+ headers,
66
+ body
67
+ });
68
+ if (!res.ok) {
69
+ await this.throwApiError(res);
70
+ }
71
+ return res.text();
72
+ }
73
+ async requestRaw(method, path, body, options, auth) {
74
+ const headers = {
75
+ ...(options?.headers ?? {})
76
+ };
77
+ this.applyAuth(headers, auth);
78
+ const res = await this.fetchImpl(this.buildUrl(path, options?.query), {
79
+ method,
80
+ headers,
81
+ body
82
+ });
83
+ if (!res.ok) {
84
+ await this.throwApiError(res);
85
+ }
86
+ return res;
87
+ }
88
+ applyAuth(headers, auth) {
89
+ if (auth === 'access' && this.accessKey) {
90
+ headers.Authorization = `Bearer ${this.accessKey}`;
91
+ }
92
+ if (auth === 'management' && this.managementKey) {
93
+ headers.Authorization = `Bearer ${this.managementKey}`;
94
+ }
95
+ if (auth === 'local' && this.localPassword) {
96
+ headers.Authorization = `Bearer ${this.localPassword}`;
97
+ headers['X-Local-Password'] = this.localPassword;
98
+ }
99
+ }
100
+ async throwApiError(res) {
101
+ let payload;
102
+ let message = `Request failed (${res.status})`;
103
+ const text = await res.text();
104
+ if (text) {
105
+ try {
106
+ payload = JSON.parse(text);
107
+ }
108
+ catch {
109
+ payload = { error: text };
110
+ }
111
+ if (payload && typeof payload === 'object') {
112
+ const obj = payload;
113
+ if (typeof obj.error === 'string')
114
+ message = obj.error;
115
+ else if (obj.error && typeof obj.error === 'object' && typeof obj.error.message === 'string') {
116
+ message = obj.error.message;
117
+ }
118
+ else if (typeof obj.message === 'string') {
119
+ message = obj.message;
120
+ }
121
+ }
122
+ }
123
+ throw new APIError(message, res.status, (payload ?? { error: message }));
124
+ }
125
+ }
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './errors.js';
@@ -0,0 +1,2 @@
1
+ export * from './types.js';
2
+ export * from './errors.js';