PageSourceSearch

https://cdn.infourok.ru/assets/ai/controllers/components/ai-token-…/ai-token-balance_controller-Wa7sDlX.js

js infourok.ru collected 2026-09-24 08:52:10 UTC 7,747 bytes, 211 lines download raw bytes

1function _assert_this_initialized(self) {
2    if (self === void 0) {
3        throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
4    }
5    return self;
6}
7function _class_call_check(instance, Constructor) {
8    if (!(instance instanceof Constructor)) {
9        throw new TypeError("Cannot call a class as a function");
10    }
11}
12function _defineProperties(target, props) {
13    for(var i = 0; i < props.length; i++){
14        var descriptor = props[i];
15        descriptor.enumerable = descriptor.enumerable || false;
16        descriptor.configurable = true;
17        if ("value" in descriptor) descriptor.writable = true;
18        Object.defineProperty(target, descriptor.key, descriptor);
19    }
20}
21function _create_class(Constructor, protoProps, staticProps) {
22    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
23    if (staticProps) _defineProperties(Constructor, staticProps);
24    return Constructor;
25}
26function _define_property(obj, key, value) {
27    if (key in obj) {
28        Object.defineProperty(obj, key, {
29            value: value,
30            enumerable: true,
31            configurable: true,
32            writable: true
33        });
34    } else {
35        obj[key] = value;
36    }
37    return obj;
38}
39function _get_prototype_of(o) {
40    _get_prototype_of = Object.setPrototypeOf ? Object.getPrototypeOf : function getPrototypeOf(o) {
41        return o.__proto__ || Object.getPrototypeOf(o);
42    };
43    return _get_prototype_of(o);
44}
45function _inherits(subClass, superClass) {
46    if (typeof superClass !== "function" && superClass !== null) {
47        throw new TypeError("Super expression must either be null or a function");
48    }
49    subClass.prototype = Object.create(superClass && superClass.prototype, {
50        constructor: {
51            value: subClass,
52            writable: true,
53            configurable: true
54        }
55    });
56    if (superClass) _set_prototype_of(subClass, superClass);
57}
58function _instanceof(left, right) {
59    if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
60        return !!right[Symbol.hasInstance](left);
61    } else {
62        return left instanceof right;
63    }
64}
65function _possible_constructor_return(self, call) {
66    if (call && (_type_of(call) === "object" || typeof call === "function")) {
67        return call;
68    }
69    return _assert_this_initialized(self);
70}
71function _set_prototype_of(o, p) {
72    _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
73        o.__proto__ = p;
74        return o;
75    };
76    return _set_prototype_of(o, p);
77}
78function _type_of(obj) {
79    "@swc/helpers - typeof";
80    return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
81}
82function _is_native_reflect_construct() {
83    if (typeof Reflect === "undefined" || !Reflect.construct) return false;
84    if (Reflect.construct.sham) return false;
85    if (typeof Proxy === "function") return true;
86    try {
87        Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
88        return true;
89    } catch (e) {
90        return false;
91    }
92}
93function _create_super(Derived) {
94    var hasNativeReflectConstruct = _is_native_reflect_construct();
95    return function _createSuperInternal() {
96        var Super = _get_prototype_of(Derived), result;
97        if (hasNativeReflectConstruct) {
98            var NewTarget = _get_prototype_of(this).constructor;
99            result = Reflect.construct(Super, arguments, NewTarget);
100        } else {
101            result = Super.apply(this, arguments);
102        }
103        return _possible_constructor_return(this, result);
104    };
105}
106import { Controller } from "@hotwired/stimulus";
107var TOKENS_UPDATED_EVENT = "streaming-message:tokens-updated";
108var TOKEN_BALANCE_STORAGE_KEY = "ai_token_balance_update";
109var BUTTON_TEXT_CLASS = "iu-button-text";
110var _class = /*#__PURE__*/ function(Controller) {
111    "use strict";
112    _inherits(_class, Controller);
113    var _super = _create_super(_class);
114    function _class() {
115        _class_call_check(this, _class);
116        var _this;
117        _this = _super.apply(this, arguments);
118        _define_property(_assert_this_initialized(_this), "onTokensUpdated", function(event) {
119            var _event_detail;
120            var leftTokens = _this.parseTokenBalance((_event_detail = event.detail) === null || _event_detail === void 0 ? void 0 : _event_detail.leftTokens);
121            if (leftTokens === null) {
122                return;
123            }
124            _this.applyTokenBalance(leftTokens);
125            _this.broadcastTokenBalance(leftTokens);
126        });
127        _define_property(_assert_this_initialized(_this), "onStorage", function(event) {
128            if (event.key !== TOKEN_BALANCE_STORAGE_KEY || !event.newValue) {
129                return;
130            }
131            try {
132                var payload = JSON.parse(event.newValue);
133                var leftTokens = _this.parseTokenBalance(payload.leftTokens);
134                if (leftTokens !== null) {
135                    _this.applyTokenBalance(leftTokens);
136                }
137            } catch (error) {
138                _this.ignoreExpectedError(error);
139            }
140        });
141        return _this;
142    }
143    _create_class(_class, [
144        {
145            key: "connect",
146            value: function connect() {
147                document.addEventListener(TOKENS_UPDATED_EVENT, this.onTokensUpdated);
148                window.addEventListener("storage", this.onStorage);
149            }
150        },
151        {
152            key: "disconnect",
153            value: function disconnect() {
154                document.removeEventListener(TOKENS_UPDATED_EVENT, this.onTokensUpdated);
155                window.removeEventListener("storage", this.onStorage);
156            }
157        },
158        {
159            key: "applyTokenBalance",
160            value: function applyTokenBalance(leftTokens) {
161                var textTarget = this.hasValueTarget ? this.valueTarget : this.element.querySelector(".".concat(BUTTON_TEXT_CLASS));
162                (textTarget !== null && textTarget !== void 0 ? textTarget : this.element).replaceChildren(this.formatTokenBalance(leftTokens));
163            }
164        },
165        {
166            key: "broadcastTokenBalance",
167            value: function broadcastTokenBalance(leftTokens) {
168                try {
169                    window.localStorage.setItem(TOKEN_BALANCE_STORAGE_KEY, JSON.stringify({
170                        leftTokens: leftTokens,
171                        updatedAt: Date.now()
172                    }));
173                } catch (error) {
174                    this.ignoreExpectedError(error);
175                }
176            }
177        },
178        {
179            key: "parseTokenBalance",
180            value: function parseTokenBalance(value) {
181                if (value === null || value === undefined || value === "") {
182                    return null;
183                }
184                var leftTokens = Number(value);
185                if (!Number.isFinite(leftTokens)) {
186                    return null;
187                }
188                return Math.max(0, Math.trunc(leftTokens));
189            }
190        },
191        {
192            key: "formatTokenBalance",
193            value: function formatTokenBalance(leftTokens) {
194                return String(leftTokens).replace(/\B(?=(\d{3})+(?!\d))/g, " ");
195            }
196        },
197        {
198            key: "ignoreExpectedError",
199            value: function ignoreExpectedError(error) {
200                if (_instanceof(error, Error)) {
201                    error.toString();
202                }
203            }
204        }
205    ]);
206    return _class;
207}(Controller);
208_define_property(_class, "targets", [
209    "value"
210]);
211/* stimulusFetch: 'lazy' */ export { _class as default };

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.