{"version":3,"file":"thinkingClock.mjs","names":[],"sources":["../../../../../src/plugins/AIComponents/Reasoning/thinkingClock.ts"],"sourcesContent":["import { createContext, useContext } from 'react';\n\n/** How long a first step that arrived done stays open, showing its reasoning, before it folds. */\nexport const FINISHED_REASONING_SHOWN_MS = 1600;\n/** How long the reasoning takes to fold. */\nexport const REASONING_FOLD_MS = 450;\n// How soon after a turn stops thinking its first step has to arrive, done, to open and show its\n// reasoning: it came with the reply that ended the thinking.\nconst ARRIVAL_WINDOW_MS = 1000;\n\ntype Turn = {\n  start: number;\n  end?: number;\n  // How long the first step thought, once it was done, in seconds.\n  firstStepDuration?: number;\n  firstStepStreamed: boolean;\n  // Whether a reply of the turn showed.\n  hadReplies: boolean;\n};\n\n/**\n * Times each turn's thinking, so the reply's first reasoning step takes over the seconds the\n * conversation counted while it showed the agent thinking. The counterpart of `AIThinkingClock`.\n *\n * It is written only from effects (`update`, `firstStepStreams`, `firstStepDone`) and read\n * during render, so a render React throws away changes nothing.\n */\nexport class AIThinkingClock {\n  private turns = new Map<string, Turn>();\n\n  // A turn's thinking starts the first time it is the latest turn and thinking, and ends when it\n  // stops. Thinking that resumes before the first step shows carries on. A turn whose replies were\n  // taken away starts thinking afresh.\n  update(\n    latest: string | undefined,\n    isThinking: boolean,\n    hasReplies: boolean,\n    hasFirstStep: boolean,\n    now = Date.now(),\n  ) {\n    if (latest === undefined) return;\n    const timed = this.turns.get(latest);\n    if (!timed || (isThinking && timed.hadReplies && !hasReplies)) {\n      if (isThinking) {\n        this.turns.set(latest, {\n          firstStepStreamed: false,\n          hadReplies: false,\n          start: now,\n        });\n      }\n      return;\n    }\n    if (hasReplies) timed.hadReplies = true;\n    if (isThinking) {\n      if (!hasFirstStep) timed.end = undefined;\n    } else if (timed.end === undefined) {\n      timed.end = now;\n    }\n  }\n\n  start(turn: string) {\n    return this.turns.get(turn)?.start;\n  }\n\n  firstStepStreams(turn: string) {\n    const timed = this.turns.get(turn);\n    if (timed) timed.firstStepStreamed = true;\n  }\n\n  // The first step stopped streaming: it thought from when the turn started thinking until now,\n  // or for its own time when that is longer.\n  firstStepDone(turn: string, own: number | undefined, now = Date.now()) {\n    const timed = this.turns.get(turn);\n    if (timed && timed.firstStepDuration === undefined) {\n      timed.firstStepDuration = Math.max(own ?? 0, (now - timed.start) / 1000);\n    }\n  }\n\n  // How long a turn's first step thought, in seconds. One that arrived done thought until the\n  // turn stopped thinking, which with no end yet is the render it arrives in; one that streamed,\n  // until `firstStepDone`.\n  durationOfFirstStep(turn: string, own: number | undefined, now = Date.now()) {\n    const timed = this.turns.get(turn);\n    if (!timed) return own;\n    if (timed.firstStepDuration !== undefined) return timed.firstStepDuration;\n    if (timed.firstStepStreamed) return own;\n    return Math.max(own ?? 0, ((timed.end ?? now) - timed.start) / 1000);\n  }\n\n  // Whether a turn's first step, which arrived done with the reply that ends the thinking, opens\n  // to show its reasoning before it folds and the answer starts. The thinking's end is noted\n  // after the render the step arrives in, so no end yet counts as ending now.\n  showsFirstStep(turn: string, now = Date.now()) {\n    const timed = this.turns.get(turn);\n    if (!timed || timed.firstStepStreamed) return false;\n    return timed.end === undefined || now - timed.end < ARRIVAL_WINDOW_MS;\n  }\n}\n\n/** A turn's clock, and the id of its first reasoning step: the one that takes over its thinking. */\nexport type AIThinkingTurn = {\n  clock: AIThinkingClock;\n  key: string;\n  firstStepId?: string;\n};\n\nexport const AIThinkingTurnContext = createContext<AIThinkingTurn | undefined>(undefined);\n\nexport const useAIThinkingTurn = () => useContext(AIThinkingTurnContext);\n"],"mappings":";;;AAGA,IAAa,8BAA8B;AAK3C,IAAM,oBAAoB;;;;;;;;AAmB1B,IAAa,kBAAb,MAA6B;CAC3B,wBAAgB,IAAI,IAAkB;CAKtC,OACE,QACA,YACA,YACA,cACA,MAAM,KAAK,IAAI,GACf;EACA,IAAI,WAAW,KAAA,GAAW;EAC1B,MAAM,QAAQ,KAAK,MAAM,IAAI,MAAM;EACnC,IAAI,CAAC,SAAU,cAAc,MAAM,cAAc,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