@gemies/sdk 10.10.0 → 10.13.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @gemies/sdk
2
2
 
3
+ ## 10.13.0
4
+
5
+ ### A usage limit says when it resets
6
+
7
+ `UsageLimitDetails` carries `resetsAt` (ISO 8601) and `autoResume`. A model provider's usage limit
8
+ (`details.limitType: 'provider_usage'`, exported as `PROVIDER_USAGE_LIMIT_TYPE`) is now raised as a
9
+ `UsageLimitError` of type `rate_limit` instead of a failed turn. When `autoResume` is true the
10
+ server has kept the message and answers it after the reset, so the send ends `sent` and the message
11
+ stays in the thread rather than going back to the composer. The plan window's `details` and
12
+ `suggestedAction` now also arrive on the open stream.
13
+
14
+ ## 10.11.0
15
+
16
+ ### Error rows name the request
17
+
18
+ Every error body and stream error frame of the API now carries `traceId`, the id its logs are
19
+ written under (for a chat turn, the same value as the run `reference`). The line under a failed
20
+ turn ends in `Ref <id>` for a refused request too, after the server's message, and for a request
21
+ that failed outside the turn (`Something went wrong! · Ref req_…`). A turn that failed still reads
22
+ `CODE · Ref <reference>`. `failureDetail` (`@gemies/sdk/model`) takes `message` and `traceId`.
23
+
3
24
  ## 10.10.0
4
25
 
5
26
  ### Comments on diff lines go with the next message
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gemies/sdk",
3
- "version": "10.10.0",
3
+ "version": "10.13.0",
4
4
  "description": "The Gemies SDK: the conversation runtime, chat UI, voice calls and dictation for Oxy apps that talk to a gemie",
5
5
  "main": "src/index.ts",
6
6
  "react-native": "src/index.ts",
@@ -38,5 +38,5 @@ export type {
38
38
  SendOptions,
39
39
  ToolInvocation,
40
40
  } from './types';
41
- export { UsageLimitError } from './usage-limit-error';
41
+ export { PROVIDER_USAGE_LIMIT_TYPE, UsageLimitError } from './usage-limit-error';
42
42
  export type { UsageLimitDetails, UsageLimitType } from './usage-limit-error';
@@ -91,17 +91,34 @@ export function readGemieMeta(chunk: unknown): GemieMeta {
91
91
  }
92
92
 
93
93
  /**
94
- * The line under a failed turn's card: the stable code and the run reference.
94
+ * The trace id of an error body or frame: `error.traceId` in the OpenAI shape, or beside an
95
+ * `error` that is a string.
96
+ */
97
+ export function traceIdOf(body: unknown): string | undefined {
98
+ const outer = body as { error?: unknown; traceId?: unknown } | null | undefined;
99
+ const inner = outer?.error as { traceId?: unknown } | null | undefined;
100
+ const id = inner?.traceId ?? outer?.traceId;
101
+ return typeof id === 'string' && id !== '' ? id : undefined;
102
+ }
103
+
104
+ /**
105
+ * The line under a failed turn's card: what failed, and the id support can trace it by.
95
106
  *
96
- * Never the server's prose. The card's own line is the app's, in the reader's
97
- * language; this is what support needs, and both halves are language-free.
98
- * Empty when the server sent neither.
107
+ * A turn that failed carries a run `reference`, and its line is the stable code and that
108
+ * reference, never the server's prose: the card's own line is the app's, in the reader's
109
+ * language, and this half is language-free. A refusal has no `reference`; its `message` says
110
+ * what to change, so it is kept, followed by the id of the request (`traceId`) when the server
111
+ * sent one. Empty when there is nothing to say.
99
112
  */
