@noodleseed/assistant 1.14.0 → 1.16.0

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.
@@ -22,6 +22,14 @@ interface AssistantErrorDetail {
22
22
  readonly code: string;
23
23
  readonly status?: number;
24
24
  readonly retryable: boolean;
25
+ /**
26
+ * The service's own refusal code, when it sent one — e.g. `daily_turn_budget_exhausted`.
27
+ *
28
+ * Additive beside `code` rather than replacing it: `code` is a published closed union that shipped
29
+ * consumers already switch on, so remapping it would break them. This is how a renderer tells a
30
+ * capacity decision apart from a fault.
31
+ */
32
+ readonly serviceCode?: string;
25
33
  }
26
34
 
27
35
  interface NamedEvent<Name extends string, Data> {
@@ -49,6 +57,17 @@ interface AssistantToolProposedDetail extends TurnDetail {
49
57
  /** Optional only for compatibility with assistant services published before named-event pinning. */
50
58
  readonly requiresConfirmation?: true;
51
59
  }
60
+ /**
61
+ * A capability the visitor must sign in to reach (ADR 0201, 5.6b). `continuation` is the value the host
62
+ * page hands to its **own** backend, which spends it with its own client credentials; on its own it names
63
+ * a conversation and nothing more.
64
+ */
65
+ interface AssistantAuthRequestedDetail extends TurnDetail {
66
+ readonly id: string;
67
+ readonly tool: string;
68
+ readonly continuation: string;
69
+ readonly expiresAt: string;
70
+ }
52
71
  interface AssistantInputRequestedDetail extends TurnDetail {
53
72
  readonly id: string;
54
73
  readonly message: string;
@@ -90,6 +109,7 @@ type AssistantContentEvent = NamedEvent<'content', AssistantContentDetail>;
90
109
  type AssistantToolStartedEvent = NamedEvent<'tool_started', AssistantToolStartedDetail>;
91
110
  type AssistantToolProposedEvent = NamedEvent<'tool_proposed', AssistantToolProposedDetail>;
92
111
  type AssistantInputRequestedEvent = NamedEvent<'input_requested', AssistantInputRequestedDetail>;
112
+ type AssistantAuthRequestedEvent = NamedEvent<'auth_requested', AssistantAuthRequestedDetail>;
93
113
  type AssistantInteractionResolvedEvent = NamedEvent<'interaction_resolved', AssistantInteractionResolvedDetail>;
94
114
  type AssistantToolCompletedEvent = NamedEvent<'tool_completed', AssistantToolCompletedDetail>;
95
115
  type AssistantViewAvailableEvent = NamedEvent<'view_available', AssistantViewAvailableDetail>;
@@ -126,7 +146,7 @@ interface AssistantUnrecognizedEvent {
126
146
  };
127
147
  }
128
148
  /** Closed event union for the DOM-free client; unknown future SSE names remain observable safely. */
129
- type AssistantClientEvent = AssistantContentEvent | AssistantToolStartedEvent | AssistantToolProposedEvent | AssistantInputRequestedEvent | AssistantInteractionResolvedEvent | AssistantToolCompletedEvent | AssistantViewAvailableEvent | AssistantErrorEvent | AssistantDoneEvent | NamedEvent<'interaction_proposed', AssistantLegacyInteractionProposedDetail> | AssistantClientLifecycleEvent | AssistantUnrecognizedEvent;
149
+ type AssistantClientEvent = AssistantContentEvent | AssistantToolStartedEvent | AssistantToolProposedEvent | AssistantInputRequestedEvent | AssistantAuthRequestedEvent | AssistantInteractionResolvedEvent | AssistantToolCompletedEvent | AssistantViewAvailableEvent | AssistantErrorEvent | AssistantDoneEvent | NamedEvent<'interaction_proposed', AssistantLegacyInteractionProposedDetail> | AssistantClientLifecycleEvent | AssistantUnrecognizedEvent;
130
150
 
131
151
  type AssistantInteractionStatus = 'pending' | 'submitting' | 'accepted' | 'declined' | 'cancelled';
132
152
  interface AssistantConfirmationData {
@@ -165,6 +185,20 @@ interface AssistantChatState {
165
185
  readonly error?: AssistantChatError;
166
186
  }
167
187
 
188
+ /** The exclusive choice, expressed so the wrong pair does not typecheck in the first place. */
189
+ type AssistantSessionSourceOptions = {
190
+ /** The customer backend route that exchanges an embed secret for a session. */
191
+ readonly sessionEndpoint: string;
192
+ readonly embedId?: undefined;
193
+ readonly serviceUrl?: undefined;
194
+ } | {
195
+ /** The non-secret public embed id `noodle deploy` printed. Safe in page source. */
196
+ readonly embedId: string;
197
+ /** Defaults to the hosted service; set it for a dev or self-hosted deployment. */
198
+ readonly serviceUrl?: string | undefined;
199
+ readonly sessionEndpoint?: undefined;
200
+ };
201
+
168
202
  type AssistantContextValue = string | number | boolean | null;
169
203
  type AssistantContext = Readonly<Record<string, AssistantContextValue>>;
170
204
  interface AssistantClientContext {
@@ -192,8 +226,7 @@ interface AssistantSessionResponse {
192
226
  readonly configuration?: AssistantConfiguration;
193
227
  }
194
228
 
195
- interface CreateAssistantClientOptions<TPageContext extends AssistantPageContext = AssistantContext> {
196
- readonly sessionEndpoint: string;
229
+ interface AssistantClientBehaviorOptions<TPageContext extends AssistantPageContext = AssistantContext> {
197
230
  readonly fetch?: typeof fetch;
198
231
  /** Untrusted page context sent only when exchanging a session. */
199
232
  readonly context?: AssistantContext;
@@ -204,6 +237,12 @@ interface CreateAssistantClientOptions<TPageContext extends AssistantPageContext
204
237
  /** Latest renderer-selected summary included as untrusted data on every message turn. */
205
238
  readonly modelContext?: AssistantModelContextUpdate;
206
239
  }
240
+ /**
241
+ * How the client is mounted: through the customer's backend (`sessionEndpoint`) or, on a public page
242
+ * with no backend at all, straight to the service with a non-secret `embedId`. The two are exclusive —
243
+ * see `session-source.ts` for why they are not merged.
244
+ */
245
+ type CreateAssistantClientOptions<TPageContext extends AssistantPageContext = AssistantContext> = AssistantClientBehaviorOptions<TPageContext> & AssistantSessionSourceOptions;
207
246
  interface AssistantClient<TPageContext extends AssistantPageContext = AssistantContext> {
208
247
  subscribe(listener: (event: AssistantClientEvent) => void): () => void;
209
248
  /** Subscribe to detached AI SDK UIMessage state; the listener receives the current state immediately. */
@@ -22,6 +22,14 @@ interface AssistantErrorDetail {
22
22
  readonly code: string;
23
23
  readonly status?: number;
24
24
  readonly retryable: boolean;
25
+ /**
26
+ * The service's own refusal code, when it sent one — e.g. `daily_turn_budget_exhausted`.
27
+ *
28
+ * Additive beside `code` rather than replacing it: `code` is a published closed union that shipped
29
+ * consumers already switch on, so remapping it would break them. This is how a renderer tells a
30
+ * capacity decision apart from a fault.
31
+ */
32
+ readonly serviceCode?: string;
25
33
  }
26
34
 
27
35
  interface NamedEvent<Name extends string, Data> {
@@ -49,6 +57,17 @@ interface AssistantToolProposedDetail extends TurnDetail {
49
57
  /** Optional only for compatibility with assistant services published before named-event pinning. */
50
58
  readonly requiresConfirmation?: true;
51
59
  }
60
+ /**
61
+ * A capability the visitor must sign in to reach (ADR 0201, 5.6b). `continuation` is the value the host
62
+ * page hands to its **own** backend, which spends it with its own client credentials; on its own it names
63
+ * a conversation and nothing more.
64
+ */
65
+ interface AssistantAuthRequestedDetail extends TurnDetail {
66
+ readonly id: string;
67
+ readonly tool: string;
68
+ readonly continuation: string;
69
+ readonly expiresAt: string;
70
+ }
52
71
  interface AssistantInputRequestedDetail extends TurnDetail {
53
72
  readonly id: string;
54
73
  readonly message: string;
@@ -90,6 +109,7 @@ type AssistantContentEvent = NamedEvent<'content', AssistantContentDetail>;
90
109
  type AssistantToolStartedEvent = NamedEvent<'tool_started', AssistantToolStartedDetail>;
91
110
  type AssistantToolProposedEvent = NamedEvent<'tool_proposed', AssistantToolProposedDetail>;
92
111
  type AssistantInputRequestedEvent = NamedEvent<'input_requested', AssistantInputRequestedDetail>;
112
+ type AssistantAuthRequestedEvent = NamedEvent<'auth_requested', AssistantAuthRequestedDetail>;
93
113
  type AssistantInteractionResolvedEvent = NamedEvent<'interaction_resolved', AssistantInteractionResolvedDetail>;
94
114
  type AssistantToolCompletedEvent = NamedEvent<'tool_completed', AssistantToolCompletedDetail>;
95
115
  type AssistantViewAvailableEvent = NamedEvent<'view_available', AssistantViewAvailableDetail>;
@@ -126,7 +146,7 @@ interface AssistantUnrecognizedEvent {
126
146
  };
127
147
  }
128
148
  /** Closed event union for the DOM-free client; unknown future SSE names remain observable safely. */
129
- type AssistantClientEvent = AssistantContentEvent | AssistantToolStartedEvent | AssistantToolProposedEvent | AssistantInputRequestedEvent | AssistantInteractionResolvedEvent | AssistantToolCompletedEvent | AssistantViewAvailableEvent | AssistantErrorEvent | AssistantDoneEvent | NamedEvent<'interaction_proposed', AssistantLegacyInteractionProposedDetail> | AssistantClientLifecycleEvent | AssistantUnrecognizedEvent;
149
+ type AssistantClientEvent = AssistantContentEvent | AssistantToolStartedEvent | AssistantToolProposedEvent | AssistantInputRequestedEvent | AssistantAuthRequestedEvent | AssistantInteractionResolvedEvent | AssistantToolCompletedEvent | AssistantViewAvailableEvent | AssistantErrorEvent | AssistantDoneEvent | NamedEvent<'interaction_proposed', AssistantLegacyInteractionProposedDetail> | AssistantClientLifecycleEvent | AssistantUnrecognizedEvent;
130
150
 
131
151
  type AssistantInteractionStatus = 'pending' | 'submitting' | 'accepted' | 'declined' | 'cancelled';
132
152
  interface AssistantConfirmationData {
@@ -165,6 +185,20 @@ interface AssistantChatState {
165
185
  readonly error?: AssistantChatError;
166
186
  }
167
187
 
188
+ /** The exclusive choice, expressed so the wrong pair does not typecheck in the first place. */
189
+ type AssistantSessionSourceOptions = {
190
+ /** The customer backend route that exchanges an embed secret for a session. */
191
+ readonly sessionEndpoint: string;
192
+ readonly embedId?: undefined;
193
+ readonly serviceUrl?: undefined;
194
+ } | {
195
+ /** The non-secret public embed id `noodle deploy` printed. Safe in page source. */
196
+ readonly embedId: string;
197
+ /** Defaults to the hosted service; set it for a dev or self-hosted deployment. */
198
+ readonly serviceUrl?: string | undefined;
199
+ readonly sessionEndpoint?: undefined;
200
+ };
201
+
168
202
  type AssistantContextValue = string | number | boolean | null;
169
203
  type AssistantContext = Readonly<Record<string, AssistantContextValue>>;
170
204
  interface AssistantClientContext {
@@ -192,8 +226,7 @@ interface AssistantSessionResponse {
192
226
  readonly configuration?: AssistantConfiguration;
193
227
  }
194
228
 
195
- interface CreateAssistantClientOptions<TPageContext extends AssistantPageContext = AssistantContext> {
196
- readonly sessionEndpoint: string;
229
+ interface AssistantClientBehaviorOptions<TPageContext extends AssistantPageContext = AssistantContext> {
197
230
  readonly fetch?: typeof fetch;
198
231
  /** Untrusted page context sent only when exchanging a session. */
199
232
  readonly context?: AssistantContext;
@@ -204,6 +237,12 @@ interface CreateAssistantClientOptions<TPageContext extends AssistantPageContext
204
237
  /** Latest renderer-selected summary included as untrusted data on every message turn. */
205
238
  readonly modelContext?: AssistantModelContextUpdate;
206
239
  }
240
+ /**
241
+ * How the client is mounted: through the customer's backend (`sessionEndpoint`) or, on a public page
242
+ * with no backend at all, straight to the service with a non-secret `embedId`. The two are exclusive —
243
+ * see `session-source.ts` for why they are not merged.
244
+ */
245
+ type CreateAssistantClientOptions<TPageContext extends AssistantPageContext = AssistantContext> = AssistantClientBehaviorOptions<TPageContext> & AssistantSessionSourceOptions;
207
246
  interface AssistantClient<TPageContext extends AssistantPageContext = AssistantContext> {
208
247
  subscribe(listener: (event: AssistantClientEvent) => void): () => void;
209
248
  /** Subscribe to detached AI SDK UIMessage state; the listener receives the current state immediately. */
package/dist/client.cjs CHANGED
@@ -22918,6 +22918,11 @@ function toAssistantClientEvent(event) {
22918
22918
  return event;
22919
22919
  }
22920
22920
  break;
22921
+ case "auth_requested":
22922
+ if (hasString(value, "id") && hasString(value, "tool") && hasString(value, "continuation") && hasString(value, "expiresAt") && hasOptionalTurnId(value)) {
22923
+ return event;
22924
+ }
22925
+ break;
22921
22926
  case "interaction_resolved":
22922
22927
  if (hasString(value, "id") && (value.action === void 0 || isInteractionAction(value.action)) && hasOptionalTurnId(value)) {
22923
22928
  return event;
@@ -23102,6 +23107,21 @@ function hasToJsonProperty(value) {
23102
23107
  return false;
23103
23108
  }
23104
23109
 
23110
+ // src/session-source.ts
23111
+ var NOODLE_CLOUD_URL = "https://cloud.noodleseed.dev";
23112
+ var PUBLIC_SESSION_PATH = "/v1/assistant/public-sessions";
23113
+ function resolveSessionSource(options) {
23114
+ const { sessionEndpoint, embedId, serviceUrl } = options;
23115
+ if (Boolean(embedId) === Boolean(sessionEndpoint)) {
23116
+ throw new Error("pass either embedId or sessionEndpoint, not both and not neither");
23117
+ }
23118
+ if (embedId) {
23119
+ const base = (serviceUrl ?? NOODLE_CLOUD_URL).replace(/\/+$/, "");
23120
+ return { kind: "public", url: `${base}${PUBLIC_SESSION_PATH}`, embedId };
23121
+ }
23122
+ return { kind: "exchange", url: sessionEndpoint };
23123
+ }
23124
+
23105
23125
  // src/transport.ts
23106
23126
  var AssistantTransportError = class extends Error {
23107
23127
  code = "invalid_response";
@@ -23229,6 +23249,19 @@ function isAbortError2(error51) {
23229
23249
  }
23230
23250
 
23231
23251
  // src/client.ts
23252
+ async function refusalCode(response) {
23253
+ try {
23254
+ const body = await response.clone().json();
23255
+ const code = body.code;
23256
+ return typeof code === "string" ? code : void 0;
23257
+ } catch {
23258
+ return void 0;
23259
+ }
23260
+ }
23261
+ var UNRETRYABLE_SERVICE_CODES = /* @__PURE__ */ new Set([
23262
+ "daily_turn_budget_exhausted",
23263
+ "daily_session_budget_exhausted"
23264
+ ]);
23232
23265
  var AssistantClientError = class extends Error {
23233
23266
  detail;
23234
23267
  constructor(detail, message, options) {
@@ -23238,7 +23271,7 @@ var AssistantClientError = class extends Error {
23238
23271
  }
23239
23272
  };
23240
23273
  var DefaultAssistantClient = class {
23241
- #sessionEndpoint;
23274
+ #source;
23242
23275
  #fetch;
23243
23276
  #listeners = /* @__PURE__ */ new Set();
23244
23277
  #chat = new AssistantChatStateStore();
@@ -23250,8 +23283,7 @@ var DefaultAssistantClient = class {
23250
23283
  #active;
23251
23284
  #pendingAppInteractions = /* @__PURE__ */ new Map();
23252
23285
  constructor(options) {
23253
- if (!options.sessionEndpoint) throw new Error("sessionEndpoint is required");
23254
- this.#sessionEndpoint = options.sessionEndpoint;
23286
+ this.#source = resolveSessionSource(options);
23255
23287
  this.#fetch = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
23256
23288
  this.#context = options.context ? { ...options.context } : void 0;
23257
23289
  this.#modelContext = options.modelContext ? copyAssistantModelContext(options.modelContext) : void 0;
@@ -23467,11 +23499,14 @@ var DefaultAssistantClient = class {
23467
23499
  return;
23468
23500
  }
23469
23501
  if (!response.ok) {
23502
+ const serviceCode = await refusalCode(response);
23470
23503
  throw clientError(
23471
23504
  "turn_failed",
23472
23505
  `Assistant turn failed (${response.status})`,
23473
23506
  false,
23474
- response.status
23507
+ response.status,
23508
+ void 0,
23509
+ serviceCode
23475
23510
  );
23476
23511
  }
23477
23512
  await this.#consume(response);
@@ -23510,23 +23545,25 @@ var DefaultAssistantClient = class {
23510
23545
  }
23511
23546
  }
23512
23547
  async #createSession(signal) {
23513
- const response = await this.#request(
23514
- this.#sessionEndpoint,
23515
- {
23516
- method: "POST",
23517
- headers: { Accept: "application/json", "Content-Type": "application/json" },
23518
- body: JSON.stringify(this.#context ? { context: this.#context } : {}),
23519
- credentials: "same-origin",
23520
- signal
23521
- },
23522
- "session_failed"
23523
- );
23548
+ const source = this.#source;
23549
+ const response = await this.#requestSession(source, {
23550
+ method: "POST",
23551
+ headers: { Accept: "application/json", "Content-Type": "application/json" },
23552
+ body: JSON.stringify(
23553
+ source.kind === "public" ? { embedId: source.embedId } : this.#context ? { context: this.#context } : {}
23554
+ ),
23555
+ credentials: source.kind === "public" ? "omit" : "same-origin",
23556
+ signal
23557
+ });
23524
23558
  if (!response.ok) {
23559
+ const serviceCode = await refusalCode(response);
23525
23560
  throw clientError(
23526
23561
  "session_failed",
23527
23562
  `Assistant session failed (${response.status})`,
23528
- true,
23529
- response.status
23563
+ serviceCode === void 0 || !UNRETRYABLE_SERVICE_CODES.has(serviceCode),
23564
+ response.status,
23565
+ void 0,
23566
+ serviceCode
23530
23567
  );
23531
23568
  }
23532
23569
  let value;
@@ -23583,6 +23620,34 @@ var DefaultAssistantClient = class {
23583
23620
  );
23584
23621
  }
23585
23622
  }
23623
+ /**
23624
+ * The mint, with the one failure a public page hits that an in-app embed cannot.
23625
+ *
23626
+ * A page that loads the embed script and then blocks `connect-src` fails here as a bare network
23627
+ * rejection — no status, no body, nothing to read. "Assistant request failed" sends a developer
23628
+ * hunting through their own code; naming the directive and the origin ends the search. The message
23629
+ * covers a plain outage too, because from inside the browser the two are indistinguishable.
23630
+ */
23631
+ async #requestSession(source, init) {
23632
+ try {
23633
+ return await this.#fetch(source.url, init);
23634
+ } catch (error51) {
23635
+ if (init.signal?.aborted) throw error51;
23636
+ if (source.kind !== "public") {
23637
+ throw clientError("session_failed", "Assistant request failed", true, void 0, {
23638
+ cause: error51
23639
+ });
23640
+ }
23641
+ throw clientError(
23642
+ "session_failed",
23643
+ `Assistant could not reach ${new URL(source.url).origin}. If the page sets a Content-Security-Policy, allow that origin in connect-src (and in script-src and frame-src).`,
23644
+ true,
23645
+ void 0,
23646
+ { cause: error51 },
23647
+ "blocked_by_page"
23648
+ );
23649
+ }
23650
+ }
23586
23651
  async #request(input, init, failureCode) {
23587
23652
  try {
23588
23653
  return await this.#fetch(input, init);
@@ -23704,9 +23769,14 @@ async function readStableErrorCode(response) {
23704
23769
  if (!isRecord4(value) || typeof value.code !== "string") return void 0;
23705
23770
  return /^[A-Za-z0-9_.-]{1,64}$/.test(value.code) ? value.code : void 0;
23706
23771
  }
23707
- function clientError(code, message, retryable, status, options) {
23772
+ function clientError(code, message, retryable, status, options, serviceCode) {
23708
23773
  return new AssistantClientError(
23709
- { code, ...status === void 0 ? {} : { status }, retryable },
23774
+ {
23775
+ code,
23776
+ ...status === void 0 ? {} : { status },
23777
+ retryable,
23778
+ ...serviceCode === void 0 ? {} : { serviceCode }
23779
+ },
23710
23780
  message,
23711
23781
  options
23712
23782
  );