agent-blackboard 0.3.0 → 0.3.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.
@@ -1,4 +1,8 @@
1
1
  import { AgentBlackboardError } from './errors.mjs';
2
+ const DEFAULT_READ_RETRY = { maxRetries: 2, initialDelayMs: 100, maxDelayMs: 1000 };
3
+ const MAX_READ_RETRIES = 10;
4
+ const MAX_READ_DELAY_MS = 60_000;
5
+ const RETRYABLE_READ_STATUSES = new Set([408, 429, 500, 502, 503, 504]);
2
6
  /** Converts a typed entry query into wire-format string params. */
3
7
  export function buildEntriesQuery(format) {
4
8
  const wire = {};
@@ -23,6 +27,14 @@ function buildUrl(baseUrl, path, query) {
23
27
  }
24
28
  return url;
25
29
  }
30
+ function assertSecureUrl(url) {
31
+ if (url.protocol === 'https:')
32
+ return;
33
+ if (url.protocol === 'http:' &&
34
+ (url.hostname === 'localhost' || url.hostname === '127.0.0.1' || url.hostname === '[::1]'))
35
+ return;
36
+ throw new TypeError('baseUrl must use HTTPS; HTTP is allowed only for local loopback servers');
37
+ }
26
38
  async function parseErrorBody(response) {
27
39
  const text = await response.text();
28
40
  if (!text)
@@ -34,6 +46,34 @@ async function parseErrorBody(response) {
34
46
  return text;
35
47
  }
36
48
  }
49
+ function readRetry(config, method) {
50
+ if (method !== 'GET' || config.readRetry === undefined)
51
+ return undefined;
52
+ if (typeof config.readRetry !== 'object' || config.readRetry === null)
53
+ throw new TypeError('readRetry must be an object');
54
+ const options = { ...DEFAULT_READ_RETRY, ...config.readRetry };
55
+ for (const [name, value, maximum] of [
56
+ ['maxRetries', options.maxRetries, MAX_READ_RETRIES],
57
+ ['initialDelayMs', options.initialDelayMs, MAX_READ_DELAY_MS],
58
+ ['maxDelayMs', options.maxDelayMs, MAX_READ_DELAY_MS],
59
+ ]) {
60
+ if (!Number.isSafeInteger(value) || value < 0 || value > maximum)
61
+ throw new RangeError(`${name} must be a non-negative integer no greater than ${maximum}`);
62
+ }
63
+ return options;
64
+ }
65
+ function delay(milliseconds) {
66
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
67
+ }
68
+ function retryDelay(options, attempt) {
69
+ return Math.min(options.initialDelayMs * 2 ** attempt, options.maxDelayMs);
70
+ }
71
+ async function errorFor(response, method, path) {
72
+ throw new AgentBlackboardError(`agent-blackboard request failed: ${method} ${path} -> ${response.status}`, response.status, await parseErrorBody(response));
73
+ }
74
+ async function discard(response) {
75
+ await response.body?.cancel().catch(() => undefined);
76
+ }
37
77
  /**
38
78
  * Issues an authenticated HTTP request and returns the raw `Response`,
39
79
  * throwing `AgentBlackboardError` on any non-2xx status. Callers that need the
@@ -41,17 +81,31 @@ async function parseErrorBody(response) {
41
81
  */
42
82
  export async function rawRequest(config, path, options) {
43
83
  const url = buildUrl(config.baseUrl, path, options.query ?? {});
84
+ assertSecureUrl(url);
44
85
  const headers = new Headers(options.headers);
45
86
  headers.set('authorization', `Bearer ${config.token}`);
46
87
  const init = { method: options.method, headers };
47
88
  if (options.body !== undefined)
48
89
  init.body = options.body;
49
- const response = await fetch(url, init);
50
- if (!response.ok) {
51
- const body = await parseErrorBody(response);
52
- throw new AgentBlackboardError(`agent-blackboard request failed: ${options.method} ${path} -> ${response.status}`, response.status, body);
90
+ const retry = readRetry(config, options.method);
91
+ for (let attempt = 0;; attempt++) {
92
+ let response;
93
+ try {
94
+ response = await fetch(url, init);
95
+ }
96
+ catch (error) {
97
+ if (!retry || attempt === retry.maxRetries)
98
+ throw error;
99
+ await delay(retryDelay(retry, attempt));
100
+ continue;
101
+ }
102
+ if (response.ok)
103
+ return response;
104
+ if (!retry || attempt === retry.maxRetries || !RETRYABLE_READ_STATUSES.has(response.status))
105
+ return errorFor(response, options.method, path);
106
+ await discard(response);
107
+ await delay(retryDelay(retry, attempt));
53
108
  }
54
- return response;
55
109
  }
56
110
  /** Issues an authenticated HTTP request and parses the response body as JSON. */
57
111
  export async function requestJson(config, path, options) {
@@ -1,6 +1,17 @@
1
1
  export interface ClientConfig {
2
2
  baseUrl: string;
3
3
  token: string;
4
+ /** Opt in to bounded retries for safe GET requests. */
5
+ readRetry?: ReadRetryOptions;
6
+ }
7
+ /** Bounded retry settings for GET requests that fail before a response or transiently. */
8
+ export interface ReadRetryOptions {
9
+ /** Retries after the initial request. Defaults to 2. */
10
+ maxRetries?: number;
11
+ /** First retry delay in milliseconds. Defaults to 100. */
12
+ initialDelayMs?: number;
13
+ /** Maximum retry delay in milliseconds. Defaults to 1000. */
14
+ maxDelayMs?: number;
4
15
  }
5
16
  export interface Session {
6
17
  id: string;
package/dist/index.d.mts CHANGED
@@ -17,4 +17,4 @@ export { Entries } from './client/entries.mjs';
17
17
  export { formatError } from './format-error.mjs';
18
18
  export { Sessions } from './client/sessions.mjs';
19
19
  export { Snapshots } from './client/snapshots.mjs';
20
- export type { AppendEntryInput, ClientConfig, CreateSessionInput, CredentialCreated, CredentialSummary, EntryWireFormat, GetEntriesQuery, GetRawEntriesQuery, ListSessionsQuery, PatchSessionInput, Session, SessionEntry, StructuredEntryFormat, SnapshotCounts, SnapshotExportOptions, SnapshotExportResult, SnapshotManifest, SnapshotSelection, } from './client/types.mjs';
20
+ export type { AppendEntryInput, ClientConfig, CreateSessionInput, CredentialCreated, CredentialSummary, EntryWireFormat, GetEntriesQuery, GetRawEntriesQuery, ListSessionsQuery, PatchSessionInput, ReadRetryOptions, Session, SessionEntry, StructuredEntryFormat, SnapshotCounts, SnapshotExportOptions, SnapshotExportResult, SnapshotManifest, SnapshotSelection, } from './client/types.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-blackboard",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Client library, CLI, and MCP server for Agent Blackboard session entries.",
5
5
  "homepage": "https://github.com/jonathanong/agent-blackboard#readme",
6
6
  "bugs": {