doover-js 0.4.1 → 0.4.3

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.
@@ -6,7 +6,7 @@ export { useAgentConnections, agentConnectionsQueryKey, } from "./useAgentConnec
6
6
  export { useChannelSubscription, } from "./useChannelSubscription";
7
7
  export type { ChannelSubscriptionHandlers } from "./useChannelSubscription";
8
8
  export { useChannelAggregate, channelAggregateQueryKey, } from "./useChannelAggregate";
9
- export type { UseChannelAggregateResult } from "./useChannelAggregate";
9
+ export type { UseChannelAggregateOptions, UseChannelAggregateResult, } from "./useChannelAggregate";
10
10
  export { useAgentChannel } from "./useAgentChannel";
11
11
  export { useSendMessage } from "./useSendMessage";
12
12
  export { useUpdateAggregate } from "./useUpdateAggregate";
@@ -15,6 +15,8 @@ export { useUpdateMessage } from "./useUpdateMessage";
15
15
  export type { UpdateMessageVariables, UseUpdateMessageOptions, } from "./useUpdateMessage";
16
16
  export { useChannelMessages, channelMessagesQueryKey, } from "./useChannelMessages";
17
17
  export type { UseChannelMessagesOptions, UseChannelMessagesResult, } from "./useChannelMessages";
18
+ export { useChannelMessage, channelMessageQueryKey, } from "./useChannelMessage";
19
+ export type { UseChannelMessageOptions, UseChannelMessageResult, } from "./useChannelMessage";
18
20
  export { useSendRpc } from "./useSendRpc";
19
21
  export type { RpcCommandId, RpcStatusEvent, SendRpcVariables, UseSendRpcOptions, UseSendRpcResult, } from "./useSendRpc";
20
22
  export { useMultiAgentAggregates, multiAgentAggregatesQueryKey, } from "./useMultiAgentAggregates";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resetSharedQueryClient = exports.getSharedQueryClient = exports.useTurnCredentials = exports.multiAgentChannelMessagesQueryKey = exports.useMultiAgentChannelMessages = exports.multiAgentAggregatesQueryKey = exports.useMultiAgentAggregates = exports.useSendRpc = exports.channelMessagesQueryKey = exports.useChannelMessages = exports.useUpdateMessage = exports.useUpdateAggregate = exports.useSendMessage = exports.useAgentChannel = exports.channelAggregateQueryKey = exports.useChannelAggregate = exports.useChannelSubscription = exports.agentConnectionsQueryKey = exports.useAgentConnections = exports.useConnectionState = exports.useDooverClient = exports.DooverProvider = void 0;
3
+ exports.resetSharedQueryClient = exports.getSharedQueryClient = exports.useTurnCredentials = exports.multiAgentChannelMessagesQueryKey = exports.useMultiAgentChannelMessages = exports.multiAgentAggregatesQueryKey = exports.useMultiAgentAggregates = exports.useSendRpc = exports.channelMessageQueryKey = exports.useChannelMessage = exports.channelMessagesQueryKey = exports.useChannelMessages = exports.useUpdateMessage = exports.useUpdateAggregate = exports.useSendMessage = exports.useAgentChannel = exports.channelAggregateQueryKey = exports.useChannelAggregate = exports.useChannelSubscription = exports.agentConnectionsQueryKey = exports.useAgentConnections = exports.useConnectionState = exports.useDooverClient = exports.DooverProvider = void 0;
4
4
  var context_1 = require("./context");
5
5
  Object.defineProperty(exports, "DooverProvider", { enumerable: true, get: function () { return context_1.DooverProvider; } });
6
6
  Object.defineProperty(exports, "useDooverClient", { enumerable: true, get: function () { return context_1.useDooverClient; } });
