@opendikw/web-client 0.1.7 → 0.1.9-rc.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/README.md CHANGED
@@ -69,3 +69,13 @@ remain in that repository.
69
69
  For document Q&A, createSession(signal, { pagePath: "sources/paper.md" }) binds an
70
70
  immutable evidence scope to the session. Omit the second argument for existing
71
71
  whole-base conversations. Session.scope exposes the server-confirmed path.
72
+
73
+ `AgentClient` and `DikwClient` accept `requestHeaders: () => ({ "X-App-Model": selectedModelId })`
74
+ for optional nonsecret application headers on every request, including session CRUD,
75
+ aborts, multipart imports and streaming calls. Core only forwards them to the current
76
+ browser origin. Both clients also accept an explicit `fetch` transport so applications
77
+ can handle an identity mismatch without replacing global fetch.
78
+ The clients retain their JSON request and NDJSON response headers and ignore
79
+ authentication, cookie, Origin/Host and Core connection override headers from this hook.
80
+ Do not place
81
+ provider credentials in browser headers; resolve them in your authenticated BFF.
@@ -3,6 +3,10 @@ import type { SessionTraceView } from "../types/trace.js";
3
3
  export interface AgentClientOptions {
4
4
  coreUrl?: string;
5
5
  token?: string;
6
+ /** Resolve nonsecret application headers separately for each model turn. */
7
+ requestHeaders?: () => Record<string, string>;
8
+ /** Explicit application transport, also used for session CRUD and aborts. */
9
+ fetch?: typeof fetch;
6
10
  }
7
11
  export declare class AgentClientError extends Error {
8
12
  readonly status: number;
@@ -22,5 +26,6 @@ export declare class AgentClient {
22
26
  confirmProposal(sessionId: string, proposalId: string, signal?: AbortSignal): Promise<AgentSession>;
23
27
  rejectProposal(sessionId: string, proposalId: string, signal?: AbortSignal): Promise<AgentSession>;
24
28
  sendMessage(sessionId: string, message: string, signal?: AbortSignal): AsyncGenerator<AgentStreamEvent>;
29
+ private requestJson;
25
30
  private connectionPayload;
26
31
  }
@@ -1,3 +1,4 @@
1
+ import { applicationHeaders } from "../applicationHeaders.js";
1
2
  import { decodeNdjsonStream } from "../core/ndjson.js";
2
3
  export class AgentClientError extends Error {
3
4
  status;
@@ -15,52 +16,53 @@ export class AgentClient {
15
16
  this.options = options;
16
17
  }
17
18
  async listSessions(signal) {
18
- return requestJson("/agent/sessions", { signal });
19
+ return this.requestJson("/agent/sessions", { signal });
19
20
  }
20
21
  async createSession(signal, scope) {
21
- return requestJson("/agent/sessions", {
22
+ return this.requestJson("/agent/sessions", {
22
23
  method: "POST",
23
24
  ...(scope ? { body: JSON.stringify({ scope }) } : {}),
24
25
  signal,
25
26
  });
26
27
  }
27
28
  async getSession(sessionId, signal) {
28
- return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
29
+ return this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
29
30
  signal,
30
31
  });
31
32
  }
32
33
  async getSessionTraces(sessionId, signal) {
33
- return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/traces`, { signal });
34
+ return this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/traces`, { signal });
34
35
  }
35
36
  async renameSession(sessionId, title, signal) {
36
- return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
37
+ return this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
37
38
  method: "PATCH",
38
39
  body: JSON.stringify({ title }),
39
40
  signal,
40
41
  });
41
42
  }
42
43
  async deleteSession(sessionId, signal) {
43
- await requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
44
+ await this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}`, {
44
45
  method: "DELETE",
45
46
  signal,
46
47
  });
47
48
  }
48
49
  async abort(sessionId, signal) {
49
- await requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/abort`, {
50
+ await this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/abort`, {
50
51
  method: "POST",
51
52
  signal,
52
53
  });
53
54
  }
54
55
  async confirmProposal(sessionId, proposalId, signal) {
55
- return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/confirm`, { method: "POST", body: JSON.stringify(this.connectionPayload()), signal });
56
+ return this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/confirm`, { method: "POST", body: JSON.stringify(this.connectionPayload()), signal });
56
57
  }
57
58
  async rejectProposal(sessionId, proposalId, signal) {
58
- return requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/reject`, { method: "POST", signal });
59
+ return this.requestJson(`/agent/sessions/${encodeURIComponent(sessionId)}/proposals/${encodeURIComponent(proposalId)}/reject`, { method: "POST", signal });
59
60
  }
