{"record":{"id":"93dd554814c14d05","repo":"shadcn-ui/ui","slug":"count-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"count must be a non-negative integer.","messagePattern":"count must be a non-negative integer\\.","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"packages/helpers/src/core/chat.ts","lineNumber":729,"sourceCode":"        delayMs,\n        phase: \"before-start\",\n      })\n\n      return api\n    },\n\n    get(count) {\n      const deferredIndex = turns.findIndex(\n        (turn) => turn.resolve !== undefined\n      )\n      const limit = deferredIndex === -1 ? turns.length : deferredIndex\n\n      if (count === undefined) {\n        count = limit\n      }\n\n      if (!Number.isInteger(count) || count < 0) {\n        throw new RangeError(\"count must be a non-negative integer.\")\n      }\n\n      if (deferredIndex !== -1 && count > deferredIndex) {\n        throw new Error(\n          \"get() cannot materialize a continuation turn without a live transcript.\"\n        )\n      }\n\n      return turns\n        .slice(0, count)\n        .map((turn) => cloneValue(turn.message as MESSAGE))\n    },\n\n    next(messages) {\n      const turn = findNextUserTurn(messages)\n\n      return turn?.message ? cloneValue(turn.message) : null\n    },","sourceCodeStart":711,"sourceCodeEnd":747,"githubUrl":"https://github.com/shadcn-ui/ui/blob/efac5987074af84ece57c367c6dd83387b967022/packages/helpers/src/core/chat.ts#L711-L747","documentation":"chat.get(count) validates its optional count argument with Number.isInteger and a >= 0 check, throwing a RangeError otherwise. The count caps how many scripted messages are returned from the transcript; get() takes a number, not an options object.","triggerScenarios":"Passing a negative number, a fraction such as 1.5, NaN, or a non-number coerced badly; accidentally passing an options object like get({ count: 1 }).","commonSituations":"Computed counts from arithmetic that can go negative; off-by-one in slice math; misreading the API as accepting options.","solutions":["Pass an explicit non-negative integer, or omit count to get all non-deferred turns.","Clamp computed counts with Math.max(0, Math.floor(n)).","If you meant options, note get() takes a number directly."],"exampleFix":"// before\nchat.get(-1)           // throws RangeError\nchat.get(1.5)          // throws RangeError\nchat.get({ count: 1 }) // throws RangeError\n\n// after\nchat.get(0)\nchat.get(1)\nchat.get()             // all available turns","handlingStrategy":"validation","validationCode":"function safeGet(chat, count) {\n  if (count === undefined) return chat.get()\n  if (!Number.isInteger(count) || count < 0) {\n    return [] // or throw a domain error\n  }\n  return chat.get(count)\n}","typeGuard":"const isNonNegInt = (n: unknown): n is number =>\n  typeof n === \"number\" && Number.isInteger(n) && n >= 0","tryCatchPattern":"try {\n  chat.get(count)\n} catch (e) {\n  if (e instanceof RangeError) {\n    // fix count and retry, or default to chat.get()\n  } else throw e\n}","preventionTips":["Always derive count with Math.max(0, Math.floor(n)).","Remember get() takes a number, not an options object."],"tags":["chat","validation","api-misuse","range-error"],"backgroundTag":null,"analyzedSha":"efac5987074af84ece57c367c6dd83387b967022","analyzedAt":"2026-08-12T05:00:50.218Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}