@@ -25,6 +25,9 @@ Object.defineProperty(exports, "useUpdateMessage", { enumerable: true, get: func
25
25
  var useChannelMessages_1 = require("./useChannelMessages");
26
26
  Object.defineProperty(exports, "useChannelMessages", { enumerable: true, get: function () { return useChannelMessages_1.useChannelMessages; } });
27
27
  Object.defineProperty(exports, "channelMessagesQueryKey", { enumerable: true, get: function () { return useChannelMessages_1.channelMessagesQueryKey; } });
28
+ var useChannelMessage_1 = require("./useChannelMessage");
29
+ Object.defineProperty(exports, "useChannelMessage", { enumerable: true, get: function () { return useChannelMessage_1.useChannelMessage; } });
30
+ Object.defineProperty(exports, "channelMessageQueryKey", { enumerable: true, get: function () { return useChannelMessage_1.channelMessageQueryKey; } });
28
31
  var useSendRpc_1 = require("./useSendRpc");
29
32
  Object.defineProperty(exports, "useSendRpc", { enumerable: true, get: function () { return useSendRpc_1.useSendRpc; } });
30
33
  var useMultiAgentAggregates_1 = require("./useMultiAgentAggregates");
@@ -1,7 +1,8 @@
1
1
  import type { Aggregate } from "../types/common";
2
+ import { type UseChannelAggregateOptions } from "./useChannelAggregate";
2
3
  /**
3
4
  * Convenience wrapper around `useChannelAggregate` that accepts an agent id
4
5
  * and channel name as separate arguments — the most common shape at call
5
6
  * sites where the identifier isn't already a `ChannelIdentifier`.
6
7
  */
7
- export declare function useAgentChannel<TData = Aggregate["data"]>(agentId: string | undefined, channelName: string): import("./useChannelAggregate").UseChannelAggregateResult<TData>;
8
+ export declare function useAgentChannel<TData = Aggregate["data"]>(agentId: string | undefined, channelName: string, options?: UseChannelAggregateOptions): import("./useChannelAggregate").UseChannelAggregateResult<TData>;
@@ -7,6 +7,6 @@ const useChannelAggregate_1 = require("./useChannelAggregate");
7
7
  * and channel name as separate arguments — the most common shape at call
8
8
  * sites where the identifier isn't already a `ChannelIdentifier`.
9
9
  */
10
- function useAgentChannel(agentId, channelName) {
11
- return (0, useChannelAggregate_1.useChannelAggregate)({ agentId, channelName });
10
+ function useAgentChannel(agentId, channelName, options) {
11
+ return (0, useChannelAggregate_1.useChannelAggregate)({ agentId, channelName }, options);
12
12
  }
@@ -19,6 +19,22 @@ export interface UseChannelAggregateResult<TData> extends Omit<UseQueryResult<Ag
19
19
  /** Server timestamp (epoch seconds) of the last aggregate update. */
20
20
  last_updated: number | null | undefined;
21
21
  }
22
+ export interface UseChannelAggregateOptions {
23
+ /**
24
+ * Whether to fetch the initial aggregate over REST on mount.
25
+ *
26
+ * Defaults to `true` — the hook does a one-shot HTTP GET to seed the cache
27
+ * before the gateway WebSocket has connected, so the first paint isn't
28
+ * empty.
29
+ *
30
+ * Set to `false` for purely WS-driven consumers that don't want the REST
31
+ * round-trip (e.g. when the gateway's upsert-on-subscribe semantics make
32
+ * the REST call redundant or actively wrong, like channels that don't yet
33
+ * exist). The cache is then populated only by `ChannelSync` /
34
+ * `AggregateUpdate` events.
35
+ */
36
+ fetchInitial?: boolean;
37
+ }
22
38
  /**
23
39
  * Fetch a channel's aggregate and keep it live via the gateway. Incoming
24
40
  * `channelSync` and `aggregateUpdate` events patch the react-query cache
@@ -30,4 +46,4 @@ export interface UseChannelAggregateResult<TData> extends Omit<UseQueryResult<Ag
30
46
  * don't have the implicit index signature that `Aggregate["data"]` carries
31
47
  * and enforcing compatibility there just forces `as` casts at call sites.
32
48
  */
33
- export declare function useChannelAggregate<TData = Aggregate["data"]>(identifier: ChannelIdentifier): UseChannelAggregateResult<TData>;
49
+ export declare function useChannelAggregate<TData = Aggregate["data"]>(identifier: ChannelIdentifier, options?: UseChannelAggregateOptions): UseChannelAggregateResult<TData>;
@@ -4,6 +4,7 @@ exports.channelAggregateQueryKey = channelAggregateQueryKey;
4
4
  exports.useChannelAggregate = useChannelAggregate;
5
5
  const react_1 = require("react");
6
6
  const react_query_1 = require("@tanstack/react-query");
7
+ const errors_1 = require("../http/errors");
7
8
  const context_1 = require("./context");
8
9
  const useChannelSubscription_1 = require("./useChannelSubscription");
9
10
  function channelAggregateQueryKey(agentId, channelName) {
@@ -20,11 +21,12 @@ function channelAggregateQueryKey(agentId, channelName) {
20
21
  * don't have the implicit index signature that `Aggregate["data"]` carries
21
22
  * and enforcing compatibility there just forces `as` casts at call sites.
22
23
  */
23
- function useChannelAggregate(identifier) {
24
+ function useChannelAggregate(identifier, options) {
24
25
  const client = (0, context_1.useDooverClient)();
25
26
  const queryClient = (0, react_query_1.useQueryClient)();
26
27
  const { agentId, channelName } = identifier;
27
28
  const key = channelAggregateQueryKey(agentId, channelName);
29
+ const fetchInitial = options?.fetchInitial ?? true;
28
30
  const onAggregate = (0, react_1.useCallback)((aggregate) => {
29
31
  queryClient.setQueryData(key, aggregate);
30
32
  },
@@ -34,9 +36,18 @@ function useChannelAggregate(identifier) {
34
36
  (0, useChannelSubscription_1.useChannelSubscription)(identifier, { onAggregate });
35
37
  const query = (0, react_query_1.useQuery)({
36
38
  queryKey: key,
37
- enabled: !!agentId && !!channelName,
39
+ enabled: fetchInitial && !!agentId && !!channelName,
38
40
  staleTime: Infinity,
39
41
  queryFn: () => client.viewer.getAggregate(identifier),
42
+ // A 404 means the aggregate doesn't exist — retrying won't change that,
43
+ // and the caller needs the error promptly to render an empty/"not
44
+ // installed" state. Fall back to the react-query default (3 retries) for
45
+ // every other failure.
46
+ retry: (failureCount, error) => {
47
+ if (error instanceof errors_1.DooverApiError && error.status === 404)
48
+ return false;
49
+ return failureCount < 3;
50
+ },
40
51
  });
41
52
  const aggregate = query.data;
42
53
  const { data: _ignored, ...rest } = query;
@@ -0,0 +1,38 @@
1
+ import { type UseQueryResult } from "@tanstack/react-query";
2
+ import type { JSONValue, MessageAttachment, MessageStructure } from "../types/common";
3
+ import type { ChannelIdentifier } from "../types/viewer";
4
+ export declare function channelMessageQueryKey(agentId: string | undefined, channelName: string | undefined, messageId: string | undefined): readonly ["doover", "agent", string | undefined, "channel", string | undefined, "message", string | undefined];
5
+ export interface UseChannelMessageOptions {
6
+ /** If false, skip subscribing for live message-update events. Defaults true. */
7
+ liveUpdates?: boolean;
8
+ }
9
+ /**
10
+ * Shape returned by `useChannelMessage`. Mirrors `UseChannelAggregateResult`:
11
+ * `data` is unwrapped to the message payload (`message.data`) so the common
12
+ * `const { data } = useChannelMessage(…)` pattern works without a secondary
13
+ * access. The full `MessageStructure` and a couple of useful slots
14
+ * (`attachments`, `timestamp`) are hoisted alongside.
15
+ */
16
+ export interface UseChannelMessageResult<TData> extends Omit<UseQueryResult<MessageStructure<TData> | undefined>, "data"> {
17
+ /** Unwrapped message payload (`message.data`). Undefined until first fetch. */
18
+ data: TData | undefined;
19
+ /** Full message structure, or undefined until first fetch. */
20
+ message: MessageStructure<TData> | undefined;
21
+ attachments: MessageAttachment[] | undefined;
22
+ /** Server timestamp reported by the message. */
23
+ timestamp: number | undefined;
24
+ }
25
+ /**
26
+ * Fetch a single channel message by id and keep it live via gateway
27
+ * `MessageUpdate` events. Mirrors `useChannelAggregate`'s pattern: REST seeds
28
+ * the cache, then `onMessageUpdate` patches it whenever the message body is
29
+ * mutated server-side — handy for watching long-running RPC-style messages
30
+ * where the receiver writes progress into a `response` field.
31
+ *
32
+ * Channel subscriptions deliver every message in the channel, so the
33
+ * handlers filter to the requested `messageId` before patching the cache.
34
+ *
35
+ * Typed on `TData` — the shape of `message.data`. Defaults to `JSONValue` to
36
+ * match `MessageStructure`.
37
+ */
38
+ export declare function useChannelMessage<TData = JSONValue>(identifier: ChannelIdentifier, messageId: string | undefined, options?: UseChannelMessageOptions): UseChannelMessageResult<TData>;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.channelMessageQueryKey = channelMessageQueryKey;
4
+ exports.useChannelMessage = useChannelMessage;
5
+ const react_1 = require("react");
6
+ const react_query_1 = require("@tanstack/react-query");
7
+ const context_1 = require("./context");
8
+ const useChannelSubscription_1 = require("./useChannelSubscription");
9
+ function channelMessageQueryKey(agentId, channelName, messageId) {
10
+ return [
11
+ "doover",
12
+ "agent",
13
+ agentId,
14
+ "channel",
15
+ channelName,
16
+ "message",
17
+ messageId,
18
+ ];
19
+ }
20
+ /**
21
+ * Fetch a single channel message by id and keep it live via gateway
22
+ * `MessageUpdate` events. Mirrors `useChannelAggregate`'s pattern: REST seeds
23
+ * the cache, then `onMessageUpdate` patches it whenever the message body is
24
+ * mutated server-side — handy for watching long-running RPC-style messages
25
+ * where the receiver writes progress into a `response` field.
26
+ *
27
+ * Channel subscriptions deliver every message in the channel, so the
28
+ * handlers filter to the requested `messageId` before patching the cache.
29
+ *
30
+ * Typed on `TData` — the shape of `message.data`. Defaults to `JSONValue` to
31
+ * match `MessageStructure`.
32
+ */
33
+ function useChannelMessage(identifier, messageId, options) {
34
+ const client = (0, context_1.useDooverClient)();
35
+ const queryClient = (0, react_query_1.useQueryClient)();
36
+ const { agentId, channelName } = identifier;
37
+ const liveUpdates = options?.liveUpdates ?? true;
38
+ const key = channelMessageQueryKey(agentId, channelName, messageId);
39
+ const onMessageUpdate = (0, react_1.useCallback)((message) => {
40
+ if (message.id !== messageId)
41
+ return;
42
+ queryClient.setQueryData(key, message);
43
+ },
44
+ // The key array is structurally stable per (agentId, channelName, messageId).
45
+ // eslint-disable-next-line react-hooks/exhaustive-deps
46
+ [queryClient, agentId, channelName, messageId]);
47
+ // Cover the case where the caller has the id before the create event has
48
+ // landed on this client — patch the cache when it shows up.
49
+ const onMessage = (0, react_1.useCallback)((message) => {
50
+ if (message.id !== messageId)
51
+ return;
52
+ queryClient.setQueryData(key, message);
53
+ },
54
+ // eslint-disable-next-line react-hooks/exhaustive-deps
55
+ [queryClient, agentId, channelName, messageId]);
56
+ (0, useChannelSubscription_1.useChannelSubscription)(liveUpdates && messageId ? identifier : undefined, { onMessage, onMessageUpdate });
57
+ const query = (0, react_query_1.useQuery)({
58
+ queryKey: key,
59
+ enabled: !!agentId && !!channelName && !!messageId,
60
+ staleTime: Infinity,
61
+ queryFn: () => client.messages.getMessage(agentId, channelName, messageId),
62
+ });
63
+ const message = query.data;
64
+ const { data: _ignored, ...rest } = query;
65
+ return {
66
+ ...rest,
67
+ data: message?.data,
68
+ message,
69
+ attachments: message?.attachments,
70
+ timestamp: message?.timestamp,
71
+ };
72
+ }
@@ -270,6 +270,72 @@ function wrapper(client) {
270
270
  liveId,
271
271
  ]));
272
272
  });
273
+ (0, mocha_1.it)("useChannelMessage seeds via REST and patches on MessageUpdate", async () => {
274
+ const messageId = (0, snowflake_1.generateSnowflakeIdAtTime)(new Date("2026-01-01T00:00:00.000Z"));
275
+ const otherId = (0, snowflake_1.generateSnowflakeIdAtTime)(new Date("2026-01-01T00:00:01.000Z"));
276
+ const fetchMock = (0, helpers_1.createFetchMock)(() => (0, helpers_1.createJsonResponse)({
277
+ id: messageId,
278
+ author_id: "u1",
279
+ channel: { agent_id: "a1", name: "rpc" },
280
+ data: { method: "ping", response: { status: "pulling" } },
281
+ attachments: [],
282
+ }));
283
+ const client = new doover_client_1.DooverClient({
284
+ dataRestUrl: "https://api.example.com",
285
+ controlApiUrl: "https://control.example.com",
286
+ dataWssUrl: "wss://ws.example.com",
287
+ fetchImpl: fetchMock,
288
+ webSocketImpl: helpers_1.MockWebSocket,
289
+ disableBrowserLifecycleHooks: true,
290
+ });
291
+ const { result } = (0, react_1.renderHook)(() => (0, react_2.useChannelMessage)({ agentId: "a1", channelName: "rpc" }, messageId), { wrapper: wrapper(client) });
292
+ await (0, react_1.waitFor)(() => (0, chai_1.expect)(result.current.isSuccess).to.equal(true));
293
+ (0, chai_1.expect)(result.current.data?.response?.status).to.equal("pulling");
294
+ (0, chai_1.expect)(result.current.message?.id).to.equal(messageId);
295
+ await (0, react_1.act)(async () => {
296
+ const ws = helpers_1.MockWebSocket.instances[0];
297
+ ws.open();
298
+ ws.receive({ op: 0, t: "Hello", d: {} });
299
+ ws.receive({
300
+ op: 0,
301
+ t: "Ready",
302
+ d: { session_id: "s1", session_token: "t1", subscriptions: [] },
303
+ });
304
+ // Update for an unrelated message — should be ignored.
305
+ ws.receive({
306
+ op: 0,
307
+ t: "MessageUpdate",
308
+ d: {
309
+ channel: { agent_id: "a1", name: "rpc" },
310
+ author_id: "u1",
311
+ message: {
312
+ id: otherId,
313
+ author_id: "u1",
314
+ channel: { agent_id: "a1", name: "rpc" },
315
+ data: { method: "ping", response: { status: "success" } },
316
+ attachments: [],
317
+ },
318
+ },
319
+ });
320
+ // Update for the message we're watching — should patch.
321
+ ws.receive({
322
+ op: 0,
323
+ t: "MessageUpdate",
324
+ d: {
325
+ channel: { agent_id: "a1", name: "rpc" },
326
+ author_id: "u1",
327
+ message: {
328
+ id: messageId,
329
+ author_id: "u1",
330
+ channel: { agent_id: "a1", name: "rpc" },
331
+ data: { method: "ping", response: { status: "success" } },
332
+ attachments: [],
333
+ },
334
+ },
335
+ });
336
+ });
337
+ await (0, react_1.waitFor)(() => (0, chai_1.expect)(result.current.data?.response?.status).to.equal("success"));
338
+ });
273
339
  (0, mocha_1.it)("getSharedQueryClient returns the same QueryClient across callers", () => {
274
340
  (0, react_2.resetSharedQueryClient)();
275
341
  const a = (0, react_2.getSharedQueryClient)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doover-js",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "TypeScript client for Doover.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",