@experiture/xyra-cli 0.6.1 → 0.6.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.
@@ -21903,7 +21903,7 @@ var RUNTIME_ACTIVITY_DETAIL_KINDS = [
21903
21903
  "recovery",
21904
21904
  "response"
21905
21905
  ];
21906
- var ACTIVITY_ID = /^(?:preparing:(?:context|media)|routing:route|routing:capability-handoff:o[1-9][0-9]*|working:attention|working:solver(?::o[1-9][0-9]*)?|working:recovery|responding:response|working:tool:r[1-9][0-9]*:t[1-9][0-9]*(?::s[1-9][0-9]*:(?:a[1-9][0-9]*|retry:a(?:[2-9]|[1-9][0-9]+)))?|(?:preparing|routing|working|responding):provider-retry:e[1-9][0-9]*:a(?:[2-9]|[1-9][0-9]+))$/;
21906
+ var ACTIVITY_ID = /^(?:preparing:(?:context|media)|routing:route|routing:capability-handoff:o[1-9][0-9]*|working:solver(?::o[1-9][0-9]*)?|working:recovery|responding:response|working:tool:r[1-9][0-9]*:t[1-9][0-9]*(?::s[1-9][0-9]*:(?:a[1-9][0-9]*|retry:a(?:[2-9]|[1-9][0-9]+)))?|(?:preparing|routing|working|responding):provider-retry:e[1-9][0-9]*:a(?:[2-9]|[1-9][0-9]+))$/;
21907
21907
  var CONTROL_OR_BIDI = /[\p{Cc}\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069]/u;
