@sixb/client 0.1.12 → 0.1.13

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 (98) hide show
  1. package/dist/agent-streams.d.ts +7 -2
  2. package/dist/agent-streams.d.ts.map +1 -1
  3. package/dist/api.d.ts +8 -1
  4. package/dist/api.d.ts.map +1 -1
  5. package/dist/base-url.d.ts +2 -0
  6. package/dist/base-url.d.ts.map +1 -0
  7. package/dist/browser.js +7 -7
  8. package/dist/browser.js.map +3 -3
  9. package/dist/{chunk-sjb188se.js → chunk-1d0xnr6p.js} +38 -7
  10. package/dist/chunk-1d0xnr6p.js.map +10 -0
  11. package/dist/{chunk-bsy1hg20.js → chunk-50rkzhcy.js} +23 -30
  12. package/dist/chunk-50rkzhcy.js.map +10 -0
  13. package/dist/{chunk-hx3n4fer.js → chunk-6k1hzwk1.js} +13 -11
  14. package/dist/chunk-6k1hzwk1.js.map +17 -0
  15. package/dist/{chunk-e8qjsvha.js → chunk-hdgztpzs.js} +364 -71
  16. package/dist/chunk-hdgztpzs.js.map +19 -0
  17. package/dist/{chunk-tyym13kq.js → chunk-jh4ts8mt.js} +3 -1
  18. package/dist/chunk-jh4ts8mt.js.map +10 -0
  19. package/dist/{chunk-kh20z2mt.js → chunk-nkx0fxxn.js} +2 -2
  20. package/dist/chunk-nkx0fxxn.js.map +10 -0
  21. package/dist/{chunk-t50n91tk.js → chunk-tgyh8wz9.js} +7 -6
  22. package/dist/chunk-tgyh8wz9.js.map +12 -0
  23. package/dist/chunk-ys86w6hc.js +277 -0
  24. package/dist/chunk-ys86w6hc.js.map +12 -0
  25. package/dist/client-authority.d.ts +4 -0
  26. package/dist/client-authority.d.ts.map +1 -1
  27. package/dist/connectors/queries.d.ts.map +1 -1
  28. package/dist/events-builder.d.ts +2 -2
  29. package/dist/events-builder.d.ts.map +1 -1
  30. package/dist/events-transport.d.ts +4 -1
  31. package/dist/events-transport.d.ts.map +1 -1
  32. package/dist/generated/@tanstack/react-query.gen.d.ts +1019 -365
  33. package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -1
  34. package/dist/generated/index.d.ts +2 -2
  35. package/dist/generated/index.d.ts.map +1 -1
  36. package/dist/generated/sdk.gen.d.ts +5 -1
  37. package/dist/generated/sdk.gen.d.ts.map +1 -1
  38. package/dist/generated/types.gen.d.ts +332 -79
  39. package/dist/generated/types.gen.d.ts.map +1 -1
  40. package/dist/hooks.js +29 -12
  41. package/dist/hooks.js.map +6 -6
  42. package/dist/index.d.ts +2 -0
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +21 -10
  45. package/dist/index.js.map +1 -1
  46. package/dist/logs-builder.d.ts +1 -1
  47. package/dist/logs-builder.d.ts.map +1 -1
  48. package/dist/logs-transport.d.ts +4 -1
  49. package/dist/logs-transport.d.ts.map +1 -1
  50. package/dist/logs.js +4 -4
  51. package/dist/models.d.ts +2 -1
  52. package/dist/models.d.ts.map +1 -1
  53. package/dist/models.js +5 -3
  54. package/dist/models.js.map +1 -1
  55. package/dist/query-hooks.d.ts +3 -21
  56. package/dist/query-hooks.d.ts.map +1 -1
  57. package/dist/query.d.ts +2 -4
  58. package/dist/query.d.ts.map +1 -1
  59. package/dist/query.js +4 -4
  60. package/dist/session.d.ts +56 -0
  61. package/dist/session.d.ts.map +1 -0
  62. package/dist/useAgentActivityStream.d.ts +3 -0
  63. package/dist/useAgentActivityStream.d.ts.map +1 -1
  64. package/dist/useAgentRunStream.d.ts +4 -1
  65. package/dist/useAgentRunStream.d.ts.map +1 -1
  66. package/dist/ws-socket.d.ts +3 -0
  67. package/dist/ws-socket.d.ts.map +1 -1
  68. package/package.json +2 -2
  69. package/src/agent-streams.ts +11 -4
  70. package/src/api.ts +37 -32
  71. package/src/base-url.ts +30 -0
  72. package/src/browser.ts +1 -1
  73. package/src/client-authority.ts +19 -0
  74. package/src/events-builder.ts +5 -9
  75. package/src/events-transport.ts +6 -2
  76. package/src/generated/@tanstack/react-query.gen.ts +31 -0
  77. package/src/generated/index.ts +6 -0
  78. package/src/generated/sdk.gen.ts +376 -69
  79. package/src/generated/types.gen.ts +429 -75
  80. package/src/index.ts +13 -1
  81. package/src/logs-builder.ts +2 -2
  82. package/src/logs-transport.ts +6 -2
  83. package/src/models.ts +4 -2
  84. package/src/query-hooks.ts +2 -17
  85. package/src/query.ts +6 -11
  86. package/src/session.ts +316 -0
  87. package/src/useAgentActivityStream.ts +6 -2
  88. package/src/useAgentRunStream.ts +7 -3
  89. package/src/ws-socket.ts +62 -5
  90. package/dist/chunk-65zz479s.js +0 -23
  91. package/dist/chunk-65zz479s.js.map +0 -10
  92. package/dist/chunk-bsy1hg20.js.map +0 -10
  93. package/dist/chunk-e8qjsvha.js.map +0 -19
  94. package/dist/chunk-hx3n4fer.js.map +0 -17
  95. package/dist/chunk-kh20z2mt.js.map +0 -10
  96. package/dist/chunk-sjb188se.js.map +0 -10
  97. package/dist/chunk-t50n91tk.js.map +0 -12
  98. package/dist/chunk-tyym13kq.js.map +0 -10
