@boldare/askdare 0.5.2 → 0.5.3

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,11 @@
1
1
  # @boldare/askdare
2
2
 
3
+ ## 0.5.3
4
+
5
+ ### Patch Changes
6
+
7
+ - fe4de07: The token budget reserved before each model call now counts a conversation's history once. Before, after a message in which the bot called tools, the next message reserved the input of all its model calls, about three times the history after one with three calls, so long conversations with tool-using bots ran out of `dailyTokensPerIp` a few turns early. Real usage still replaces the reservation when the message ends, and `onUsage` still reports the summed counts. Nothing to change.
8
+
3
9
  ## 0.5.2
4
10
 
5
11
  ### Patch Changes
package/dist/next.js CHANGED
@@ -270,12 +270,12 @@ function createLimits(options) {
270
270
  reserved += perCall;
271
271
  return true;
272
272
  },
273
- async settle(usage, conversationId) {
273
+ async settle(usage, conversationId, historyTokens = usage.inputTokens) {
274
274
  const actual = budgetTokens(usage);
275
275
  await Promise.all(list.map((budget) => store.hit(budget.key, 2 * DAY, actual - reserved)));
276
276
  const inputKey = `ask:ci:${conversationId}`;
277
277
  const previous = (await store.hit(inputKey, CONVERSATION_TTL, 0)).count;
278
- await store.hit(inputKey, CONVERSATION_TTL, usage.inputTokens - previous);
278
+ await store.hit(inputKey, CONVERSATION_TTL, historyTokens - previous);
279
279
  if (!keys.conversationId) await store.hit(`ask:c:${conversationId}`, CONVERSATION_TTL, 1);
280
280
  }
281
281
  };
@@ -2772,6 +2772,7 @@ function streamTurn(options) {
2772
2772
  let steps = 0;
2773
2773
  let model;
2774
2774
  let usage = ZERO_USAGE;
2775
+ let lastInput;
2775
2776
  const tools = [];
2776
2777
  let finishReason;
2777
2778
  let outcome = "completed";
@@ -2833,11 +2834,12 @@ function streamTurn(options) {
2833
2834
  steps += 1;
2834
2835
  model = step.model ?? model;
2835
2836
  usage = addUsage(usage, step.usage);
2837
+ lastInput = step.usage.inputTokens;
2836
2838
  },
2837
2839
  onToolCall: (call) => tools.push(call)
2838
2840
  });
2839
2841
  finishReason = result.reason;
2840
- options.settle?.(result.usage, conversationId);
2842
+ options.settle?.(result.usage, conversationId, lastInput ?? result.usage.inputTokens);
2841
2843
  const proposed = result.replies && normalizeReplies(result.replies);
2842
2844
  const released = holding ? await releaseAnswer(conversationId, proposed ?? []) : "allowed";
2843
2845
  const replies = released === "allowed" ? proposed : void 0;
@@ -3459,8 +3461,8 @@ function createRequestHandler(config) {
3459
3461
  conversationId: id,
3460
3462
  identityKey: key
3461
3463
  })).token,
