@neta-art/cohub 1.0.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.
- package/README.md +132 -0
- package/dist/apis/channels.d.ts +13 -0
- package/dist/apis/channels.js +24 -0
- package/dist/apis/cron-jobs.d.ts +19 -0
- package/dist/apis/cron-jobs.js +32 -0
- package/dist/apis/models.d.ts +8 -0
- package/dist/apis/models.js +17 -0
- package/dist/apis/session-access.d.ts +13 -0
- package/dist/apis/session-access.js +19 -0
- package/dist/apis/spaces.d.ts +177 -0
- package/dist/apis/spaces.js +373 -0
- package/dist/apis/tasks.d.ts +20 -0
- package/dist/apis/tasks.js +25 -0
- package/dist/apis/user.d.ts +20 -0
- package/dist/apis/user.js +58 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.js +38 -0
- package/dist/http.d.ts +24 -0
- package/dist/http.js +34 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/realtime.d.ts +2 -0
- package/dist/realtime.js +5 -0
- package/dist/transport.d.ts +27 -0
- package/dist/transport.js +57 -0
- package/dist/types.d.ts +211 -0
- package/dist/types.js +1 -0
- package/dist/websocket.d.ts +119 -0
- package/dist/websocket.js +398 -0
- package/package.json +56 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export class HttpError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
body;
|
|
4
|
+
constructor(message, status, body) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "HttpError";
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.body = body;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class HttpTransport {
|
|
12
|
+
baseUrl;
|
|
13
|
+
fetcher;
|
|
14
|
+
getAccessToken;
|
|
15
|
+
onUnauthorized;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.baseUrl = options.baseUrl ?? "";
|
|
18
|
+
this.fetcher = options.fetch ?? fetch;
|
|
19
|
+
this.getAccessToken = options.getAccessToken;
|
|
20
|
+
this.onUnauthorized = options.onUnauthorized;
|
|
21
|
+
}
|
|
22
|
+
async withAuthorization(init) {
|
|
23
|
+
const headers = new Headers(init?.headers);
|
|
24
|
+
const token = this.getAccessToken ? await this.getAccessToken() : null;
|
|
25
|
+
if (token) {
|
|
26
|
+
headers.set("Authorization", `Bearer ${token}`);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
headers.delete("Authorization");
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
...init,
|
|
33
|
+
headers,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async request(path, init) {
|
|
37
|
+
const fetcher = init?.fetch ?? this.fetcher;
|
|
38
|
+
const url = this.baseUrl ? `${this.baseUrl}${path}` : path;
|
|
39
|
+
const response = await fetcher(url, await this.withAuthorization(init));
|
|
40
|
+
if (response.status === 401) {
|
|
41
|
+
await this.onUnauthorized?.();
|
|
42
|
+
throw new HttpError("unauthorized", 401, null);
|
|
43
|
+
}
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
46
|
+
const body = contentType.includes("application/json")
|
|
47
|
+
? await response.json().catch(() => null)
|
|
48
|
+
: await response.text().catch(() => response.statusText);
|
|
49
|
+
const message = typeof body === "string" ? body : JSON.stringify(body ?? null);
|
|
50
|
+
throw new HttpError(message || response.statusText, response.status, body);
|
|
51
|
+
}
|
|
52
|
+
if (response.status === 204) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
return response.json();
|
|
56
|
+
}
|
|
57
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import type { SessionBindingRecord as ProtocolSessionBindingRecord, SessionRecord as ProtocolSessionRecord } from "@neta-art/cohub-protocol/model";
|
|
2
|
+
import type { ChannelConfig } from "@neta-art/cohub-protocol/gateway";
|
|
3
|
+
import type { ContentBlock } from "@neta-art/cohub-protocol/core";
|
|
4
|
+
import type { MessageRecord } from "@neta-art/cohub-protocol/model";
|
|
5
|
+
export type { ChannelConfig, DiscordChannelConfig, } from "@neta-art/cohub-protocol/gateway";
|
|
6
|
+
export type ApiError = {
|
|
7
|
+
message: string;
|
|
8
|
+
};
|
|
9
|
+
export type { ContentBlock, MessageRecord };
|
|
10
|
+
export type SpaceFsEntry = {
|
|
11
|
+
name: string;
|
|
12
|
+
path: string;
|
|
13
|
+
type: "file" | "dir" | "symlink";
|
|
14
|
+
size: number;
|
|
15
|
+
mimeType: string | null;
|
|
16
|
+
mtimeMs: number;
|
|
17
|
+
};
|
|
18
|
+
export type SpaceFsTreeResponse = {
|
|
19
|
+
path: string;
|
|
20
|
+
entries: SpaceFsEntry[];
|
|
21
|
+
};
|
|
22
|
+
export type SpaceFsFileKind = "text" | "binary";
|
|
23
|
+
export type SpaceFsEncoding = "utf-8" | "base64";
|
|
24
|
+
export type SpaceFsFileResponse = {
|
|
25
|
+
path: string;
|
|
26
|
+
name: string;
|
|
27
|
+
size: number;
|
|
28
|
+
mimeType: string | null;
|
|
29
|
+
mtimeMs: number;
|
|
30
|
+
kind: SpaceFsFileKind;
|
|
31
|
+
encoding: SpaceFsEncoding;
|
|
32
|
+
content: string;
|
|
33
|
+
};
|
|
34
|
+
export type SpaceFsWriteFileInput = {
|
|
35
|
+
path: string;
|
|
36
|
+
content: string;
|
|
37
|
+
encoding: SpaceFsEncoding;
|
|
38
|
+
};
|
|
39
|
+
export type SpaceFsMoveInput = {
|
|
40
|
+
fromPath: string;
|
|
41
|
+
toPath: string;
|
|
42
|
+
};
|
|
43
|
+
export type SessionBindingRecord = ProtocolSessionBindingRecord;
|
|
44
|
+
export type SessionRecord = ProtocolSessionRecord & {
|
|
45
|
+
bindings?: SessionBindingRecord[];
|
|
46
|
+
totalMessages?: number;
|
|
47
|
+
totalToolCalls?: number;
|
|
48
|
+
totalInputTokens?: number;
|
|
49
|
+
totalOutputTokens?: number;
|
|
50
|
+
totalCost?: string | number | null;
|
|
51
|
+
};
|
|
52
|
+
export type SpaceRecord = {
|
|
53
|
+
id: string;
|
|
54
|
+
userUuid: string;
|
|
55
|
+
name: string | null;
|
|
56
|
+
description: string | null;
|
|
57
|
+
storageRepoName?: string | null;
|
|
58
|
+
baseCheckpointId?: string | null;
|
|
59
|
+
headCheckpointId?: string | null;
|
|
60
|
+
title: string | null;
|
|
61
|
+
status: string | null;
|
|
62
|
+
meta: Record<string, unknown> | null;
|
|
63
|
+
createdAt: string;
|
|
64
|
+
updatedAt: string;
|
|
65
|
+
channels?: {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string | null;
|
|
68
|
+
provider: string;
|
|
69
|
+
status: string;
|
|
70
|
+
}[];
|
|
71
|
+
accessLevel?: "minimal";
|
|
72
|
+
};
|
|
73
|
+
export type SpaceBootstrapSource = {
|
|
74
|
+
type: "blank";
|
|
75
|
+
} | {
|
|
76
|
+
type: "git_repo";
|
|
77
|
+
repoUrl?: string;
|
|
78
|
+
ref?: string | null;
|
|
79
|
+
} | {
|
|
80
|
+
type: "checkpoint";
|
|
81
|
+
checkpointId: string;
|
|
82
|
+
};
|
|
83
|
+
export type SpaceCreateResponse = {
|
|
84
|
+
space: SpaceRecord;
|
|
85
|
+
taskRunId: string;
|
|
86
|
+
};
|
|
87
|
+
export type SpaceListItem = SpaceRecord;
|
|
88
|
+
export type SessionMessagesResponse = {
|
|
89
|
+
space: SpaceRecord;
|
|
90
|
+
session: SessionRecord;
|
|
91
|
+
messages: MessageRecord[];
|
|
92
|
+
};
|
|
93
|
+
export type SessionMessagesPaginatedResponse = {
|
|
94
|
+
space: SpaceRecord;
|
|
95
|
+
session: SessionRecord;
|
|
96
|
+
messages: MessageRecord[];
|
|
97
|
+
hasMore: boolean;
|
|
98
|
+
nextCursor: number | undefined;
|
|
99
|
+
};
|
|
100
|
+
export type ModelCatalogEntry = {
|
|
101
|
+
provider: string;
|
|
102
|
+
id: string;
|
|
103
|
+
model: Record<string, unknown>;
|
|
104
|
+
};
|
|
105
|
+
export type Channel = {
|
|
106
|
+
id: string;
|
|
107
|
+
userUuid: string;
|
|
108
|
+
provider: string;
|
|
109
|
+
name: string;
|
|
110
|
+
status: string;
|
|
111
|
+
createdAt: string;
|
|
112
|
+
updatedAt: string;
|
|
113
|
+
boundSpace: {
|
|
114
|
+
id: string;
|
|
115
|
+
title: string | null;
|
|
116
|
+
status: string;
|
|
117
|
+
} | null;
|
|
118
|
+
};
|
|
119
|
+
export type SpaceEnvInput = {
|
|
120
|
+
name: string;
|
|
121
|
+
value: string;
|
|
122
|
+
};
|
|
123
|
+
export type SpaceChannelBindingInput = {
|
|
124
|
+
channelId: string;
|
|
125
|
+
config?: ChannelConfig | null;
|
|
126
|
+
};
|
|
127
|
+
export type SpaceSessionsResponse = {
|
|
128
|
+
space: SpaceRecord;
|
|
129
|
+
sessions: SessionRecord[];
|
|
130
|
+
};
|
|
131
|
+
export type UserSshKey = {
|
|
132
|
+
id: string;
|
|
133
|
+
key: string;
|
|
134
|
+
title: string;
|
|
135
|
+
giteaKeyId: number;
|
|
136
|
+
createdAt: string;
|
|
137
|
+
};
|
|
138
|
+
export type CronJobRecord = {
|
|
139
|
+
id: string;
|
|
140
|
+
userUuid: string;
|
|
141
|
+
title: string;
|
|
142
|
+
taskType: string;
|
|
143
|
+
payload: Record<string, unknown>;
|
|
144
|
+
cronExpression: string;
|
|
145
|
+
timezone: string;
|
|
146
|
+
bullJobKey: string;
|
|
147
|
+
spaceId: string | null;
|
|
148
|
+
sessionId: string | null;
|
|
149
|
+
enabled: boolean;
|
|
150
|
+
createdAt: string;
|
|
151
|
+
updatedAt: string;
|
|
152
|
+
};
|
|
153
|
+
export type TaskRunRecord = {
|
|
154
|
+
id: string;
|
|
155
|
+
jobId: string;
|
|
156
|
+
cronJobId: string | null;
|
|
157
|
+
taskType: string;
|
|
158
|
+
status: "pending" | "running" | "completed" | "failed";
|
|
159
|
+
payload: unknown;
|
|
160
|
+
result: unknown;
|
|
161
|
+
errorMessage: string | null;
|
|
162
|
+
attemptCount: number;
|
|
163
|
+
spaceId: string | null;
|
|
164
|
+
sessionId: string | null;
|
|
165
|
+
userUuid: string | null;
|
|
166
|
+
scheduledAt: string | null;
|
|
167
|
+
startedAt: string | null;
|
|
168
|
+
finishedAt: string | null;
|
|
169
|
+
createdAt: string;
|
|
170
|
+
updatedAt: string;
|
|
171
|
+
};
|
|
172
|
+
export type CheckpointRecord = {
|
|
173
|
+
id: string;
|
|
174
|
+
spaceId: string;
|
|
175
|
+
commitHash: string;
|
|
176
|
+
description: string;
|
|
177
|
+
parentCheckpointId: string | null;
|
|
178
|
+
forkCount: number;
|
|
179
|
+
meta: Record<string, unknown> | null;
|
|
180
|
+
createdAt: string;
|
|
181
|
+
};
|
|
182
|
+
export type SpaceCheckpointDetailResponse = {
|
|
183
|
+
checkpoint: CheckpointRecord;
|
|
184
|
+
};
|
|
185
|
+
export type CreateCronJobInput = {
|
|
186
|
+
title: string;
|
|
187
|
+
taskType: string;
|
|
188
|
+
payload: Record<string, unknown>;
|
|
189
|
+
cronExpression: string;
|
|
190
|
+
timezone?: string;
|
|
191
|
+
spaceId?: string;
|
|
192
|
+
sessionId?: string;
|
|
193
|
+
};
|
|
194
|
+
export type CreateScheduledTaskInput = {
|
|
195
|
+
taskType: string;
|
|
196
|
+
payload: Record<string, unknown>;
|
|
197
|
+
scheduleAt: string;
|
|
198
|
+
spaceId?: string;
|
|
199
|
+
sessionId?: string;
|
|
200
|
+
};
|
|
201
|
+
export type SpaceRole = "host" | "maker" | "guest";
|
|
202
|
+
export type SpaceMember = {
|
|
203
|
+
userId: string;
|
|
204
|
+
role: SpaceRole;
|
|
205
|
+
createdAt: string;
|
|
206
|
+
updatedAt: string;
|
|
207
|
+
};
|
|
208
|
+
export type SpaceAccessPolicy = {
|
|
209
|
+
signed_in_user: SpaceRole | null;
|
|
210
|
+
anonymous_user: SpaceRole | null;
|
|
211
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { type ChannelEnvelope } from "@neta-art/cohub-protocol/realtime";
|
|
2
|
+
import type { ContentBlock } from "@neta-art/cohub-protocol/core";
|
|
3
|
+
export type WebsocketEventPayload = ChannelEnvelope;
|
|
4
|
+
export type WebSocketLike = {
|
|
5
|
+
readonly readyState: number;
|
|
6
|
+
onopen: ((event: Event) => void) | null;
|
|
7
|
+
onmessage: ((event: MessageEvent) => void) | null;
|
|
8
|
+
onerror: ((event: Event) => void) | null;
|
|
9
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
10
|
+
send(data: string): void;
|
|
11
|
+
close(code?: number, reason?: string): void;
|
|
12
|
+
};
|
|
13
|
+
export type WebSocketConstructor = new (url: string) => WebSocketLike;
|
|
14
|
+
export type WebsocketClientOptions = {
|
|
15
|
+
url?: string;
|
|
16
|
+
autoReconnect?: boolean;
|
|
17
|
+
reconnectBaseDelayMs?: number;
|
|
18
|
+
reconnectMaxDelayMs?: number;
|
|
19
|
+
pingIntervalMs?: number;
|
|
20
|
+
pongTimeoutMs?: number;
|
|
21
|
+
debug?: boolean;
|
|
22
|
+
getAccessToken?: () => Promise<string | null> | string | null;
|
|
23
|
+
WebSocketImpl?: WebSocketConstructor;
|
|
24
|
+
};
|
|
25
|
+
export type WebsocketClientState = "idle" | "connecting" | "open" | "closed";
|
|
26
|
+
export type WebsocketClientEvents = {
|
|
27
|
+
open: {
|
|
28
|
+
connectionId?: string | null;
|
|
29
|
+
};
|
|
30
|
+
close: {
|
|
31
|
+
code: number;
|
|
32
|
+
reason: string;
|
|
33
|
+
willReconnect: boolean;
|
|
34
|
+
};
|
|
35
|
+
error: {
|
|
36
|
+
error: unknown;
|
|
37
|
+
};
|
|
38
|
+
event: WebsocketEventPayload;
|
|
39
|
+
ready: {
|
|
40
|
+
connectionId: string;
|
|
41
|
+
};
|
|
42
|
+
auth: {
|
|
43
|
+
connectionId: string;
|
|
44
|
+
user: Record<string, unknown>;
|
|
45
|
+
};
|
|
46
|
+
messageAccepted: {
|
|
47
|
+
requestId?: string | null;
|
|
48
|
+
clientMessageId?: string | null;
|
|
49
|
+
sessionId?: string | null;
|
|
50
|
+
spaceId?: string | null;
|
|
51
|
+
};
|
|
52
|
+
serverError: {
|
|
53
|
+
code?: string;
|
|
54
|
+
message?: string;
|
|
55
|
+
requestId?: string | null;
|
|
56
|
+
sessionId?: string | null;
|
|
57
|
+
spaceId?: string | null;
|
|
58
|
+
clientMessageId?: string | null;
|
|
59
|
+
};
|
|
60
|
+
pong: {
|
|
61
|
+
requestId?: string | null;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
type EventHandler<T> = (payload: T) => void;
|
|
65
|
+
export declare class WebsocketClient {
|
|
66
|
+
private readonly url;
|
|
67
|
+
private readonly autoReconnect;
|
|
68
|
+
private readonly reconnectBaseDelayMs;
|
|
69
|
+
private readonly reconnectMaxDelayMs;
|
|
70
|
+
private readonly pingIntervalMs;
|
|
71
|
+
private readonly pongTimeoutMs;
|
|
72
|
+
private readonly debug;
|
|
73
|
+
private readonly getAccessToken?;
|
|
74
|
+
private readonly WebSocketImpl;
|
|
75
|
+
private ws;
|
|
76
|
+
private pingTimer;
|
|
77
|
+
private reconnectTimer;
|
|
78
|
+
private reconnectAttempt;
|
|
79
|
+
private manuallyClosed;
|
|
80
|
+
private connectPromise;
|
|
81
|
+
private authWaiter;
|
|
82
|
+
private awaitingPong;
|
|
83
|
+
private lastPingRequestId;
|
|
84
|
+
private pongDeadlineAt;
|
|
85
|
+
state: WebsocketClientState;
|
|
86
|
+
connectionId: string | null;
|
|
87
|
+
private readonly listeners;
|
|
88
|
+
constructor(options?: WebsocketClientOptions);
|
|
89
|
+
on<K extends keyof WebsocketClientEvents>(type: K, handler: EventHandler<WebsocketClientEvents[K]>): () => void;
|
|
90
|
+
off<K extends keyof WebsocketClientEvents>(type: K, handler: EventHandler<WebsocketClientEvents[K]>): void;
|
|
91
|
+
private emit;
|
|
92
|
+
private log;
|
|
93
|
+
connect(): Promise<void>;
|
|
94
|
+
disconnect(code?: number, reason?: string): Promise<void>;
|
|
95
|
+
sendMessage(input: {
|
|
96
|
+
spaceId: string;
|
|
97
|
+
sessionId: string;
|
|
98
|
+
content: ContentBlock[];
|
|
99
|
+
clientMessageId?: string;
|
|
100
|
+
requestId?: string;
|
|
101
|
+
model?: string;
|
|
102
|
+
provider?: string;
|
|
103
|
+
}): Promise<void>;
|
|
104
|
+
ack(eventId?: string, requestId?: string): void;
|
|
105
|
+
ping(requestId?: string): void;
|
|
106
|
+
private ensureOpen;
|
|
107
|
+
private send;
|
|
108
|
+
private authenticate;
|
|
109
|
+
private createAuthWaiter;
|
|
110
|
+
private resolveAuthWaiter;
|
|
111
|
+
private rejectAuthWaiter;
|
|
112
|
+
private handleMessage;
|
|
113
|
+
private startPingLoop;
|
|
114
|
+
private stopPingLoop;
|
|
115
|
+
private clearReconnectTimer;
|
|
116
|
+
private scheduleReconnect;
|
|
117
|
+
}
|
|
118
|
+
export declare const createWebsocketClient: (options?: WebsocketClientOptions) => WebsocketClient;
|
|
119
|
+
export {};
|