@sixb/client 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/LICENSE +21 -0
- package/README.md +193 -0
- package/dist/actions.d.ts +61 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/agent-streams.d.ts +72 -0
- package/dist/agent-streams.d.ts.map +1 -0
- package/dist/api.d.ts +54 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/browser.d.ts +41 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +174 -0
- package/dist/browser.js.map +10 -0
- package/dist/chunk-05yz32g0.js +240 -0
- package/dist/chunk-05yz32g0.js.map +10 -0
- package/dist/chunk-2d7239yx.js +1181 -0
- package/dist/chunk-2d7239yx.js.map +17 -0
- package/dist/chunk-3g6ad4jc.js +147 -0
- package/dist/chunk-3g6ad4jc.js.map +10 -0
- package/dist/chunk-aw8rcsq3.js +145 -0
- package/dist/chunk-aw8rcsq3.js.map +10 -0
- package/dist/chunk-ctsdm59b.js +1477 -0
- package/dist/chunk-ctsdm59b.js.map +19 -0
- package/dist/chunk-fxrh83wy.js +180 -0
- package/dist/chunk-fxrh83wy.js.map +12 -0
- package/dist/chunk-pey7nh0d.js +123 -0
- package/dist/chunk-pey7nh0d.js.map +10 -0
- package/dist/chunk-trpg18dp.js +3 -0
- package/dist/chunk-trpg18dp.js.map +9 -0
- package/dist/events-builder.d.ts +218 -0
- package/dist/events-builder.d.ts.map +1 -0
- package/dist/events-hooks.d.ts +50 -0
- package/dist/events-hooks.d.ts.map +1 -0
- package/dist/events-model.d.ts +12 -0
- package/dist/events-model.d.ts.map +1 -0
- package/dist/events-provider.d.ts +56 -0
- package/dist/events-provider.d.ts.map +1 -0
- package/dist/events-transport.d.ts +62 -0
- package/dist/events-transport.d.ts.map +1 -0
- package/dist/events.d.ts +5 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/file.d.ts +27 -0
- package/dist/file.d.ts.map +1 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts +17064 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -0
- package/dist/generated/client/client.gen.d.ts +3 -0
- package/dist/generated/client/client.gen.d.ts.map +1 -0
- package/dist/generated/client/index.d.ts +9 -0
- package/dist/generated/client/index.d.ts.map +1 -0
- package/dist/generated/client/types.gen.d.ts +118 -0
- package/dist/generated/client/types.gen.d.ts.map +1 -0
- package/dist/generated/client/utils.gen.d.ts +34 -0
- package/dist/generated/client/utils.gen.d.ts.map +1 -0
- package/dist/generated/client.gen.d.ts +13 -0
- package/dist/generated/client.gen.d.ts.map +1 -0
- package/dist/generated/core/auth.gen.d.ts +19 -0
- package/dist/generated/core/auth.gen.d.ts.map +1 -0
- package/dist/generated/core/bodySerializer.gen.d.ts +26 -0
- package/dist/generated/core/bodySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/params.gen.d.ts +44 -0
- package/dist/generated/core/params.gen.d.ts.map +1 -0
- package/dist/generated/core/pathSerializer.gen.d.ts +34 -0
- package/dist/generated/core/pathSerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts +19 -0
- package/dist/generated/core/queryKeySerializer.gen.d.ts.map +1 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts +72 -0
- package/dist/generated/core/serverSentEvents.gen.d.ts.map +1 -0
- package/dist/generated/core/types.gen.d.ts +79 -0
- package/dist/generated/core/types.gen.d.ts.map +1 -0
- package/dist/generated/core/utils.gen.d.ts +20 -0
- package/dist/generated/core/utils.gen.d.ts.map +1 -0
- package/dist/generated/index.d.ts +3 -0
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/sdk.gen.d.ts +464 -0
- package/dist/generated/sdk.gen.d.ts.map +1 -0
- package/dist/generated/types.gen.d.ts +7672 -0
- package/dist/generated/types.gen.d.ts.map +1 -0
- package/dist/hooks.d.ts +123 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +3179 -0
- package/dist/hooks.js.map +16 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +329 -0
- package/dist/index.js.map +9 -0
- package/dist/logs-builder.d.ts +59 -0
- package/dist/logs-builder.d.ts.map +1 -0
- package/dist/logs-hooks.d.ts +30 -0
- package/dist/logs-hooks.d.ts.map +1 -0
- package/dist/logs-transport.d.ts +38 -0
- package/dist/logs-transport.d.ts.map +1 -0
- package/dist/logs.d.ts +7 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +22 -0
- package/dist/logs.js.map +9 -0
- package/dist/models.d.ts +126 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +252 -0
- package/dist/models.js.map +9 -0
- package/dist/query-hooks.d.ts +365 -0
- package/dist/query-hooks.d.ts.map +1 -0
- package/dist/query.d.ts +27 -0
- package/dist/query.d.ts.map +1 -0
- package/dist/query.js +15 -0
- package/dist/query.js.map +9 -0
- package/dist/sha256.d.ts +15 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/telemetry-events.d.ts +25 -0
- package/dist/telemetry-events.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/useAgentRunStream.d.ts +21 -0
- package/dist/useAgentRunStream.d.ts.map +1 -0
- package/dist/ws-socket.d.ts +45 -0
- package/dist/ws-socket.d.ts.map +1 -0
- package/package.json +105 -0
- package/src/actions.ts +310 -0
- package/src/agent-streams.ts +275 -0
- package/src/api.ts +229 -0
- package/src/browser.ts +267 -0
- package/src/events-builder.ts +735 -0
- package/src/events-hooks.ts +233 -0
- package/src/events-model.ts +40 -0
- package/src/events-provider.ts +161 -0
- package/src/events-transport.ts +193 -0
- package/src/events.ts +6 -0
- package/src/file.ts +367 -0
- package/src/generated/@tanstack/react-query.gen.ts +3876 -0
- package/src/generated/client/client.gen.ts +288 -0
- package/src/generated/client/index.ts +25 -0
- package/src/generated/client/types.gen.ts +209 -0
- package/src/generated/client/utils.gen.ts +316 -0
- package/src/generated/client.gen.ts +18 -0
- package/src/generated/core/auth.gen.ts +41 -0
- package/src/generated/core/bodySerializer.gen.ts +84 -0
- package/src/generated/core/params.gen.ts +169 -0
- package/src/generated/core/pathSerializer.gen.ts +171 -0
- package/src/generated/core/queryKeySerializer.gen.ts +117 -0
- package/src/generated/core/serverSentEvents.gen.ts +243 -0
- package/src/generated/core/types.gen.ts +104 -0
- package/src/generated/core/utils.gen.ts +140 -0
- package/src/generated/index.ts +672 -0
- package/src/generated/sdk.gen.ts +1984 -0
- package/src/generated/types.gen.ts +9049 -0
- package/src/hooks.ts +404 -0
- package/src/index.ts +18 -0
- package/src/logs-builder.ts +171 -0
- package/src/logs-hooks.ts +112 -0
- package/src/logs-transport.ts +112 -0
- package/src/logs.ts +15 -0
- package/src/models.ts +420 -0
- package/src/query-hooks.ts +1021 -0
- package/src/query.ts +175 -0
- package/src/sha256.ts +159 -0
- package/src/telemetry-events.ts +71 -0
- package/src/useAgentRunStream.ts +62 -0
- package/src/ws-socket.ts +202 -0
package/src/browser.ts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import type { AuthSessionAudience } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
configureSixbClient as configureGeneratedSixbClient,
|
|
4
|
+
normalizeSixbApiBaseUrl,
|
|
5
|
+
SIXB_CSRF_TOKEN_RESPONSE_HEADER_NAME,
|
|
6
|
+
} from "./api"
|
|
7
|
+
import { client } from "./generated/client.gen"
|
|
8
|
+
|
|
9
|
+
export type { SixbApiErrorInit } from "./api"
|
|
10
|
+
export { isSixbApiError, SixbApiError } from "./api"
|
|
11
|
+
|
|
12
|
+
import { getAuthSession } from "./generated/sdk.gen"
|
|
13
|
+
import type { GetAuthSessionResponse } from "./generated/types.gen"
|
|
14
|
+
|
|
15
|
+
const SESSION_ACTIVITY_HEADER_NAME = "x-sixb-session-activity"
|
|
16
|
+
const SESSION_ACTIVITY_HEADER_VALUE = "1"
|
|
17
|
+
const SESSION_ACTIVITY_WINDOW_MS = 5 * 60_000
|
|
18
|
+
const SESSION_ACTIVITY_EVENT_TYPES = ["pointerdown", "keydown", "touchstart"] as const
|
|
19
|
+
const SESSION_ACTIVITY_LISTENER_OPTIONS = { capture: true, passive: true } as const
|
|
20
|
+
|
|
21
|
+
export interface SixbBrowserRuntimeConfig {
|
|
22
|
+
readonly api: {
|
|
23
|
+
readonly baseUrl: string
|
|
24
|
+
}
|
|
25
|
+
readonly auth: {
|
|
26
|
+
readonly audience: AuthSessionAudience
|
|
27
|
+
readonly enabled: boolean
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SixbBrowserRuntimeDefaults {
|
|
32
|
+
readonly apiBaseUrl?: string
|
|
33
|
+
readonly audience: AuthSessionAudience
|
|
34
|
+
readonly authEnabled?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface SixbBrowserClientController {
|
|
38
|
+
setCsrfToken(token: string | null): void
|
|
39
|
+
getCsrfToken(): string | null
|
|
40
|
+
dispose(): void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SixbBrowserClientOptions {
|
|
44
|
+
readonly getCurrentUrl?: () => string | null
|
|
45
|
+
readonly redirect?: (url: string) => void
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let activeBrowserController: SixbBrowserClientController | null = null
|
|
49
|
+
|
|
50
|
+
declare global {
|
|
51
|
+
interface Window {
|
|
52
|
+
__SIXB_RUNTIME__?: Partial<SixbBrowserRuntimeConfig>
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function renderSixbBrowserRuntimeScript(config: SixbBrowserRuntimeConfig): string {
|
|
57
|
+
const safeConfig = JSON.stringify(config).replaceAll("<", "\\u003c")
|
|
58
|
+
return `<script>window.__SIXB_RUNTIME__ = ${safeConfig};</script>`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function readSixbBrowserRuntimeConfig(
|
|
62
|
+
defaults: SixbBrowserRuntimeDefaults
|
|
63
|
+
): SixbBrowserRuntimeConfig {
|
|
64
|
+
const runtime = window.__SIXB_RUNTIME__
|
|
65
|
+
const runtimeAuthEnabled = runtime?.auth?.enabled
|
|
66
|
+
return {
|
|
67
|
+
api: {
|
|
68
|
+
baseUrl: normalizeSixbApiBaseUrl(
|
|
69
|
+
runtime?.api?.baseUrl ?? defaults.apiBaseUrl ?? window.location.origin
|
|
70
|
+
),
|
|
71
|
+
},
|
|
72
|
+
auth: {
|
|
73
|
+
audience: runtime?.auth?.audience ?? defaults.audience,
|
|
74
|
+
enabled:
|
|
75
|
+
typeof runtimeAuthEnabled === "boolean"
|
|
76
|
+
? runtimeAuthEnabled
|
|
77
|
+
: (defaults.authEnabled ?? true),
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function configureSixbBrowserClient(
|
|
83
|
+
config: SixbBrowserRuntimeConfig,
|
|
84
|
+
options: SixbBrowserClientOptions = {}
|
|
85
|
+
): SixbBrowserClientController {
|
|
86
|
+
activeBrowserController?.dispose()
|
|
87
|
+
|
|
88
|
+
let csrfToken: string | null = null
|
|
89
|
+
let disposed = false
|
|
90
|
+
let redirectStarted = false
|
|
91
|
+
const browserDocument = typeof document === "undefined" ? null : document
|
|
92
|
+
let lastActivityAt = browserDocument?.visibilityState === "visible" ? Date.now() : null
|
|
93
|
+
|
|
94
|
+
const recordVisibleActivity = () => {
|
|
95
|
+
if (browserDocument?.visibilityState === "visible") {
|
|
96
|
+
lastActivityAt = Date.now()
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (browserDocument) {
|
|
101
|
+
for (const type of SESSION_ACTIVITY_EVENT_TYPES) {
|
|
102
|
+
browserDocument.addEventListener(
|
|
103
|
+
type,
|
|
104
|
+
recordVisibleActivity,
|
|
105
|
+
SESSION_ACTIVITY_LISTENER_OPTIONS
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
browserDocument.addEventListener("visibilitychange", recordVisibleActivity)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
configureGeneratedSixbClient(client, {
|
|
112
|
+
baseUrl: config.api.baseUrl,
|
|
113
|
+
auth: {
|
|
114
|
+
kind: "cookie",
|
|
115
|
+
csrfToken: () => csrfToken,
|
|
116
|
+
},
|
|
117
|
+
})
|
|
118
|
+
const activityInterceptorId = client.interceptors.request.use((request) => {
|
|
119
|
+
const activityAge =
|
|
120
|
+
lastActivityAt === null ? Number.POSITIVE_INFINITY : Date.now() - lastActivityAt
|
|
121
|
+
const hasRecentVisibleActivity =
|
|
122
|
+
browserDocument?.visibilityState === "visible" &&
|
|
123
|
+
activityAge >= 0 &&
|
|
124
|
+
activityAge < SESSION_ACTIVITY_WINDOW_MS
|
|
125
|
+
|
|
126
|
+
if (hasRecentVisibleActivity) {
|
|
127
|
+
request.headers.set(SESSION_ACTIVITY_HEADER_NAME, SESSION_ACTIVITY_HEADER_VALUE)
|
|
128
|
+
} else {
|
|
129
|
+
request.headers.delete(SESSION_ACTIVITY_HEADER_NAME)
|
|
130
|
+
}
|
|
131
|
+
return request
|
|
132
|
+
})
|
|
133
|
+
const authResponseInterceptorId = client.interceptors.response.use((response, request) => {
|
|
134
|
+
const responseCsrfToken = response.headers.get(SIXB_CSRF_TOKEN_RESPONSE_HEADER_NAME)
|
|
135
|
+
if (responseCsrfToken) {
|
|
136
|
+
csrfToken = responseCsrfToken
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (
|
|
140
|
+
!config.auth.enabled ||
|
|
141
|
+
response.status !== 401 ||
|
|
142
|
+
isAuthRedirectExcludedRequest(config, request) ||
|
|
143
|
+
request.headers.has("authorization")
|
|
144
|
+
) {
|
|
145
|
+
return response
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
csrfToken = null
|
|
149
|
+
if (redirectStarted) {
|
|
150
|
+
return response
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const currentUrl = options.getCurrentUrl
|
|
155
|
+
? options.getCurrentUrl()
|
|
156
|
+
: typeof window === "undefined"
|
|
157
|
+
? null
|
|
158
|
+
: window.location.href
|
|
159
|
+
if (!currentUrl || isSixbAuthPage(config, currentUrl)) {
|
|
160
|
+
return response
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
redirectStarted = true
|
|
164
|
+
const redirect =
|
|
165
|
+
options.redirect ??
|
|
166
|
+
((url: string) => {
|
|
167
|
+
if (typeof window !== "undefined") window.location.assign(url)
|
|
168
|
+
})
|
|
169
|
+
redirect(createSixbSignInUrl(config, currentUrl))
|
|
170
|
+
} catch {
|
|
171
|
+
// Allow a later response to retry navigation without replacing the original API error.
|
|
172
|
+
redirectStarted = false
|
|
173
|
+
}
|
|
174
|
+
return response
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
const controller: SixbBrowserClientController = {
|
|
178
|
+
setCsrfToken(token) {
|
|
179
|
+
csrfToken = token
|
|
180
|
+
},
|
|
181
|
+
getCsrfToken() {
|
|
182
|
+
return csrfToken
|
|
183
|
+
},
|
|
184
|
+
dispose() {
|
|
185
|
+
if (disposed) return
|
|
186
|
+
disposed = true
|
|
187
|
+
csrfToken = null
|
|
188
|
+
lastActivityAt = null
|
|
189
|
+
client.interceptors.request.eject(activityInterceptorId)
|
|
190
|
+
client.interceptors.response.eject(authResponseInterceptorId)
|
|
191
|
+
if (browserDocument) {
|
|
192
|
+
for (const type of SESSION_ACTIVITY_EVENT_TYPES) {
|
|
193
|
+
browserDocument.removeEventListener(
|
|
194
|
+
type,
|
|
195
|
+
recordVisibleActivity,
|
|
196
|
+
SESSION_ACTIVITY_LISTENER_OPTIONS
|
|
197
|
+
)
|
|
198
|
+
}
|
|
199
|
+
browserDocument.removeEventListener("visibilitychange", recordVisibleActivity)
|
|
200
|
+
}
|
|
201
|
+
if (activeBrowserController === controller) {
|
|
202
|
+
activeBrowserController = null
|
|
203
|
+
client.setConfig({ auth: undefined, credentials: undefined })
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
activeBrowserController = controller
|
|
209
|
+
return controller
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function isAuthRedirectExcludedRequest(
|
|
213
|
+
config: SixbBrowserRuntimeConfig,
|
|
214
|
+
request: Request
|
|
215
|
+
): boolean {
|
|
216
|
+
const requestUrl = new URL(request.url)
|
|
217
|
+
const apiUrl = new URL(normalizeSixbApiBaseUrl(config.api.baseUrl), requestUrl)
|
|
218
|
+
const apiPath = apiUrl.pathname.replace(/\/+$/, "")
|
|
219
|
+
const method = request.method.toUpperCase()
|
|
220
|
+
return (
|
|
221
|
+
requestUrl.origin === apiUrl.origin &&
|
|
222
|
+
((method === "GET" && requestUrl.pathname === `${apiPath}/api/auth/session`) ||
|
|
223
|
+
(method === "POST" && requestUrl.pathname === `${apiPath}/api/auth/sign-out`))
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function isSixbAuthPage(config: SixbBrowserRuntimeConfig, currentUrl: string): boolean {
|
|
228
|
+
try {
|
|
229
|
+
const current = new URL(currentUrl)
|
|
230
|
+
const api = new URL(normalizeSixbApiBaseUrl(config.api.baseUrl), current)
|
|
231
|
+
return (
|
|
232
|
+
current.origin === api.origin &&
|
|
233
|
+
(current.pathname === "/auth" || current.pathname.startsWith("/auth/"))
|
|
234
|
+
)
|
|
235
|
+
} catch {
|
|
236
|
+
return false
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export async function requireSixbBrowserAuthSession(
|
|
241
|
+
config: SixbBrowserRuntimeConfig,
|
|
242
|
+
controller: SixbBrowserClientController,
|
|
243
|
+
options: {
|
|
244
|
+
readonly returnTo?: string
|
|
245
|
+
readonly redirect?: (url: string) => void
|
|
246
|
+
} = {}
|
|
247
|
+
): Promise<GetAuthSessionResponse> {
|
|
248
|
+
const { data } = await getAuthSession({ throwOnError: true })
|
|
249
|
+
|
|
250
|
+
if (data.authenticated) {
|
|
251
|
+
controller.setCsrfToken(data.csrfToken)
|
|
252
|
+
return data
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
controller.setCsrfToken(null)
|
|
256
|
+
const returnTo = options.returnTo ?? window.location.href
|
|
257
|
+
const redirect = options.redirect ?? ((url: string) => window.location.assign(url))
|
|
258
|
+
redirect(createSixbSignInUrl(config, returnTo))
|
|
259
|
+
return data
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function createSixbSignInUrl(config: SixbBrowserRuntimeConfig, returnTo: string): string {
|
|
263
|
+
const url = new URL("/auth/sign-in", normalizeSixbApiBaseUrl(config.api.baseUrl))
|
|
264
|
+
url.searchParams.set("audience", config.auth.audience)
|
|
265
|
+
url.searchParams.set("returnTo", returnTo)
|
|
266
|
+
return url.toString()
|
|
267
|
+
}
|