3462
- settle: (usage, id) => {
3463
- reservation.settle(usage, id).catch((error) => report(toAskdareError(error)));
3464
+ settle: (usage, id, history) => {
3465
+ reservation.settle(usage, id, history).catch((error) => report(toAskdareError(error)));
3464
3466
  },
3465
3467
  report: config.onUsage,
3466
3468
  issueFeedback: feedbackOn(bot) ? (id, itemId) => feedbackHandle(tokens, bot, {
@@ -1,4 +1,4 @@
1
- /* Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change. */
1
+ /* Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change. */
2
2
  /*
3
3
  * Default skin of the chat (ADR-0008, ADR-0011), in the iframe and inside a page. Everything is
4
4
  * built on the public styling API: the --ask-* tokens (set per bot by the theme stylesheet), the
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import type { ReactElement } from 'react';
3
3
  import { useSkin } from './context';
4
4
  import { SparkIcon } from './icons';
package/dist/ui/chat.tsx CHANGED
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  'use client';
3
3
 
4
4
  // The entry of an ejected copy (`npx @boldare/askdare add ui`): the same DefaultChat as in
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import { useChat } from '@boldare/askdare/react';
3
3
  import { type KeyboardEvent, type ReactElement, useEffect, useId, useRef } from 'react';
4
4
  import { RestartIcon } from './icons';
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import type { ChatBot, Translate } from '@boldare/askdare/react';
3
3
  import { type Context, createContext, type RefObject, useContext } from 'react';
4
4
 
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import { FeedbackButton, useChat, useChatState, useMessage } from '@boldare/askdare/react';
3
3
  import { type FormEvent, type ReactElement, useEffect, useId, useRef, useState } from 'react';
4
4
  import { useSkin } from './context';
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import {
3
3
  AttachButton,
4
4
  Composer,
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import { useChat } from '@boldare/askdare/react';
3
3
  import type { ReactElement } from 'react';
4
4
  import { Avatar } from './avatar';
package/dist/ui/icons.tsx CHANGED
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import type { ReactElement, ReactNode } from 'react';
3
3
 
4
4
  /** Line icons on a 24px grid, drawn with the current text color. */
package/dist/ui/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  'use client';
3
3
 
4
4
  // The default look of the chat (ADR-0011): what `npx @boldare/askdare add ui` copies into an app.
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import {
3
3
  Citations,
4
4
  Message,
package/dist/ui/panel.tsx CHANGED
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import {
3
3
  ErrorMessage,
4
4
  HumanCheck,
@@ -1,4 +1,4 @@
1
- // Ejected from @boldare/askdare@0.5.2 with `npx @boldare/askdare add ui`. This copy is yours to change.
1
+ // Ejected from @boldare/askdare@0.5.3 with `npx @boldare/askdare add ui`. This copy is yours to change.
2
2
  import { useChat } from '@boldare/askdare/react';
3
3
  import type { ReactElement, ReactNode } from 'react';
4
4
  import { Avatar } from './avatar';
package/docs/costs.md CHANGED
@@ -57,7 +57,7 @@ ceiling per bot per 30 days = 60 × highest price per 1M tokens
57
57
 
58
58
  On current models the highest is usually output, and 10 × cached input equals input. Real spending is lower whenever some of the tokens are cheaper input or cached input. The ceiling is what an attacker with ten or more IP addresses can make you pay every day.
59
59
 
60
- Long conversations are mostly cached input: every model call reads the whole conversation again, and the cache serves all of it but the newest part. We measured a 50-turn support conversation with knowledge searches at 97.9 % cached input. It used 3.8M input tokens, cost 0.31 USD, and counts as about 460,000 tokens against the budgets. One visitor can hold a conversation of about 25 turns like it on the default `dailyTokensPerIp`.
60
+ Long conversations are mostly cached input: every model call reads the whole conversation again, and the cache serves all of it but the newest part. We measured a 50-turn support conversation with knowledge searches at 97.9 % cached input. It used 3.8M input tokens, cost 0.31 USD, and counts as about 460,000 tokens against the budgets. One visitor can hold a conversation of about 28 turns like it on the default `dailyTokensPerIp`.
61
61
 
62
62
  ### Speech
63
63
 
@@ -88,7 +88,7 @@ The budgets are checked before each message. A few things let the real number en
88
88
  - **A message can use somewhat more than it reserved.** Before each model call, askdare reserves an estimate of that call against every budget:
89
89
 
90
90
  ```text
91
- one call = input tokens of the previous message in this conversation
91
+ one call = input tokens of the last model call of the previous message
92
92
  + characters of the instructions, message and page context / 2
93
93
  + maxOutputTokens
94
94
  + tokens of the attached files
@@ -170,7 +170,7 @@ OpenAI caches the beginning of prompts automatically on supported models, and ca
170
170
  - Start the instructions with text that is the same for every visitor. Put what varies, such as the locale or a signed-in user's name, at the end.
171
171
  - Keep the tool list stable. askdare already keeps it the same on every model call of a message.
172
172
 
173
- askdare's budgets count cached tokens in full, so they overestimate the cost when caching works. OpenAI's [prompt caching guide](https://developers.openai.com/api/docs/guides/prompt-caching) lists the minimum prompt length that can be cached.
173
+ askdare's budgets count input read from the prompt cache as a tenth of a token, close to its price, so they last longer when caching works ([Tokens](#tokens)). The estimate reserved before each model call still counts the conversation so far as whole tokens, in case the cache has gone cold. OpenAI's [prompt caching guide](https://developers.openai.com/api/docs/guides/prompt-caching) lists the minimum prompt length that can be cached.
174
174
 
175
175
  ## Reply options
176
176
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boldare/askdare",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Embeddable AI chatbot for Next.js, as a script-tag widget or React components, built on the official OpenAI SDK.",
5
5
  "license": "MIT",
6
6
  "author": {