agents
Version:
A home for your AI agents
57 lines (56 loc) • 2.38 kB
JavaScript
import { i as _classPrivateFieldInitSpec, n as _classPrivateFieldSet2, r as _assertClassBrand, t as _classPrivateFieldGet2 } from "./classPrivateFieldGet2-DZBYAB34.js";
import { t as _classPrivateMethodInitSpec } from "./classPrivateMethodInitSpec-qMjJ6sHQ.js";
//#region src/chat/text-segment-joiner.ts
var _hasText = /* @__PURE__ */ new WeakMap();
var _lastTextEndedWithWhitespace = /* @__PURE__ */ new WeakMap();
var _needsBoundarySpace = /* @__PURE__ */ new WeakMap();
var _TextSegmentJoiner_brand = /* @__PURE__ */ new WeakSet();
/**
* @internal Converts structured stream chunks into text and boundary events
* without gluing words across non-text chunks. Sibling-package support for
* Voice and `@cloudflare/think`, not a public API.
*/
var TextSegmentJoiner = class {
constructor() {
_classPrivateMethodInitSpec(this, _TextSegmentJoiner_brand);
_classPrivateFieldInitSpec(this, _hasText, false);
_classPrivateFieldInitSpec(this, _lastTextEndedWithWhitespace, false);
_classPrivateFieldInitSpec(this, _needsBoundarySpace, false);
}
pushChunk(chunk) {
if (chunk.type === "text-delta") {
const text = textFromChunk(chunk);
return text ? _assertClassBrand(_TextSegmentJoiner_brand, this, _pushText).call(this, text) : [];
}
if (typeof chunk.type !== "string" || !_assertClassBrand(_TextSegmentJoiner_brand, this, _markBoundary).call(this)) return [];
return [{ type: "boundary" }];
}
};
function _markBoundary() {
if (!_classPrivateFieldGet2(_hasText, this) || _classPrivateFieldGet2(_needsBoundarySpace, this)) return false;
_classPrivateFieldSet2(_needsBoundarySpace, this, true);
return true;
}
function _pushText(text) {
const events = [];
if (_classPrivateFieldGet2(_needsBoundarySpace, this) && !_classPrivateFieldGet2(_lastTextEndedWithWhitespace, this) && !/^\s/.test(text)) events.push({
type: "text",
text: " "
});
events.push({
type: "text",
text
});
_classPrivateFieldSet2(_hasText, this, true);
_classPrivateFieldSet2(_lastTextEndedWithWhitespace, this, /\s$/.test(text));
_classPrivateFieldSet2(_needsBoundarySpace, this, false);
return events;
}
function textFromChunk(chunk) {
if (typeof chunk.text === "string") return chunk.text;
if (typeof chunk.delta === "string") return chunk.delta;
return null;
}
//#endregion
export { TextSegmentJoiner as t };
//# sourceMappingURL=text-segment-joiner-BtAFQSA_.js.map