60
61
  async *sendMessage(sessionId, message, signal) {
61
- const response = await fetch(`/agent/sessions/${encodeURIComponent(sessionId)}/messages`, {
62
+ const response = await (this.options.fetch ?? fetch)(`/agent/sessions/${encodeURIComponent(sessionId)}/messages`, {
62
63
  method: "POST",
63
64
  headers: {
65
+ ...applicationHeaders(this.options.requestHeaders?.()),
64
66
  Accept: "application/x-ndjson",
65
67
  "Content-Type": "application/json",
66
68
  },
@@ -77,6 +79,12 @@ export class AgentClient {
77
79
  yield event;
78
80
  }
79
81
  }
82
+ requestJson(path, init = {}) {
83
+ return requestJson(path, {
84
+ ...init,
85
+ headers: { ...applicationHeaders(this.options.requestHeaders?.()), ...init.headers },
86
+ }, this.options.fetch ?? fetch);
87
+ }
80
88
  connectionPayload() {
81
89
  return {
82
90
  ...(this.options.coreUrl ? { coreUrl: this.options.coreUrl } : {}),
@@ -84,8 +92,8 @@ export class AgentClient {
84
92
  };
85
93
  }
86
94
  }
87
- async function requestJson(path, init = {}) {
88
- const response = await fetch(path, {
95
+ async function requestJson(path, init = {}, fetcher = fetch) {
96
+ const response = await fetcher(path, {
89
97
  ...init,
90
98
  headers: {
91
99
  Accept: "application/json",
@@ -0,0 +1,2 @@
1
+ /** Application hooks carry nonsecret metadata, never authentication or endpoints. */
2
+ export declare function applicationHeaders(values?: Record<string, string>): Record<string, string>;
@@ -0,0 +1,17 @@
1
+ /** Application hooks carry nonsecret metadata, never authentication or endpoints. */
2
+ export function applicationHeaders(values = {}) {
3
+ return Object.fromEntries(Object.entries(values).filter(([name]) => ![
4
+ "accept",
5
+ "content-type",
6
+ "authorization",
7
+ "proxy-authorization",
8
+ "cookie",
9
+ "cookie2",
10
+ "host",
11
+ "origin",
12
+ "x-dikw-core-url",
13
+ "x-dikw-core-token",
14
+ "x-core-url",
15
+ "x-core-token",
16
+ ].includes(name.toLowerCase())));
17
+ }
@@ -8,6 +8,10 @@ export interface DikwClientConfig {
8
8
  * to the empty string in same-origin proxy mode; pass an explicit value
9
9
  * (e.g. the user-visible server URL) to distinguish proxy targets. */
10
10
  coreId?: string;
11
+ /** Nonsecret first-party headers, resolved for every request. */
12
+ requestHeaders?: () => Record<string, string>;
13
+ /** Explicit application transport; never changes global fetch. */
14
+ fetch?: typeof fetch;
11
15
  }
12
16
  export interface JsonRequestOptions {
13
17
  method?: "GET" | "POST";
@@ -27,6 +31,7 @@ export declare class DikwClientError extends Error {
27
31
  });
28
32
  }
29
33
  export declare class DikwClient {
34
+ private readonly config;
30
35
  private readonly baseUrl;
31
36
  private readonly token;
32
37
  private readonly _coreId;
@@ -88,6 +93,7 @@ export declare class DikwClient {
88
93
  }, signal?: AbortSignal): AsyncGenerator<RetrieveStreamEvent>;
89
94
  streamTaskEvents(taskId: string, fromSeq?: number, signal?: AbortSignal): AsyncGenerator<TaskEvent>;
90
95
  streamNdjson<T>(path: string, options?: JsonRequestOptions): AsyncGenerator<T>;
96
+ private applicationHeaders;
91
97
  private headers;
92
98
  }
93
99
  export declare function buildRequestUrl(baseUrl: string, path: string, params?: Record<string, string | number | boolean | null | undefined>): string;
@@ -1,3 +1,4 @@
1
+ import { applicationHeaders } from "../applicationHeaders.js";
1
2
  import { decodeNdjsonStream } from "./ndjson.js";
2
3
  import { prepareImport, protectReferencedAssets } from "./import-safety.js";
3
4
  export class DikwClientError extends Error {
@@ -13,10 +14,12 @@ export class DikwClientError extends Error {
13
14
  }
14
15
  }
15
16
  export class DikwClient {
17
+ config;
16
18
  baseUrl;
17
19
  token;
18
20
  _coreId;
19
21
  constructor(config = {}) {
22
+ this.config = config;
20
23
  this.baseUrl = normalizeBaseUrl(config.baseUrl ?? "");
21
24
  this.token = config.token ?? "";
22
25
  // Default to the normalized baseUrl, but allow callers to override —
@@ -184,12 +187,13 @@ export class DikwClient {
184
187
  async postMultipart(path, form, signal) {
185
188
  // FormData sets its own Content-Type with boundary — never inject one.
186
189
  const headers = {
190
+ ...this.applicationHeaders(),
187
191
  Accept: "application/json",
188
192
  };
189
193
  if (this.token) {
190
194
  headers.Authorization = `Bearer ${this.token}`;
191
195
  }
192
- const response = await fetch(buildRequestUrl(this.baseUrl, path), {
196
+ const response = await (this.config.fetch ?? fetch)(buildRequestUrl(this.baseUrl, path), {
193
197
  method: "POST",
194
198
  headers,
195
199
  body: form,
@@ -204,7 +208,7 @@ export class DikwClient {
204
208
  return (await response.json());
205
209
  }
206
210
  async requestJson(path, options = {}) {
207
- const response = await fetch(buildRequestUrl(this.baseUrl, path, options.params), {
211
+ const response = await (this.config.fetch ?? fetch)(buildRequestUrl(this.baseUrl, path, options.params), {
208
212
  method: options.method ?? "GET",
209
213
  headers: this.headers(options.body !== undefined),
210
214
  body: options.body === undefined ? undefined : JSON.stringify(options.body),
@@ -265,7 +269,7 @@ export class DikwClient {
265
269
  }
266
270
  }
267
271
  async *streamNdjson(path, options = {}) {
268
- const response = await fetch(buildRequestUrl(this.baseUrl, path, options.params), {
272
+ const response = await (this.config.fetch ?? fetch)(buildRequestUrl(this.baseUrl, path, options.params), {
269
273
  method: options.method ?? "GET",
270
274
  headers: this.headers(options.body !== undefined),
271
275
  body: options.body === undefined ? undefined : JSON.stringify(options.body),
@@ -295,8 +299,16 @@ export class DikwClient {
295
299
  yield event;
296
300
  }
297
301
  }
302
+ applicationHeaders() {
303
+ if (typeof window !== "undefined" &&
304
+ this.baseUrl &&
305
+ new URL(this.baseUrl, window.location.href).origin !== window.location.origin)
306
+ return {};
307
+ return applicationHeaders(this.config.requestHeaders?.());
308
+ }
298
309
  headers(hasJsonBody) {
299
310
  const headers = {
311
+ ...this.applicationHeaders(),
300
312
  Accept: "application/json, application/x-ndjson",
301
313
  };
302
314
  if (hasJsonBody) {
@@ -35,7 +35,7 @@ export interface TranslateOptions {
35
35
  fetch?: typeof fetch;
36
36
  pollIntervalMs?: number;
37
37
  }
38
- export type TranslateErrorCode = "translator_auth" | "translator_rate_limit" | "translator_timeout" | "translator_api" | "translator_invalid_response" | "translator_input" | "translate_disabled" | "aborted" | "invalid_response";
38
+ export type TranslateErrorCode = "translator_auth" | "translator_forbidden" | "translator_settings_changed" | "translator_unavailable" | "translator_rate_limit" | "translator_timeout" | "translator_api" | "translator_invalid_response" | "translator_input" | "translate_disabled" | "aborted" | "invalid_response";
39
39
  export declare class TranslateError extends Error {
40
40
  readonly code: TranslateErrorCode;
41
41
  constructor(code: TranslateErrorCode, message: string);
@@ -268,6 +268,9 @@ function serverErrorMessage(body) {
268
268
  }
269
269
  const KNOWN_CODES = new Set([
270
270
  "translator_auth",
271
+ "translator_forbidden",
272
+ "translator_settings_changed",
273
+ "translator_unavailable",
271
274
  "translator_rate_limit",
272
275
  "translator_timeout",
273
276
  "translator_api",
@@ -286,6 +289,12 @@ function pickErrorCode(body, status) {
286
289
  return "translate_disabled";
287
290
  if (status === 401)
288
291
  return "translator_auth";
292
+ if (status === 403)
293
+ return "translator_forbidden";
294
+ if (status === 409)
295
+ return "translator_settings_changed";
296
+ if (status === 422)
297
+ return "translator_unavailable";
289
298
  if (status === 413)
290
299
  return "translator_input";
291
300
  if (status === 429)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opendikw/web-client",
3
- "version": "0.1.7",
3
+ "version": "0.1.9-rc.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "engines": {