100
- export function failureDetail(failure: { code?: string; reference?: string }): string | undefined {
101
- const detail = [
102
- failure.code,
103
- failure.reference === undefined ? undefined : `Ref ${failure.reference}`,
104
- ]
113
+ export function failureDetail(failure: {
114
+ code?: string;
115
+ reference?: string;
116
+ message?: string;
117
+ traceId?: string;
118
+ }): string | undefined {
119
+ const lead = failure.reference === undefined && failure.message ? failure.message : failure.code;
120
+ const id = failure.reference ?? failure.traceId;
121
+ const detail = [lead, id ? `Ref ${id}` : undefined]
105
122
  .filter((value): value is string => typeof value === 'string' && value !== '')
106
123
  .join(' · ');
107
124
  return detail === '' ? undefined : detail;
@@ -11,6 +11,23 @@ export interface UsageLimitDetails {
11
11
  current?: number;
12
12
  limit?: number;
13
13
  tier?: string;
14
+ /** When the limit resets, ISO 8601, when the server knows. */
15
+ resetsAt?: string;
16
+ /** The server keeps the message and answers it by itself once the limit resets. */
17
+ autoResume?: boolean;
18
+ }
19
+
20
+ /** `details.limitType` of a usage limit of the model's provider, not of the person's plan. */
21
+ export const PROVIDER_USAGE_LIMIT_TYPE = 'provider_usage';
22
+
23
+ /** The two fields a refusal says about its reset, read off whatever carried them. */
24
+ export function usageResetOf(
25
+ error: { resetsAt?: unknown; autoResume?: unknown } | null | undefined,
26
+ ): Pick<UsageLimitDetails, 'resetsAt' | 'autoResume'> {
27
+ return {
28
+ ...(typeof error?.resetsAt === 'string' ? { resetsAt: error.resetsAt } : {}),
29
+ ...(error?.autoResume === true ? { autoResume: true } : {}),
30
+ };
14
31
  }
15
32
 
16
33
  export class UsageLimitError extends Error {
@@ -47,9 +47,9 @@ import {
47
47
  parseHostAction,
48
48
  } from './stream-events';
49
49
  import { hasUsableStreamOutput, type StreamOutputEvidence } from './stream-outcome';
50
- import { failureDetail, readGemieMeta, type FailedTurn } from './turn-failure';
50
+ import { failureDetail, readGemieMeta, traceIdOf, type FailedTurn } from './turn-failure';
51
51
  import type { AgentRun, Message, SendOptions, ToolInvocation } from './types';
52
- import { UsageLimitError } from './usage-limit-error';
52
+ import { PROVIDER_USAGE_LIMIT_TYPE, UsageLimitError, usageResetOf } from './usage-limit-error';
53
53
  import { useGemieRealtime } from '../realtime/react';
54
54
 
55
55
  export type { ToolInvocation };
@@ -66,7 +66,8 @@ export type { FailedTurn };
66
66
  * - `failed`: the turn was rolled back to what the list was before the send,
67
67
  * so the caller hands the text back to the composer. Only the usage-limit
68
68
  * errors end this way now: they open a dialog of their own, and a retry
69
- * would be the wrong offer next to "upgrade" or "wait".
69
+ * would be the wrong offer next to "upgrade" or "wait". A limit the server
70
+ * will answer after by itself (`autoResume`) is `sent` instead.
70
71
  * - `aborted`: the person stopped it; partial output is theirs to keep.
71
72
  */
72
73
  export type SendOutcome = 'sent' | 'errored' | 'failed' | 'aborted';
@@ -80,6 +81,8 @@ interface ThrownErrorBody {
80
81
  /** Alia's run id, on a turn that failed after the stream began. */
81
82
  reference?: string;
82
83
  suggestedAction?: 'wait' | 'upgrade';
84
+ resetsAt?: string;
85
+ autoResume?: boolean;
83
86
  }
84
87
 
85
88
  /** Structured `error` object the server may embed in a non-OK response / SSE error event. */
@@ -92,6 +95,11 @@ interface StreamErrorObject {
92
95
  type?: string;
93
96
  /** Alia's run id, on a turn that failed (before any output or part-way). */
94
97
  reference?: string;
98
+ /** The id of the request that produced this error, a refusal's included. */
99
+ traceId?: string;
100
+ /** A usage limit's reset time, and whether the server answers the message then. */
101
+ resetsAt?: string;
102
+ autoResume?: boolean;
95
103
  details?: {
96
104
  limitType?: string;
97
105
  current?: number;
@@ -486,6 +494,18 @@ export function useConversationStream({
486
494
  return 'sent';
487
495
  };
488
496
 
497
+ /**
498
+ * A usage limit's end. When the server keeps the message and answers it after the reset,
499
+ * the person's turn stays in the thread; handing it back to the composer would send it twice.
500
+ */
501
+ const settleUsageLimit = (error: unknown): SendOutcome => {
502
+ const autoResume = (error as Partial<UsageLimitError>).details?.autoResume === true;
503
+ if (!autoResume || hasUsableStreamOutput(outputEvidence)) return settleError();
504
+ dropPendingUpdates();
505
+ setMessagesAndRef((prev) => prev.filter((m) => m.id !== assistantMessage.id));
506
+ return 'sent';
507
+ };
508
+
489
509
  /**
490
510
  * The server's stand-in for an answer it could not get, if one arrived.
491
511
  *
@@ -494,6 +514,8 @@ export function useConversationStream({
494
514
  * read as. The flag is remembered here and answered when the stream ends.
495
515
  */
496
516
  let syntheticTail: { retryable: boolean; detail?: string } | null = null;
517
+ /** The id of a refused request, for the error row of the failure thrown from its body. */
518
+ let refusedTraceId: string | undefined;
497
519
  /** The reply's text as the model produced it: what the host previews when the turn settles. */
498
520
  let replyText = '';
499
521
  // A room can deliberately remain quiet. Only its server frame establishes this,
@@ -864,14 +886,21 @@ export function useConversationStream({
864
886
  return rollback();
865
887
  }
866
888
 
867
- // Detect usage limit errors (429 rate limit, 402 insufficient credits, 403 model access)
868
- if (response.status === 429 || response.status === 402 || response.status === 403) {
869
- const errObj =
870
- errorData?.error && typeof errorData.error === 'object' ? errorData.error : null;
889
+ // Detect usage limit errors (429 rate limit, 402 insufficient credits, 403 model access,
890
+ // and a provider's limit, which fails the turn with 503 and says when it resets)
891
+ const errObj =
892
+ errorData?.error && typeof errorData.error === 'object' ? errorData.error : null;
893
+ const providerLimit = errObj?.details?.limitType === PROVIDER_USAGE_LIMIT_TYPE;
894
+ if (
895
+ providerLimit ||
896
+ response.status === 429 ||
897
+ response.status === 402 ||
898
+ response.status === 403
899
+ ) {
871
900
  const isModelAccess = response.status === 403 && errObj?.code === 'MODEL_NOT_IN_PLAN';
872
901
  const isCredits = response.status === 402 || errObj?.code === 'INSUFFICIENT_CREDITS';
873
902
 
874
- if (isModelAccess || isCredits || response.status === 429) {
903
+ if (providerLimit || isModelAccess || isCredits || response.status === 429) {
875
904
  throw new UsageLimitError({
876
905
  type: isModelAccess ? 'model_access' : isCredits ? 'credits' : 'rate_limit',
877
906
  code:
@@ -896,6 +925,7 @@ export function useConversationStream({
896
925
  current: errObj?.details?.current,
897
926
  limit: errObj?.details?.limit,
898
927
  tier: errObj?.details?.tier,
928
+ ...usageResetOf(errObj),
899
929
  });
900
930
  }
901
931
  }
@@ -924,6 +954,7 @@ export function useConversationStream({
924
954
  failureDetail({
925
955
  code: typeof failed.code === 'string' ? failed.code : undefined,
926
956
  reference: typeof failed.reference === 'string' ? failed.reference : undefined,
957
+ traceId: traceIdOf(errorData),
927
958
  }),
928
959
  );
929
960
  }
@@ -942,6 +973,7 @@ export function useConversationStream({
942
973
  } else {
943
974
  errorMessage = response.statusText || errorMessage;
944
975
  }
976
+ refusedTraceId = traceIdOf(errorData);
945
977
  throw new Error(errorMessage);
946
978
  }
947
979
 
@@ -1306,13 +1338,15 @@ export function useConversationStream({
1306
1338
  host.onFeatureLocked?.(lockedFeature);
1307
1339
  return settleError();
1308
1340
  }
1309
- // Check for usage limit errors (rate limit, credits, model access)
1341
+ // Check for usage limit errors (rate limit, credits, model access, a provider's)
1342
+ const providerLimit = err.details?.limitType === PROVIDER_USAGE_LIMIT_TYPE;
1310
1343
  if (
1344
+ providerLimit ||
1311
1345
  errorCode(err) === 'MODEL_NOT_IN_PLAN' ||
1312
1346
  errorCode(err) === 'INSUFFICIENT_CREDITS' ||
1313
1347
  err.type === 'rate_limit_error'
1314
1348
  ) {
1315
- const isRateLimit = err.type === 'rate_limit_error';
1349
+ const isRateLimit = providerLimit || err.type === 'rate_limit_error';
1316
1350
  throw new UsageLimitError({
1317
1351
  type:
1318
1352
  errorCode(err) === 'MODEL_NOT_IN_PLAN'
@@ -1339,6 +1373,7 @@ export function useConversationStream({
1339
1373
  current:
1340
1374
  typeof err.details?.current === 'number' ? err.details.current : undefined,
1341
1375
  limit: typeof err.details?.limit === 'number' ? err.details.limit : undefined,
1376
+ ...usageResetOf(err),
1342
1377
  });
1343
1378
  }
1344
1379
 
@@ -1352,15 +1387,17 @@ export function useConversationStream({
1352
1387
  // A turn that failed after it started carries a run reference:
1353
1388
  // its card says code and reference, never the server's English
1354
1389
  // prose. A refusal without one (a gate, a bad request) keeps its
1355
- // message, which is the only thing that says what to change.
1356
- const detail =
1357
- typeof err.reference === 'string'
1358
- ? failureDetail({
1359
- code: typeof err.code === 'string' ? err.code : undefined,
1360
- reference: err.reference,
1361
- })
1362
- : getErrorMessage(err) || undefined;
1363
- return keepFailedTurn(err.retryable !== false, detail);
1390
+ // message, which is the only thing that says what to change, with
1391
+ // the request's id after it.
1392
+ return keepFailedTurn(
1393
+ err.retryable !== false,
1394
+ failureDetail({
1395
+ code: typeof err.code === 'string' ? err.code : undefined,
1396
+ reference: typeof err.reference === 'string' ? err.reference : undefined,
1397
+ message: getErrorMessage(err) || undefined,
1398
+ traceId: traceIdOf(parsed),
1399
+ }),
1400
+ );
1364
1401
  }
1365
1402
 
1366
1403
  // Handle usage/credits info (comes at the end of stream).
@@ -1567,7 +1604,7 @@ export function useConversationStream({
1567
1604
  if (e instanceof UsageLimitError || errorName(e) === 'UsageLimitError') {
1568
1605
  setError(e instanceof Error ? e : new Error(getErrorMessage(e)));
1569
1606
  setComposerPhase('error');
1570
- return settleError();
1607
+ return settleUsageLimit(e);
1571
1608
  }
1572
1609
 
1573
1610
  // A streaming fetch may throw a non-Error object (e.g. the response body)
@@ -1597,10 +1634,11 @@ export function useConversationStream({
1597
1634
  retryable: errBody?.retryable ?? !isCredits,
1598
1635
  retryAfterSeconds: errBody?.retryAfter,
1599
1636
  suggestedAction: errBody?.suggestedAction || (isCredits ? 'upgrade' : 'wait'),
1637
+ ...usageResetOf(errBody),
1600
1638
  });
1601
1639
  setError(usageError);
1602
1640
  setComposerPhase('error');
1603
- return settleError();
1641
+ return settleUsageLimit(usageError);
1604
1642
  }
1605
1643
  }
1606
1644
 
@@ -1615,7 +1653,10 @@ export function useConversationStream({
1615
1653
  );
1616
1654
  setError(finalError);
1617
1655
  setComposerPhase('error');
1618
- return keepFailedTurn(true, finalError.message);
1656
+ return keepFailedTurn(
1657
+ true,
1658
+ failureDetail({ message: finalError.message, traceId: refusedTraceId }),
1659
+ );
1619
1660
  } finally {
1620
1661
  // Batches belong to the current turn. An older finally must not flush or clear
1621
1662
  // the next turn's shared timer, even when its controller ref has been cleared.