package/src/api.ts CHANGED
@@ -1,7 +1,15 @@
1
1
  import type { SixbErrorCode } from "@sixb/core"
2
- import { assertSharedAccessGrantId, markClientSharedAuthority } from "./client-authority"
2
+ import { normalizeSixbApiBaseUrl } from "./base-url"
3
+ import {
4
+ assertSharedAccessGrantId,
5
+ markClientSessionAuthority,
6
+ markClientSharedAuthority,
7
+ } from "./client-authority"
3
8
  import { type Auth, type Client, type Config, createClient, createConfig } from "./generated/client"
4
9
  import { client as sharedClient } from "./generated/client.gen"
10
+ import { createSixbSessionFetch, type SixbSessionOptions, type SixbSessionStore } from "./session"
11
+
12
+ export { normalizeSixbApiBaseUrl } from "./base-url"
5
13
 
6
14
  export const SIXB_CSRF_HEADER_NAME = "x-sixb-csrf"
7
15
  export const SIXB_CSRF_TOKEN_RESPONSE_HEADER_NAME = "x-sixb-csrf-token"
@@ -23,6 +31,13 @@ export type SixbClientAuth =
23
31
  readonly grantId: string
24
32
  readonly csrfToken?: () => string | null | undefined
25
33
  }
34
+ | {
35
+ /** A native client's session: its access token is sent, and refreshed, on every request. */
36
+ readonly kind: "session"
37
+ readonly store: SixbSessionStore
38
+ /** Called once when the session ends; the store has been cleared. Sign the user in again. */
39
+ readonly onSessionEnded?: () => void
40
+ }
26
41
  | {
27
42
  readonly kind: "none"
28
43
  }
@@ -94,6 +109,7 @@ export function isSixbApiError(value: unknown): value is SixbApiError {
94
109
  export function createSixbClient(options: SixbClientOptions = {}): SixbClient {
95
110
  const client = createClient(createSixbClientConfig(options))
96
111
  markClientSharedAuthority(client, options.auth?.kind === "shared")
112
+ markClientSessionAuthority(client, sixbSessionOptions(options))
97
113
  installSixbErrorInterceptor(client)
98
114
  return client
99
115
  }
@@ -104,6 +120,7 @@ export function configureSixbClient(
104
120
  ): SixbClient {
105
121
  client.setConfig(createSixbClientConfig(options))
106
122
  markClientSharedAuthority(client, options.auth?.kind === "shared")
123
+ markClientSessionAuthority(client, sixbSessionOptions(options))
107
124
  installSixbErrorInterceptor(client)
108
125
  return client
109
126
  }
@@ -119,6 +136,9 @@ export function createSixbClientConfig(options: SixbClientOptions = {}): Config
119
136
 
120
137
  const baseUrl =
121
138
  options.baseUrl === undefined ? undefined : normalizeSixbApiBaseUrl(options.baseUrl)
139
+ const session = sixbSessionOptions(options)
140
+ // Shares and sessions stamp their credential at the transport, on every request to the API. The
141
+ // generated per-operation auth only covers operations whose OpenAPI entry declares that scheme.
122
142
  const configuredFetch =
123
143
  auth.kind === "shared"
124
144
  ? createSharedAuthorityFetch({
@@ -126,36 +146,34 @@ export function createSixbClientConfig(options: SixbClientOptions = {}): Config
126
146
  grantId: auth.grantId,
127
147
  fetch: options.fetch ?? globalThis.fetch,
128
148
  })
129
- : options.fetch
149
+ : session
150
+ ? createSixbSessionFetch(session)
151
+ : options.fetch
130
152
 
131
153
  return createConfig({
132
154
  ...(baseUrl === undefined ? {} : { baseUrl }),
133
155
  ...(configuredFetch === undefined ? {} : { fetch: configuredFetch }),
134
156
  ...(options.headers === undefined ? {} : { headers: options.headers }),
135
157
  credentials: auth.kind === "cookie" || auth.kind === "shared" ? "include" : options.credentials,
136
- auth: auth.kind === "none" ? undefined : createSixbAuthResolver(auth),
158
+ auth:
159
+ auth.kind === "none" || auth.kind === "session" ? undefined : createSixbAuthResolver(auth),
137
160
  })
138
161
  }
