@sixb/client 0.1.1 → 0.1.2
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 +2 -2
- package/dist/agent-streams.d.ts +26 -3
- package/dist/agent-streams.d.ts.map +1 -1
- package/dist/api.d.ts +6 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/browser.d.ts +1 -1
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +2 -2
- package/dist/browser.js.map +1 -1
- package/dist/{chunk-2vsyqn32.js → chunk-5vp5zr0d.js} +1 -1
- package/dist/{chunk-1mnf48s9.js → chunk-ctf97s25.js} +2 -2
- package/dist/{chunk-w527w02k.js → chunk-mpgzkmwc.js} +2 -74
- package/dist/chunk-mpgzkmwc.js.map +10 -0
- package/dist/{chunk-h8bd95xf.js → chunk-skkt217g.js} +50 -2
- package/dist/chunk-skkt217g.js.map +19 -0
- package/dist/{chunk-hkrfakyt.js → chunk-tsdnw994.js} +2 -2
- package/dist/{chunk-dkd2wgva.js → chunk-v4jdc1nb.js} +40 -28
- package/dist/{chunk-dkd2wgva.js.map → chunk-v4jdc1nb.js.map} +3 -3
- package/dist/{chunk-6w5w9e75.js → chunk-zfne7wxr.js} +27 -12
- package/dist/chunk-zfne7wxr.js.map +10 -0
- package/dist/client-provider.d.ts +8 -0
- package/dist/client-provider.d.ts.map +1 -0
- package/dist/connectors/connection.d.ts +50 -0
- package/dist/connectors/connection.d.ts.map +1 -0
- package/dist/connectors/index.d.ts +4 -0
- package/dist/connectors/index.d.ts.map +1 -0
- package/dist/connectors/mutations.d.ts +62 -0
- package/dist/connectors/mutations.d.ts.map +1 -0
- package/dist/connectors/queries.d.ts +536 -0
- package/dist/connectors/queries.d.ts.map +1 -0
- package/dist/generated/@tanstack/react-query.gen.d.ts +3508 -735
- package/dist/generated/@tanstack/react-query.gen.d.ts.map +1 -1
- package/dist/generated/index.d.ts +2 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/sdk.gen.d.ts +33 -1
- package/dist/generated/sdk.gen.d.ts.map +1 -1
- package/dist/generated/types.gen.d.ts +1819 -198
- package/dist/generated/types.gen.d.ts.map +1 -1
- package/dist/hooks.d.ts +2 -0
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +1118 -351
- package/dist/hooks.js.map +11 -6
- package/dist/index.js +25 -11
- package/dist/index.js.map +1 -1
- package/dist/logs.js +3 -3
- package/dist/models.d.ts +0 -31
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +19 -7
- package/dist/models.js.map +1 -1
- package/dist/query-hooks.d.ts +2 -6
- package/dist/query-hooks.d.ts.map +1 -1
- package/dist/query.js +3 -3
- package/dist/useAgentActivityStream.d.ts +14 -0
- package/dist/useAgentActivityStream.d.ts.map +1 -0
- package/package.json +6 -2
- package/src/agent-streams.ts +68 -45
- package/src/api.ts +26 -2
- package/src/browser.ts +1 -1
- package/src/client-provider.ts +12 -0
- package/src/connectors/connection.ts +443 -0
- package/src/connectors/index.ts +12 -0
- package/src/connectors/mutations.ts +417 -0
- package/src/connectors/queries.ts +150 -0
- package/src/generated/@tanstack/react-query.gen.ts +245 -0
- package/src/generated/index.ts +48 -0
- package/src/generated/sdk.gen.ts +160 -0
- package/src/generated/types.gen.ts +2417 -230
- package/src/hooks.ts +6 -0
- package/src/models.ts +0 -103
- package/src/query-hooks.ts +8 -14
- package/src/useAgentActivityStream.ts +48 -0
- package/dist/chunk-6w5w9e75.js.map +0 -10
- package/dist/chunk-h8bd95xf.js.map +0 -19
- package/dist/chunk-w527w02k.js.map +0 -10
- /package/dist/{chunk-2vsyqn32.js.map → chunk-5vp5zr0d.js.map} +0 -0
- /package/dist/{chunk-1mnf48s9.js.map → chunk-ctf97s25.js.map} +0 -0
- /package/dist/{chunk-hkrfakyt.js.map → chunk-tsdnw994.js.map} +0 -0
package/src/agent-streams.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { AgentRunStreamEvent } from "@sixb/core/agents/streams"
|
|
2
|
-
// Import the
|
|
3
|
-
//
|
|
4
|
-
import {
|
|
1
|
+
import type { AgentRunActivityEvent, AgentRunStreamEvent } from "@sixb/core/agents/streams"
|
|
2
|
+
// Import the wire validator from the browser-safe streams subpath: the `@sixb/core` root pulls in
|
|
3
|
+
// node-only runtime (e.g. `node:crypto`), which breaks the Atlas browser bundle.
|
|
4
|
+
import { isAgentRunActivityEvent, isAgentRunStreamEvent } from "@sixb/core/agents/streams"
|
|
5
5
|
import type { BrokerRecord } from "@sixb/core/broker"
|
|
6
6
|
import type { GetAgentRunResponses } from "./generated/types.gen"
|
|
7
7
|
import {
|
|
@@ -11,7 +11,11 @@ import {
|
|
|
11
11
|
type ReconnectingSocketState,
|
|
12
12
|
} from "./ws-socket"
|
|
13
13
|
|
|
14
|
-
export type {
|
|
14
|
+
export type {
|
|
15
|
+
AgentRunActivityEvent,
|
|
16
|
+
AgentRunFailure,
|
|
17
|
+
AgentRunStreamEvent,
|
|
18
|
+
} from "@sixb/core/agents/streams"
|
|
15
19
|
export type { ReconnectingSocket, ReconnectingSocketState } from "./ws-socket"
|
|
16
20
|
|
|
17
21
|
export type AgentRunSnapshot = GetAgentRunResponses[200]
|
|
@@ -38,10 +42,15 @@ export interface AgentRunStreamUnsubscribeMessage {
|
|
|
38
42
|
readonly runId?: string
|
|
39
43
|
}
|
|
40
44
|
|
|
45
|
+
export interface AgentActivityStreamSubscribeMessage {
|
|
46
|
+
readonly type: "subscribe.activity"
|
|
47
|
+
}
|
|
48
|
+
|
|
41
49
|
export type AgentRunStreamClientMessage =
|
|
42
50
|
| AgentRunStreamSubscribeMessage
|
|
43
51
|
| AgentRunStreamReplayMessage
|
|
44
52
|
| AgentRunStreamUnsubscribeMessage
|
|
53
|
+
| AgentActivityStreamSubscribeMessage
|
|
45
54
|
|
|
46
55
|
export type AgentRunStreamServerMessage =
|
|
47
56
|
| { readonly type: "connected"; readonly channel?: string }
|
|
@@ -53,6 +62,9 @@ export type AgentRunStreamServerMessage =
|
|
|
53
62
|
readonly count: number
|
|
54
63
|
}
|
|
55
64
|
| { readonly type: "unsubscribed"; readonly runId?: string | null }
|
|
65
|
+
| { readonly type: "subscribed.activity" }
|
|
66
|
+
| { readonly type: "unsubscribed.activity" }
|
|
67
|
+
| { readonly type: "activity"; readonly event: AgentRunActivityEvent }
|
|
56
68
|
| { readonly type: "error"; readonly message: string }
|
|
57
69
|
| { readonly type: "run.snapshot"; readonly run: AgentRunSnapshot }
|
|
58
70
|
| { readonly type: "record"; readonly record: AgentRunStreamRecord }
|
|
@@ -74,6 +86,18 @@ export interface AgentRunSocketOptions {
|
|
|
74
86
|
readonly onStateChange?: (state: ReconnectingSocketState) => void
|
|
75
87
|
}
|
|
76
88
|
|
|
89
|
+
export interface AgentActivitySocketOptions {
|
|
90
|
+
readonly reconnect?: boolean
|
|
91
|
+
readonly reconnectDelayMs?: number
|
|
92
|
+
/** Override the API base url. Defaults to the global client config. */
|
|
93
|
+
readonly baseUrl?: string
|
|
94
|
+
readonly onActivity: (event: AgentRunActivityEvent) => void
|
|
95
|
+
/** Called after each initial or reconnect subscription, so durable state can be reconciled. */
|
|
96
|
+
readonly onSubscribed?: () => void
|
|
97
|
+
readonly onError?: (message: string) => void
|
|
98
|
+
readonly onStateChange?: (state: ReconnectingSocketState) => void
|
|
99
|
+
}
|
|
100
|
+
|
|
77
101
|
/**
|
|
78
102
|
* Open a subscribing WebSocket to `/ws/agents` for one run and stream its records to `onEvent`.
|
|
79
103
|
* Tracks the latest cursor across reconnects so no record is replayed. React-free: the
|
|
@@ -117,6 +141,33 @@ export function createAgentRunSocket(options: AgentRunSocketOptions): Reconnecti
|
|
|
117
141
|
})
|
|
118
142
|
}
|
|
119
143
|
|
|
144
|
+
/** Open one project-level lifecycle feed, independent of how many Agent threads are running. */
|
|
145
|
+
export function createAgentActivitySocket(options: AgentActivitySocketOptions): ReconnectingSocket {
|
|
146
|
+
return createReconnectingSocket({
|
|
147
|
+
url: createSixbAgentsWebSocketUrl(options.baseUrl),
|
|
148
|
+
reconnect: options.reconnect,
|
|
149
|
+
reconnectDelayMs: options.reconnectDelayMs,
|
|
150
|
+
connectionErrorMessage: "Agent activity websocket connection failed.",
|
|
151
|
+
onError: options.onError,
|
|
152
|
+
onStateChange: options.onStateChange,
|
|
153
|
+
subscribeMessage: () =>
|
|
154
|
+
({ type: "subscribe.activity" }) satisfies AgentActivityStreamSubscribeMessage,
|
|
155
|
+
onMessage: (data, sink) => {
|
|
156
|
+
const message = parseAgentRunStreamServerMessage(data)
|
|
157
|
+
if (!message) return
|
|
158
|
+
if (message.type === "activity") {
|
|
159
|
+
options.onActivity(message.event)
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
if (message.type === "subscribed.activity") {
|
|
163
|
+
options.onSubscribed?.()
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
if (message.type === "error") sink.reportError(message.message)
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
120
171
|
export function parseAgentRunStreamServerMessage(
|
|
121
172
|
value: unknown
|
|
122
173
|
): AgentRunStreamServerMessage | null {
|
|
@@ -147,6 +198,14 @@ export function parseAgentRunStreamServerMessage(
|
|
|
147
198
|
return { type: "error", message: String(parsed.message ?? "Agent stream error.") }
|
|
148
199
|
}
|
|
149
200
|
|
|
201
|
+
if (parsed.type === "activity" && isAgentRunActivityEvent(parsed.event)) {
|
|
202
|
+
return { type: "activity", event: parsed.event }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (parsed.type === "subscribed.activity" || parsed.type === "unsubscribed.activity") {
|
|
206
|
+
return { type: parsed.type }
|
|
207
|
+
}
|
|
208
|
+
|
|
150
209
|
if (parsed.type === "connected") {
|
|
151
210
|
return {
|
|
152
211
|
type: "connected",
|
|
@@ -197,9 +256,10 @@ function isAgentRunSnapshot(value: unknown): value is AgentRunSnapshot {
|
|
|
197
256
|
typeof value.threadId === "string" &&
|
|
198
257
|
typeof value.agentId === "string" &&
|
|
199
258
|
typeof value.triggerMessageId === "string" &&
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
259
|
+
(value.requestedBy === undefined ||
|
|
260
|
+
(isRecord(value.requestedBy) &&
|
|
261
|
+
typeof value.requestedBy.type === "string" &&
|
|
262
|
+
typeof value.requestedBy.id === "string")) &&
|
|
203
263
|
isDurableAgentRunStatus(value.status) &&
|
|
204
264
|
isFiniteNumber(value.attempt) &&
|
|
205
265
|
typeof value.streamId === "string" &&
|
|
@@ -219,39 +279,6 @@ function isAgentRunStreamRecord(value: unknown): value is AgentRunStreamRecord {
|
|
|
219
279
|
)
|
|
220
280
|
}
|
|
221
281
|
|
|
222
|
-
function isAgentRunStreamEvent(value: unknown): value is AgentRunStreamEvent {
|
|
223
|
-
if (
|
|
224
|
-
!isRecord(value) ||
|
|
225
|
-
value.schemaVersion !== AGENT_RUN_STREAM_SCHEMA_VERSION ||
|
|
226
|
-
typeof value.projectId !== "string" ||
|
|
227
|
-
typeof value.runId !== "string" ||
|
|
228
|
-
typeof value.threadId !== "string" ||
|
|
229
|
-
typeof value.agentId !== "string" ||
|
|
230
|
-
!isFiniteNumber(value.attempt) ||
|
|
231
|
-
typeof value.occurredAt !== "string" ||
|
|
232
|
-
typeof value.type !== "string"
|
|
233
|
-
) {
|
|
234
|
-
return false
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
switch (value.type) {
|
|
238
|
-
case "agent.run.started":
|
|
239
|
-
return value.modelId === undefined || typeof value.modelId === "string"
|
|
240
|
-
case "agent.ui.chunk":
|
|
241
|
-
return Number.isInteger(value.chunkIndex) && Object.hasOwn(value, "chunk")
|
|
242
|
-
case "agent.message.finalized":
|
|
243
|
-
return typeof value.messageId === "string"
|
|
244
|
-
case "agent.run.finished":
|
|
245
|
-
return (
|
|
246
|
-
isAgentRunStatus(value.status) &&
|
|
247
|
-
(value.finishReason === undefined || typeof value.finishReason === "string") &&
|
|
248
|
-
(value.error === undefined || typeof value.error === "string")
|
|
249
|
-
)
|
|
250
|
-
default:
|
|
251
|
-
return false
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
282
|
function isDurableAgentRunStatus(value: unknown): value is AgentRunSnapshot["status"] {
|
|
256
283
|
return (
|
|
257
284
|
value === "queued" ||
|
|
@@ -262,10 +289,6 @@ function isDurableAgentRunStatus(value: unknown): value is AgentRunSnapshot["sta
|
|
|
262
289
|
)
|
|
263
290
|
}
|
|
264
291
|
|
|
265
|
-
function isAgentRunStatus(value: unknown): value is "succeeded" | "failed" | "cancelled" {
|
|
266
|
-
return value === "succeeded" || value === "failed" || value === "cancelled"
|
|
267
|
-
}
|
|
268
|
-
|
|
269
292
|
function isFiniteNumber(value: unknown): value is number {
|
|
270
293
|
return typeof value === "number" && Number.isFinite(value)
|
|
271
294
|
}
|
package/src/api.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { SixbErrorCode } from "@sixb/core"
|
|
1
2
|
import { type Auth, type Client, type Config, createClient, createConfig } from "./generated/client"
|
|
3
|
+
import { client as sharedClient } from "./generated/client.gen"
|
|
2
4
|
|
|
3
5
|
export const SIXB_CSRF_HEADER_NAME = "x-sixb-csrf"
|
|
4
6
|
export const SIXB_CSRF_TOKEN_RESPONSE_HEADER_NAME = "x-sixb-csrf-token"
|
|
@@ -34,13 +36,16 @@ export interface SixbApiErrorInit {
|
|
|
34
36
|
readonly body?: unknown
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
/** Known Sixb codes with a forward-compatible escape hatch for newer servers. */
|
|
40
|
+
export type SixbApiErrorCode = SixbErrorCode | (string & Record<never, never>)
|
|
41
|
+
|
|
37
42
|
/**
|
|
38
43
|
* A structured HTTP error thrown by the Sixb client. The raw generated client
|
|
39
44
|
* throws the bare response body (a string, or parsed JSON) with no status code
|
|
40
45
|
* or identity, which makes failures impossible to branch on and prints as
|
|
41
46
|
* cryptic quoted text in dev tooling. `SixbApiError` carries the status, the
|
|
42
|
-
* parsed `body`, and a readable `message`, so callers
|
|
43
|
-
*
|
|
47
|
+
* parsed `body`, an optional stable `code`, and a readable `message`, so callers
|
|
48
|
+
* and error boundaries can branch without parsing prose.
|
|
44
49
|
*/
|
|
45
50
|
export class SixbApiError extends Error {
|
|
46
51
|
readonly status: number
|
|
@@ -48,6 +53,7 @@ export class SixbApiError extends Error {
|
|
|
48
53
|
readonly url: string
|
|
49
54
|
readonly method: string
|
|
50
55
|
readonly body: unknown
|
|
56
|
+
readonly code?: SixbApiErrorCode
|
|
51
57
|
|
|
52
58
|
constructor(message: string, init: SixbApiErrorInit) {
|
|
53
59
|
super(message)
|
|
@@ -57,6 +63,7 @@ export class SixbApiError extends Error {
|
|
|
57
63
|
this.url = init.url ?? ""
|
|
58
64
|
this.method = init.method ?? ""
|
|
59
65
|
this.body = init.body
|
|
66
|
+
this.code = extractErrorCode(init.body)
|
|
60
67
|
}
|
|
61
68
|
}
|
|
62
69
|
|
|
@@ -214,6 +221,19 @@ function extractErrorDetail(body: unknown): string | undefined {
|
|
|
214
221
|
return undefined
|
|
215
222
|
}
|
|
216
223
|
|
|
224
|
+
function extractErrorCode(body: unknown): SixbApiErrorCode | undefined {
|
|
225
|
+
if (!body || typeof body !== "object") {
|
|
226
|
+
return undefined
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
const code = Reflect.get(body, "code")
|
|
231
|
+
return typeof code === "string" && code.trim() === code && code.length > 0 ? code : undefined
|
|
232
|
+
} catch {
|
|
233
|
+
return undefined
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
217
237
|
function safeRequestPath(url: string): string {
|
|
218
238
|
try {
|
|
219
239
|
return new URL(url).pathname
|
|
@@ -227,3 +247,7 @@ function installSixbErrorInterceptor(client: SixbClient): void {
|
|
|
227
247
|
client.interceptors.error.use(sixbErrorInterceptor)
|
|
228
248
|
}
|
|
229
249
|
}
|
|
250
|
+
|
|
251
|
+
// Package-level SDK functions use the generated singleton. Initialize it once at the transport
|
|
252
|
+
// boundary so the shared client and `createSixbClient()` expose the same HTTP error contract.
|
|
253
|
+
installSixbErrorInterceptor(sharedClient)
|
package/src/browser.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "./api"
|
|
7
7
|
import { client } from "./generated/client.gen"
|
|
8
8
|
|
|
9
|
-
export type { SixbApiErrorInit } from "./api"
|
|
9
|
+
export type { SixbApiErrorCode, SixbApiErrorInit } from "./api"
|
|
10
10
|
export { isSixbApiError, SixbApiError } from "./api"
|
|
11
11
|
|
|
12
12
|
import { getAuthSession } from "./generated/sdk.gen"
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, createElement, type ReactNode, useContext } from "react"
|
|
2
|
+
import type { Client } from "./generated/client"
|
|
3
|
+
|
|
4
|
+
const SixbClientContext = createContext<Client | undefined>(undefined)
|
|
5
|
+
|
|
6
|
+
export function SixbProvider(props: { client: Client; children?: ReactNode }) {
|
|
7
|
+
return createElement(SixbClientContext.Provider, { value: props.client }, props.children)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function useSixbProviderClient(): Client | undefined {
|
|
11
|
+
return useContext(SixbClientContext)
|
|
12
|
+
}
|