21908
21908
  function includes(values, value) {
21909
21909
  return typeof value === "string" && values.includes(value);
@@ -21911,7 +21911,7 @@ function includes(values, value) {
21911
21911
  function runtimeActivityIssue(value) {
21912
21912
  if (!isRecord(value))
21913
21913
  return "activity_detail_shape_invalid";
21914
- if (Object.keys(value).sort().join("|") !== ["activityId", ...value.attention === void 0 ? [] : ["attention"], "detail", "displayText", "phase", "schemaVersion", "status", "textSource"].sort().join("|"))
21914
+ if (Object.keys(value).sort().join("|") !== ["activityId", "detail", "displayText", "phase", "schemaVersion", "status", "textSource"].join("|"))
21915
21915
  return "activity_unexpected_field";
21916
21916
  if (value.schemaVersion !== 2)
21917
21917
  return "activity_schema_version_invalid";
@@ -21932,7 +21932,7 @@ function runtimeActivityIssue(value) {
21932
21932
  if (!includes(RUNTIME_ACTIVITY_DETAIL_KINDS, value.detail.kind))
21933
21933
  return "activity_detail_kind_invalid";
21934
21934
  const identity = value.activityId;
21935
- const expectedKind = identity === "preparing:context" ? "context" : identity === "preparing:media" ? "media" : identity === "routing:route" ? "route" : identity.startsWith("routing:capability-handoff:o") ? "capability_handoff" : identity === "working:attention" ? "tool" : identity === "working:solver" || identity.startsWith("working:solver:o") ? "solver" : identity === "working:recovery" ? "recovery" : identity === "responding:response" ? "response" : identity.includes(":provider-retry:") || identity.includes(":retry:") ? "retry" : identity.includes(":s") ? "workflow_step" : "tool";
21935
+ const expectedKind = identity === "preparing:context" ? "context" : identity === "preparing:media" ? "media" : identity === "routing:route" ? "route" : identity.startsWith("routing:capability-handoff:o") ? "capability_handoff" : identity === "working:solver" || identity.startsWith("working:solver:o") ? "solver" : identity === "working:recovery" ? "recovery" : identity === "responding:response" ? "response" : identity.includes(":provider-retry:") || identity.includes(":retry:") ? "retry" : identity.includes(":s") ? "workflow_step" : "tool";
21936
21936
  if (value.detail.kind !== expectedKind || !identity.startsWith(`${value.phase}:`))
21937
21937
  return "activity_detail_identity_mismatch";
21938
21938
  const bounded = (number, minimum = 0) => typeof number === "number" && Number.isSafeInteger(number) && number >= minimum && number <= 999;
@@ -22054,9 +22054,7 @@ var XyraTurnProtocolValidator = class {
22054
22054
  if (activityIssue)
22055
22055
  this.failProtocol(activityIssue, "Runtime turn emitted an invalid activity payload.", "activity");
22056
22056
  const prior = this.activityLifecycle.get(payload2.activityId);
22057
- if (payload2.activityId === "working:attention") {
22058
- this.activityLifecycle.set(payload2.activityId, payload2);
22059
- } else if (!prior) {
22057
+ if (!prior) {
22060
22058
  if (payload2.status !== "started") {
22061
22059
  this.failProtocol("activity_terminal_before_started", "Runtime activity terminal status arrived before started.", "activity");
22062
22060
  }
@@ -22282,12 +22280,29 @@ function combineAbortSignals(...signals) {
22282
22280
  function extractSseEvents(buffer) {
22283
22281
  const segments = buffer.split("\n\n");
22284
22282
  const remaining = segments.pop() ?? "";
22285
- const events = segments.map((entry) => entry.trim()).filter(Boolean).map((entry) => {
22286
- const lines = entry.split("\n");
22287
- const event = lines.find((line) => line.startsWith("event:"))?.slice(6).trim() || "message";
22288
- const data = lines.filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim()).join("\n");
22289
- return { event, data };
22290
- });
22283
+ const events = [];
22284
+ for (const segment of segments) {
22285
+ let event = "message";
22286
+ let hasData = false;
22287
+ const dataLines = [];
22288
+ for (const line of segment.split("\n")) {
22289
+ if (line.startsWith(":"))
22290
+ continue;
22291
+ const separator = line.indexOf(":");
22292
+ const field = separator < 0 ? line : line.slice(0, separator);
22293
+ let value = separator < 0 ? "" : line.slice(separator + 1);
22294
+ if (value.startsWith(" "))
22295
+ value = value.slice(1);
22296
+ if (field === "event")
22297
+ event = value || "message";
22298
+ if (field === "data") {
22299
+ hasData = true;
22300
+ dataLines.push(value);
22301
+ }
22302
+ }
22303
+ if (hasData)
22304
+ events.push({ event, data: dataLines.join("\n") });
22305
+ }
22291
22306
  return { events, remaining };
22292
22307
  }
22293
22308
  var XyraRuntimeClient = class {
@@ -22324,15 +22339,6 @@ var XyraRuntimeClient = class {
22324
22339
  });
22325
22340
  return this.readSuccessEnvelope(response);
22326
22341
  }
22327
- async submitTimingAction(submission, options = {}) {
22328
- const response = await this.executeRequest(`${this.baseUrl}/actions`, {
22329
- method: "POST",
22330
- headers: this.buildHeaders(options),
22331
- body: JSON.stringify(submission),
22332
- signal: options.signal
22333
- });
22334
- return this.readSuccessEnvelope(response);
22335
- }
22336
22342
  async getHumanApprovalPostureOptions(conversationId, options = {}) {
22337
22343
  const response = await this.submitAction({
22338
22344
  conversationId,
@@ -22756,7 +22762,7 @@ var XyraRuntimeClient = class {
22756
22762
  });
22757
22763
  }
22758
22764
  if (payload.disposition === "waiting") {
22759
- if (!payload.conversationId || !payload.clientRequestId || !payload.runtimeTurnId || !payload.attention) {
22765
+ if (!payload.conversationId || !payload.clientRequestId || !payload.runtimeTurnId) {
22760
22766
  throw new XyraRuntimeClientError({
22761
22767
  kind: "canonical",
22762
22768
  code: "XYRA.RUNTIME.TURN_PROTOCOL_VIOLATION",
@@ -22768,8 +22774,7 @@ var XyraRuntimeClient = class {
22768
22774
  reason: payload.reason ?? "observation_window_ended",
22769
22775
  conversationId: payload.conversationId,
22770
22776
  clientRequestId: payload.clientRequestId,
22771
- runtimeTurnId: payload.runtimeTurnId,
22772
- attention: payload.attention
22777
+ runtimeTurnId: payload.runtimeTurnId
22773
22778
  };
22774
22779
  }
22775
22780
  if (!Array.isArray(payload.events)) {
@@ -22895,38 +22900,7 @@ var XyraRuntimeClient = class {
22895
22900
 
22896
22901
  // src/chat/runtimeChatClient.ts
22897
22902
  var import_conversation_creation = __toESM(require_conversation_creation());
22898
-
22899
- // src/errors/CliError.ts
22900
- var CliError = class extends Error {
22901
- code;
22902
- hint;
22903
- httpStatus;
22904
- constructor(code, message, hint, httpStatus) {
22905
- super(message);
22906
- this.name = "CliError";
22907
- this.code = code;
22908
- this.hint = hint;
22909
- this.httpStatus = httpStatus;
22910
- }
22911
- };
22912
- var CliErrorCodes = {
22913
- ConfigInvalid: "XYRA.CLI.CONFIG_INVALID",
22914
- EnvironmentNotFound: "XYRA.CLI.ENVIRONMENT_NOT_FOUND",
22915
- NotAuthenticated: "XYRA.CLI.NOT_AUTHENTICATED",
22916
- OAuthStateMismatch: "XYRA.CLI.OAUTH_STATE_MISMATCH",
22917
- OAuthCallbackError: "XYRA.CLI.OAUTH_CALLBACK_ERROR",
22918
- TokenStoreUnavailable: "XYRA.CLI.TOKEN_STORE_UNAVAILABLE",
22919
- TokenExchangeFailed: "XYRA.CLI.TOKEN_EXCHANGE_FAILED",
22920
- InsufficientScope: "XYRA.CLI.INSUFFICIENT_SCOPE",
22921
- McpProtocolError: "XYRA.CLI.MCP_PROTOCOL_ERROR",
22922
- ToolCallFailed: "XYRA.CLI.TOOL_CALL_FAILED",
22923
- ChatRequestFailed: "XYRA.CLI.CHAT_REQUEST_FAILED",
22924
- InvalidArguments: "XYRA.CLI.INVALID_ARGUMENTS",
22925
- ConfirmationRequired: "XYRA.CLI.CONFIRMATION_REQUIRED",
22926
- AmbiguousAlias: "XYRA.CLI.AMBIGUOUS_ALIAS"
22927
- };
22928
-
22929
- // src/chat/runtimeChatClient.ts
22903
+ var import_CliError = require("../errors/CliError");
22930
22904
  var XYRA_CLI_RUNTIME_LOCALES = ["en", "fr", "zh-Hans"];
22931
22905
  var XYRA_CLI_RUNTIME_STREAM_TIMEOUT_MS_V1 = 3e5;
22932
22906
  function isRecord3(value) {
@@ -22937,8 +22911,8 @@ function parseRuntimeLocale(value) {
22937
22911
  if (XYRA_CLI_RUNTIME_LOCALES.includes(candidate)) {
22938
22912
  return candidate;
22939
22913
  }
22940
- throw new CliError(
22941
- CliErrorCodes.InvalidArguments,
22914
+ throw new import_CliError.CliError(
22915
+ import_CliError.CliErrorCodes.InvalidArguments,
22942
22916
  `Unsupported chat locale '${candidate}'.`,
22943
22917
  `Use one of: ${XYRA_CLI_RUNTIME_LOCALES.join(", ")}.`
22944
22918
  );
@@ -22964,13 +22938,36 @@ function runtimeErrorBody(value) {
22964
22938
  if (!isRecord3(value)) {
22965
22939
  return value instanceof Error ? { message: value.message } : {};
22966
22940
  }
22941
+ const rawDiagnostic = isRecord3(value.browserDiagnostic) ? value.browserDiagnostic : void 0;
22942
+ const protocolDiagnostic = rawDiagnostic ? {
22943
+ ...typeof rawDiagnostic.protocolIssue === "string" ? { protocolIssue: rawDiagnostic.protocolIssue } : {},
22944
+ ...typeof rawDiagnostic.observedEventType === "string" ? { observedEventType: rawDiagnostic.observedEventType } : {},
22945
+ ...typeof rawDiagnostic.clientRequestId === "string" ? { clientRequestId: rawDiagnostic.clientRequestId } : {},
22946
+ ...typeof rawDiagnostic.conversationId === "string" ? { conversationId: rawDiagnostic.conversationId } : {},
22947
+ ...typeof rawDiagnostic.requestId === "string" ? { requestId: rawDiagnostic.requestId } : {},
22948
+ ...typeof rawDiagnostic.streamId === "string" ? { streamId: rawDiagnostic.streamId } : {}
22949
+ } : void 0;
22967
22950
  return {
22968
22951
  code: typeof value.code === "string" ? value.code : void 0,
22969
22952
  message: typeof value.message === "string" ? value.message : void 0,
22970
22953
  correlationId: typeof value.correlationId === "string" ? value.correlationId : void 0,
22971
- details: value.details
22954
+ ...protocolDiagnostic && Object.keys(protocolDiagnostic).length > 0 ? { protocolDiagnostic } : {}
22972
22955
  };
22973
22956
  }
22957
+ function protocolDiagnosticHint(error) {
22958
+ const diagnostic = error.protocolDiagnostic;
22959
+ if (!diagnostic) return error.message;
22960
+ const fields = [
22961
+ diagnostic.protocolIssue ? `protocol issue: ${diagnostic.protocolIssue}` : void 0,
22962
+ diagnostic.observedEventType ? `event: ${diagnostic.observedEventType}` : void 0,
22963
+ error.correlationId ? `correlation ID: ${error.correlationId}` : void 0,
22964
+ diagnostic.clientRequestId ? `client request ID: ${diagnostic.clientRequestId}` : void 0,
22965
+ diagnostic.conversationId ? `conversation ID: ${diagnostic.conversationId}` : void 0,
22966
+ diagnostic.requestId ? `request ID: ${diagnostic.requestId}` : void 0,
22967
+ diagnostic.streamId ? `stream ID: ${diagnostic.streamId}` : void 0
22968
+ ].filter((field) => Boolean(field));
22969
+ return fields.length > 0 ? fields.join("; ") : error.message;
22970
+ }
22974
22971
  async function readJsonBody(response) {
22975
22972
  const text = await response.text();
22976
22973
  if (!text) return {};
@@ -22985,8 +22982,8 @@ async function assertOk(response, action) {
22985
22982
  if (response.ok) return body;
22986
22983
  const canonical = canonicalErrorBody(body);
22987
22984
  const suffix = canonical.correlationId ? ` Correlation ID: ${canonical.correlationId}.` : "";
22988
- throw new CliError(
22989
- CliErrorCodes.ChatRequestFailed,
22985
+ throw new import_CliError.CliError(
22986
+ import_CliError.CliErrorCodes.ChatRequestFailed,
22990
22987
  `${action} failed${canonical.code ? ` (${canonical.code})` : ""}.${suffix}`,
22991
22988
  canonical.message ?? `HTTP ${response.status}`,
22992
22989
  response.status
@@ -22994,11 +22991,11 @@ async function assertOk(response, action) {
22994
22991
  }
22995
22992
  function conversationFromEnvelope(body) {
22996
22993
  if (!isRecord3(body) || body.success !== true || !isRecord3(body.data)) {
22997
- throw new CliError(CliErrorCodes.ChatRequestFailed, "Conversation creation returned an invalid response.");
22994
+ throw new import_CliError.CliError(import_CliError.CliErrorCodes.ChatRequestFailed, "Conversation creation returned an invalid response.");
22998
22995
  }
22999
22996
  const data = body.data;
23000
22997
  if (typeof data.id !== "string" || typeof data.title !== "string") {
23001
- throw new CliError(CliErrorCodes.ChatRequestFailed, "Conversation creation did not return a usable conversation.");
22998
+ throw new import_CliError.CliError(import_CliError.CliErrorCodes.ChatRequestFailed, "Conversation creation did not return a usable conversation.");
23002
22999
  }
23003
23000
  return {
23004
23001
  id: data.id,
@@ -23013,8 +23010,8 @@ function messageFromResponse(response) {
23013
23010
  }
23014
23011
  function requiredIdentity(value, field) {
23015
23012
  if (value) return value;
23016
- throw new CliError(
23017
- CliErrorCodes.ChatRequestFailed,
23013
+ throw new import_CliError.CliError(
23014
+ import_CliError.CliErrorCodes.ChatRequestFailed,
23018
23015
  `Chat stream completed without ${field}.`,
23019
23016
  "The Orchestrator returned an invalid runtime stream."
23020
23017
  );
@@ -23074,8 +23071,8 @@ var XyraRuntimeChatClient = class {
23074
23071
  if (response.status === 404 && canonicalErrorBody(await readJsonBody(response.clone())).code === "XYRA.CONVERSATION.NOT_FOUND") return null;
23075
23072
  const body = await assertOk(response, "Conversation creation lookup");
23076
23073
  const receipt = (0, import_conversation_creation.parseXyraConversationCreationReceiptV1)(isRecord3(body) && body.success === true ? body.data : void 0);
23077
- if (receipt.clientRequestId !== requestId) throw new CliError(
23078
- CliErrorCodes.ChatRequestFailed,
23074
+ if (receipt.clientRequestId !== requestId) throw new import_CliError.CliError(
23075
+ import_CliError.CliErrorCodes.ChatRequestFailed,
23079
23076
  "Conversation creation lookup returned a different request.",
23080
23077
  "Keep the original request unresolved; do not create another conversation."
23081
23078
  );
@@ -23089,24 +23086,15 @@ var XyraRuntimeChatClient = class {
23089
23086
  let userMessageId;
23090
23087
  let assistantMessageId;
23091
23088
  let outcome;
23092
- let waiting = false;
23093
23089
  let response;
23094
23090
  let error;
23095
23091
  let message = "";
23096
23092
  let eventCount = 0;
23097
23093
  let chunkCount = 0;
23098
- const observation = new AbortController();
23099
- const stopObservation = () => observation.abort(options.signal?.reason);
23100
- if (options.signal?.aborted) stopObservation();
23101
- options.signal?.addEventListener("abort", stopObservation, { once: true });
23102
23094
  const captureIdentity = (event) => {
23103
23095
  requestId = event.requestId;
23104
23096
  streamId = event.streamId;
23105
23097
  eventCount += 1;
23106
- if (event.type === "activity" && isRecord3(event.payload) && event.payload.activityId === "working:attention" && event.payload.status === "waiting_confirmation") {
23107
- waiting = true;
23108
- observation.abort();
23109
- }
23110
23098
  if (event.type === "start" && isRecord3(event.payload)) {
23111
23099
  executionId = typeof event.payload.executionId === "string" ? event.payload.executionId : executionId;
23112
23100
  userMessageId = typeof event.payload.userMessageId === "string" ? event.payload.userMessageId : userMessageId;
@@ -23133,25 +23121,22 @@ var XyraRuntimeChatClient = class {
23133
23121
  },
23134
23122
  onTerminal: (payload) => {
23135
23123
  outcome = payload.outcome;
23136
- if (isRecord3(payload) && payload.waiting === true) waiting = true;
23137
23124
  }
23138
23125
  }, {
23139
- signal: observation.signal,
23126
+ signal: options.signal,
23140
23127
  headers: { "x-correlation-id": clientRequestId }
23141
23128
  });
23142
- if (!waiting) outcome = "completed";
23129
+ outcome = "completed";
23143
23130
  } catch (cause) {
23144
- if (!outcome && !waiting) {
23131
+ if (!outcome) {
23145
23132
  const runtimeError = runtimeErrorBody(cause);
23146
- throw new CliError(
23147
- CliErrorCodes.ChatRequestFailed,
23133
+ throw new import_CliError.CliError(
23134
+ import_CliError.CliErrorCodes.ChatRequestFailed,
23148
23135
  `Chat stream failed${runtimeError.code ? ` (${runtimeError.code})` : ""}.`,
23149
- runtimeError.message ?? "The Orchestrator response ended before a valid terminal event."
23136
+ protocolDiagnosticHint(runtimeError) ?? "The Orchestrator response ended before a valid terminal event."
23150
23137
  );
23151
23138
  }
23152
- if (!waiting) error ??= runtimeErrorBody(cause);
23153
- } finally {
23154
- options.signal?.removeEventListener("abort", stopObservation);
23139
+ error ??= runtimeErrorBody(cause);
23155
23140
  }
23156
23141
  const result = {
23157
23142
  schemaVersion: 1,
@@ -23161,8 +23146,7 @@ var XyraRuntimeChatClient = class {
23161
23146
  executionId: requiredIdentity(executionId, "an execution ID"),
23162
23147
  streamId: requiredIdentity(streamId, "a stream ID"),
23163
23148
  userMessageId: requiredIdentity(userMessageId, "a user message ID"),
23164
- outcome: waiting ? "waiting" : outcome ?? "interrupted",
23165
- ...waiting ? { waiting: true } : {},
23149
+ outcome: outcome ?? "interrupted",
23166
23150
  ...assistantMessageId ? { assistantMessageId } : {},
23167
23151
  ...message ? { message } : {},
23168
23152
  ...response ? {
@@ -23178,34 +23162,6 @@ var XyraRuntimeChatClient = class {
23178
23162
  };
23179
23163
  return result;
23180
23164
  }
23181
- async decideAttention(input) {
23182
- const response = await this.httpFetch(new URL("/api/runtime/actions", this.env.orchestratorUrl), {
23183
- method: "POST",
23184
- headers: bearerHeaders(this.accessToken),
23185
- body: JSON.stringify({
23186
- conversationId: input.conversationId,
23187
- surface: "chat",
23188
- action: {
23189
- id: input.action,
23190
- type: `timing.${input.action}`,
23191
- payload: {
23192
- clientRequestId: input.clientRequestId,
23193
- runtimeTurnId: input.runtimeTurnId,
23194
- attentionEpoch: input.epoch,
23195
- actionRequestId: (0, import_node_crypto.randomUUID)()
23196
- }
23197
- },
23198
- turn: {
23199
- schemaVersion: 2,
23200
- interactionProfile: "text",
23201
- transportPreference: "http",
23202
- locale: input.locale,
23203
- localeSource: "platform_i18n"
23204
- }
23205
- })
23206
- });
23207
- return assertOk(response, "Attention choice");
23208
- }
23209
23165
  };
23210
23166
  // Annotate the CommonJS export names for ESM import in node:
23211
23167
  0 && (module.exports = {