139
162
 
140
- export function normalizeSixbApiBaseUrl(value: string): string {
141
- const absolute = /^[a-z][a-z\d+.-]*:\/\//i.test(value)
142
- let url: URL
143
-
144
- try {
145
- url = new URL(value, absolute ? undefined : "http://sixb.local")
146
- } catch {
147
- throw new Error(`[SixbClient] Invalid API base URL '${value}'.`)
148
- }
149
-
150
- const pathname = stripTrailingApiPath(url.pathname)
151
- if (!absolute) {
152
- return pathname
163
+ function sixbSessionOptions(options: SixbClientOptions): SixbSessionOptions | null {
164
+ const auth = options.auth
165
+ if (auth?.kind !== "session") return null
166
+ return {
167
+ baseUrl: options.baseUrl === undefined ? "" : normalizeSixbApiBaseUrl(options.baseUrl),
168
+ store: auth.store,
169
+ onSessionEnded: auth.onSessionEnded,
170
+ fetch: options.fetch,
153
171
  }
154
-
155
- return `${url.origin}${pathname === "/" ? "" : pathname}`
156
172
  }
157
173
 
158
- function createSixbAuthResolver(auth: SixbClientAuth): Config["auth"] {
174
+ function createSixbAuthResolver(
175
+ auth: Exclude<SixbClientAuth, { readonly kind: "none" | "session" }>
176
+ ): Config["auth"] {
159
177
  return (scheme: Auth) => {
160
178
  if (auth.kind === "bearer") {
161
179
  return isBearerAuth(scheme) ? auth.token : undefined
@@ -243,19 +261,6 @@ function resolveSharedApiOrigin(baseUrl: string | undefined): string {
243
261
  }
244
262
  }
245
263
 
246
- function stripTrailingApiPath(pathname: string): string {
247
- const trimmed = pathname.replace(/\/+$/, "")
248
- if (!trimmed || trimmed === "/api") {
249
- return ""
250
- }
251
-
252
- if (trimmed.endsWith("/api")) {
253
- return trimmed.slice(0, -4) || ""
254
- }
255
-
256
- return trimmed
257
- }
258
-
259
264
  /**
260
265
  * Maps the generated client's raw error (bare body) into a `SixbApiError`.
261
266
  * Runs for every failed response regardless of `throwOnError`, so both thrown
@@ -0,0 +1,30 @@
1
+ export function normalizeSixbApiBaseUrl(value: string): string {
2
+ const absolute = /^[a-z][a-z\d+.-]*:\/\//i.test(value)
3
+ let url: URL
4
+
5
+ try {
6
+ url = new URL(value, absolute ? undefined : "http://sixb.local")
7
+ } catch {
8
+ throw new Error(`[SixbClient] Invalid API base URL '${value}'.`)
9
+ }
10
+
11
+ const pathname = stripTrailingApiPath(url.pathname)
12
+ if (!absolute) {
13
+ return pathname
14
+ }
15
+
16
+ return `${url.origin}${pathname === "/" ? "" : pathname}`
17
+ }
18
+
19
+ function stripTrailingApiPath(pathname: string): string {
20
+ const trimmed = pathname.replace(/\/+$/, "")
21
+ if (!trimmed || trimmed === "/api") {
22
+ return ""
23
+ }
24
+
25
+ if (trimmed.endsWith("/api")) {
26
+ return trimmed.slice(0, -4) || ""
27
+ }
28
+
29
+ return trimmed
30
+ }
package/src/browser.ts CHANGED
@@ -457,7 +457,7 @@ export async function requireSixbBrowserAuthSession(
457
457
  const { data } = await getAuthSession({ throwOnError: true })
458
458
 
459
459
  if (data.authenticated) {
460
- controller.setCsrfToken(data.csrfToken)
460
+ controller.setCsrfToken(data.csrfToken ?? null)
461
461
  return data
462
462
  }
463
463
 
@@ -1,6 +1,8 @@
1
1
  import type { Client } from "./generated/client"
2
+ import type { SixbSessionOptions } from "./session"
2
3
 
3
4
  const sharedAuthorityClients = new WeakSet<Client>()
5
+ const sessionAuthorityClients = new WeakMap<Client, SixbSessionOptions>()
4
6
 
5
7
  export const SHARED_ACCESS_REALTIME_UNAVAILABLE =
6
8
  "Live updates are unavailable during shared access."
@@ -29,3 +31,20 @@ export function markClientSharedAuthority(client: Client, shared: boolean): void
29
31
  export function hasClientSharedAuthority(client: Client): boolean {
30
32
  return sharedAuthorityClients.has(client)
31
33
  }
34
+
35
+ /** Record the native session a client signs in with, so its WebSockets can present it too. */
36
+ export function markClientSessionAuthority(
37
+ client: Client,
38
+ session: SixbSessionOptions | null
39
+ ): void {
40
+ if (session) {
41
+ sessionAuthorityClients.set(client, session)
42
+ return
43
+ }
44
+
45
+ sessionAuthorityClients.delete(client)
46
+ }
47
+
48
+ export function getClientSessionAuthority(client: Client): SixbSessionOptions | null {
49
+ return sessionAuthorityClients.get(client) ?? null
50
+ }
@@ -50,8 +50,8 @@ export interface EventsFilterSpec extends EventSelectorSpec {}
50
50
 
51
51
  // ── Payload typing ──────────────────────────────────────────────────────────
52
52
  // Flat, named conditionals keyed on the channel — never a recursive resolver —
53
- // and `TValueTypes` is always pinned to `readonly []` so property-value
54
- // inference stays shallow enough for TypeScript's recursion limits (no TS2589).
53
+ // so property-value inference stays shallow enough for TypeScript's recursion
54
+ // limits (no TS2589).
55
55
 
56
56
  type Override<TBase, TPatch> = Omit<TBase, keyof TPatch> & TPatch
57
57
 
@@ -103,7 +103,7 @@ type TelemetryEventForProperty<TToken extends PropertyToken | undefined> =
103
103
  {
104
104
  payload: Override<
105
105
  StoredTelemetryEvent["payload"],
106
- { value: InferPropertyValue<TProperty, readonly []> }
106
+ { value: InferPropertyValue<TProperty> }
107
107
  >
108
108
  }
109
109
  >
@@ -116,10 +116,7 @@ type ObjectPropertiesEventOf<
116
116
  > = Override<
117
117
  TEvent,
118
118
  {
119
- payload: Override<
120
- TEvent["payload"],
121
- { properties: InferObjectProperties<TObjectType, readonly []> }
122
- >
119
+ payload: Override<TEvent["payload"], { properties: InferObjectProperties<TObjectType> }>
123
120
  }
124
121
  >
125
122
 
@@ -343,7 +340,6 @@ export interface EventSubscribeExecutor {
343
340
  */
344
341
  export function createWsSubscribeExecutor(options?: { client?: Client }): EventSubscribeExecutor {
345
342
  const transportClient = options?.client ?? generatedClient
346
- const baseUrl = transportClient.getConfig().baseUrl
347
343
  if (hasClientSharedAuthority(transportClient)) {
348
344
  return {
349
345
  subscribe(_filter, _handler, subscribeOptions) {
@@ -375,7 +371,7 @@ export function createWsSubscribeExecutor(options?: { client?: Client }): EventS
375
371
  reconnect: subscribeOptions?.reconnect,
376
372
  reconnectDelayMs: subscribeOptions?.reconnectDelayMs,
377
373
  handshakeTimeoutMs: subscribeOptions?.handshakeTimeoutMs,
378
- baseUrl: typeof baseUrl === "string" ? baseUrl : undefined,
374
+ client: transportClient,
379
375
  onEvent: (event) => {
380
376
  if (matches(event)) handler(event)
381
377
  },
@@ -14,6 +14,7 @@ import {
14
14
  type SixbEventTopic,
15
15
  type SixbEventType,
16
16
  } from "./events-model"
17
+ import type { Client } from "./generated/client"
17
18
  import {
18
19
  createReconnectingSocket,
19
20
  createSixbWebSocketUrl,
@@ -40,8 +41,10 @@ export interface EventSocketOptions {
40
41
  readonly reconnectDelayMs?: number
41
42
  /** Maximum time to complete the event protocol handshake. */
42
43
  readonly handshakeTimeoutMs?: number
43
- /** Override the API base url. Defaults to the global client config. */
44
+ /** Override the API base url. Defaults to `client`'s, then the global client config. */
44
45
  readonly baseUrl?: string
46
+ /** The client whose API and credential the socket uses. Defaults to the package's shared client. */
47
+ readonly client?: Client
45
48
  readonly onEvent: (event: SixbEvent) => void
46
49
  readonly onError?: (error: string) => void
47
50
  readonly onStateChange?: (state: EventSocketState) => void
@@ -81,7 +84,8 @@ export function createEventSocket(options: EventSocketOptions): EventSocket {
81
84
  }
82
85
 
83
86
  return createReconnectingSocket({
84
- url: createSixbEventsWebSocketUrl(options.baseUrl),
87
+ url: createSixbEventsWebSocketUrl(options.baseUrl ?? options.client?.getConfig().baseUrl),
88
+ client: options.client,
85
89
  reconnect: options.reconnect,
86
90
  reconnectDelayMs: options.reconnectDelayMs,
87
91
  readyTimeoutMs: options.handshakeTimeoutMs,
@@ -118,6 +118,7 @@ import {
118
118
  reactivateAuthMember,
119
119
  reauthorizeConnectorConnection,
120
120
  recreateAgentThreadSandbox,
121
+ refreshAuthSession,
121
122
  removeObjectLink,
122
123
  requestAction,
123
124
  requestPipelineRun,
@@ -455,6 +456,9 @@ import type {
455
456
  RecreateAgentThreadSandboxData,
456
457
  RecreateAgentThreadSandboxError,
457
458
  RecreateAgentThreadSandboxResponse,
459
+ RefreshAuthSessionData,
460
+ RefreshAuthSessionError,
461
+ RefreshAuthSessionResponse,
458
462
  RemoveObjectLinkData,
459
463
  RemoveObjectLinkError,
460
464
  RemoveObjectLinkResponse,
@@ -592,6 +596,33 @@ export const exchangeDeviceAuthorizationMutation = (
592
596
  return mutationOptions
593
597
  }
594
598
 
599
+ /**
600
+ * Refresh a native client's session tokens
601
+ */
602
+ export const refreshAuthSessionMutation = (
603
+ options?: Partial<Options<RefreshAuthSessionData>>
604
+ ): UseMutationOptions<
605
+ RefreshAuthSessionResponse,
606
+ RefreshAuthSessionError,
607
+ Options<RefreshAuthSessionData>
608
+ > => {
609
+ const mutationOptions: UseMutationOptions<
610
+ RefreshAuthSessionResponse,
611
+ RefreshAuthSessionError,
612
+ Options<RefreshAuthSessionData>
613
+ > = {
614
+ mutationFn: async (fnOptions) => {
615
+ const { data } = await refreshAuthSession({
616
+ ...options,
617
+ ...fnOptions,
618
+ throwOnError: true,
619
+ })
620
+ return data
621
+ },
622
+ }
623
+ return mutationOptions
624
+ }
625
+
595
626
  export type QueryKey<TOptions extends Options> = [
596
627
  Pick<TOptions, "baseUrl" | "body" | "headers" | "path" | "query"> & {
597
628
  _id: string
@@ -114,6 +114,7 @@ export {
114
114
  reactivateAuthMember,
115
115
  reauthorizeConnectorConnection,
116
116
  recreateAgentThreadSandbox,
117
+ refreshAuthSession,
117
118
  removeObjectLink,
118
119
  requestAction,
119
120
  requestPipelineRun,
@@ -699,6 +700,11 @@ export type {
699
700
  RecreateAgentThreadSandboxErrors,
700
701
  RecreateAgentThreadSandboxResponse,
701
702
  RecreateAgentThreadSandboxResponses,
703
+ RefreshAuthSessionData,
704
+ RefreshAuthSessionError,
705
+ RefreshAuthSessionErrors,
706
+ RefreshAuthSessionResponse,
707
+ RefreshAuthSessionResponses,
702
708
  RemoveObjectLinkData,
703
709
  RemoveObjectLinkError,
704
710
  RemoveObjectLinkErrors,