@burtson-labs/core-chat 1.0.4
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/LICENSE +201 -0
- package/README.md +45 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/sanitize.d.ts +19 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +119 -0
- package/dist/sanitize.js.map +1 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
+
fee for acceptance of support, warranty, indemnity, or other liability
|
|
168
|
+
obligations and/or rights consistent with this License. However, in
|
|
169
|
+
accepting such obligations, You may act only on Your own behalf
|
|
170
|
+
and on Your sole responsibility, not on behalf of any other Contributor,
|
|
171
|
+
and only if You agree to indemnify, defend, and hold each Contributor
|
|
172
|
+
harmless for any liability incurred by, or claims asserted against,
|
|
173
|
+
such Contributor by reason of your accepting any such warranty or
|
|
174
|
+
additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Burtson Labs
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.burtson.ai/logos/burtson-labs-logo-alt.png" />
|
|
4
|
+
<source media="(prefers-color-scheme: light)" srcset="https://cdn.burtson.ai/logos/burtson-labs-logo.png" />
|
|
5
|
+
<img src="https://cdn.burtson.ai/logos/burtson-labs-logo-alt.png" alt="Burtson Labs" width="150" />
|
|
6
|
+
</picture>
|
|
7
|
+
|
|
8
|
+
# @burtson-labs/core-chat
|
|
9
|
+
|
|
10
|
+
**Shared chat message types and sanitizers used across every Bandit surface.**
|
|
11
|
+
|
|
12
|
+
Tiny package, narrow surface — keeps every host on the same `ChatMessage` shape without dragging in the full runtime.
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## <img src="https://api.iconify.design/lucide/package.svg?color=%23a60ee5&width=22" align="absmiddle"> Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @burtson-labs/core-chat
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## <img src="https://api.iconify.design/lucide/shield-check.svg?color=%23a60ee5&width=22" align="absmiddle"> Status
|
|
24
|
+
|
|
25
|
+
Stable. The type surface is intentionally narrow; expanding it requires coordinated PRs across the hosts that consume it (CLI, VS Code extension, web UI).
|
|
26
|
+
|
|
27
|
+
## <img src="https://api.iconify.design/lucide/zap.svg?color=%23a60ee5&width=22" align="absmiddle"> Quick example
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import type { ChatMessage } from '@burtson-labs/core-chat';
|
|
31
|
+
import { sanitizeModelOutput } from '@burtson-labs/core-chat';
|
|
32
|
+
|
|
33
|
+
const messages: ChatMessage[] = [
|
|
34
|
+
{ role: 'system', content: 'You are Bandit.' },
|
|
35
|
+
{ role: 'user', content: 'Hi.' },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const cleaned = sanitizeModelOutput(rawModelText);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
`sanitizeModelOutput()` is the canonical pass for stripping control tokens, tool-call envelope markup, and other model-emitted artifacts that should never reach the user's screen.
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
[Apache License 2.0](../../LICENSE) — Copyright 2026 Burtson Labs.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Strip base64-of-binary blobs that leak into streamed response text —
|
|
3
|
+
* typically on multimodal turns where the gateway/model echoes the
|
|
4
|
+
* image_url payload, or when a small model recites its own prompt.
|
|
5
|
+
* A blob is 120+ consecutive base64-valid chars with no whitespace,
|
|
6
|
+
* optionally prefixed by `data:<mime>;base64,`. Replaced with a short
|
|
7
|
+
* `[base64 stripped: NNN chars]` marker so the user sees that something
|
|
8
|
+
* was elided without being shown the wall of characters. 120 is
|
|
9
|
+
* conservative — real base64 in code (JWTs, auth tokens, sha refs) is
|
|
10
|
+
* rarely on a single unbroken line past ~100 chars; embedded images
|
|
11
|
+
* are 10k+.
|
|
12
|
+
*/
|
|
13
|
+
export declare function stripBase64Blobs(text: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Remove leaked control tokens and tidy up model output before rendering.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sanitizeModelOutput(text: string | null | undefined): string;
|
|
18
|
+
export declare const controlTokenRegexes: RegExp[];
|
|
19
|
+
//# sourceMappingURL=sanitize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.d.ts","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":"AAgFA;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAgC3E;AAED,eAAO,MAAM,mBAAmB,UAAwB,CAAC"}
|
package/dist/sanitize.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const CONTROL_TOKEN_REGEXES = [
|
|
2
|
+
/<\/?\|?(?:im_start|im_end|start_of_turn|end_of_turn)\|?>/gi,
|
|
3
|
+
/<\s*end_of_turn\s*>/gi,
|
|
4
|
+
/<\s*endofturn\s*>/gi
|
|
5
|
+
];
|
|
6
|
+
const ROLE_PREFIX_REGEX = /(^|\n)\s*(?:user|assistant|system|tool)\s*[::]\s*/gi;
|
|
7
|
+
// Some small models (observed: bandit-core-1 on pburg-bowl, Apr 2026)
|
|
8
|
+
// stream raw HTML instead of markdown — wrapping every sentence in
|
|
9
|
+
// `<p>…</p>` and every identifier in `<code>…</code>`. The markdown-it
|
|
10
|
+
// renderer runs with `html: false`, so those tags arrive in the user's
|
|
11
|
+
// chat as literal escaped text ("<p>Wait, I see <code>foo.ts</code>…</p>").
|
|
12
|
+
// Convert the handful of structural tags we actually see into their
|
|
13
|
+
// markdown equivalents before rendering. Everything else that looks
|
|
14
|
+
// tag-ish gets stripped so it never reaches the user as escaped angle
|
|
15
|
+
// brackets. This is defense-in-depth: the renderer also runs DOMPurify
|
|
16
|
+
// downstream, so even if an exotic tag slips through the markdown pass
|
|
17
|
+
// it cannot execute.
|
|
18
|
+
function convertInlineHtmlToMarkdown(input) {
|
|
19
|
+
let out = input;
|
|
20
|
+
// `<code>…</code>` → `` `…` ``. `[\s\S]*?` so it survives newlines.
|
|
21
|
+
out = out.replace(/<code[^>]*>([\s\S]*?)<\/code>/gi, (_m, inner) => '`' + inner.replace(/`/g, '\\`') + '`');
|
|
22
|
+
// `<pre>…</pre>` → fenced block.
|
|
23
|
+
out = out.replace(/<pre[^>]*>([\s\S]*?)<\/pre>/gi, (_m, inner) => '\n```\n' + inner + '\n```\n');
|
|
24
|
+
// `<strong>` / `<b>` → **bold**
|
|
25
|
+
out = out.replace(/<(strong|b)[^>]*>([\s\S]*?)<\/\1>/gi, (_m, _tag, inner) => `**${inner}**`);
|
|
26
|
+
// `<em>` / `<i>` → *italic*
|
|
27
|
+
out = out.replace(/<(em|i)[^>]*>([\s\S]*?)<\/\1>/gi, (_m, _tag, inner) => `*${inner}*`);
|
|
28
|
+
// Paragraph / break / list structural tags → newlines & bullets.
|
|
29
|
+
out = out.replace(/<br\s*\/?\s*>/gi, '\n');
|
|
30
|
+
out = out.replace(/<\/p\s*>/gi, '\n\n');
|
|
31
|
+
out = out.replace(/<p[^>]*>/gi, '');
|
|
32
|
+
out = out.replace(/<\/?(?:div|section|article|header|footer|main|aside)[^>]*>/gi, '\n');
|
|
33
|
+
out = out.replace(/<li[^>]*>/gi, '- ');
|
|
34
|
+
out = out.replace(/<\/li\s*>/gi, '\n');
|
|
35
|
+
out = out.replace(/<\/?(?:ul|ol)[^>]*>/gi, '\n');
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
// Catch-all: any remaining `<tagname…>` or `</tagname>` pair that the
|
|
39
|
+
// conversions above missed. Stripped rather than converted because we
|
|
40
|
+
// have no sensible markdown equivalent and leaving them as literal text
|
|
41
|
+
// is worse than dropping them. Deliberately narrow (letter-starting
|
|
42
|
+
// tag name) so real `<` in prose ("x < 5") is left alone.
|
|
43
|
+
const STRAY_HTML_TAG_REGEX = /<\/?[a-zA-Z][a-zA-Z0-9-]*(?:\s[^>]*)?>/g;
|
|
44
|
+
// Tool-call envelope (and its content) must be stripped entirely before
|
|
45
|
+
// the generic tag pass runs — otherwise the tags go but the JSON body
|
|
46
|
+
// (`{"name":"apply_edit","params":{"find":"…entire function body…"}}`)
|
|
47
|
+
// stays visible in the chat. Covers both closed blocks and the
|
|
48
|
+
// partial/unclosed form that arrives mid-stream.
|
|
49
|
+
const TOOL_CALL_BLOCK_REGEX = /<tool_call\b[^>]*>[\s\S]*?<\/tool_call\s*>/gi;
|
|
50
|
+
const TOOL_CALL_OPEN_TAIL_REGEX = /<tool_call\b[\s\S]*$/i;
|
|
51
|
+
// Trailing partial tag starter, e.g. `…foo<` or `…foo<tool_ca`, left
|
|
52
|
+
// behind when the stream cuts mid-tag. Without this the user sees a
|
|
53
|
+
// bare `<` hanging off the last visible line until the next chunk
|
|
54
|
+
// arrives.
|
|
55
|
+
const PARTIAL_TAG_TAIL_REGEX = /<\/?[a-zA-Z][a-zA-Z0-9_-]*(?:\s[^>]*)?$|<$/;
|
|
56
|
+
// Gemma-family models (observed: bandit-core:12b-it-qat on pburg-bowl,
|
|
57
|
+
// Apr 2026) frequently emit a bare `>` as the first character of every
|
|
58
|
+
// assistant response — a chat-template artifact that isn't stripped by
|
|
59
|
+
// our existing control-token regex because it's just the closing
|
|
60
|
+
// bracket, not a recognizable tag. Real Markdown blockquotes require
|
|
61
|
+
// a space after the `>` (`> quoted text`), so the safe heuristic is:
|
|
62
|
+
// strip a leading `>` ONLY when it's immediately followed by a
|
|
63
|
+
// non-whitespace character. That preserves intentional blockquotes
|
|
64
|
+
// and removes the Gemma artifact.
|
|
65
|
+
const GEMMA_LEADING_ANGLE_REGEX = /(^|\n)>(?=\S)/g;
|
|
66
|
+
/**
|
|
67
|
+
* Strip base64-of-binary blobs that leak into streamed response text —
|
|
68
|
+
* typically on multimodal turns where the gateway/model echoes the
|
|
69
|
+
* image_url payload, or when a small model recites its own prompt.
|
|
70
|
+
* A blob is 120+ consecutive base64-valid chars with no whitespace,
|
|
71
|
+
* optionally prefixed by `data:<mime>;base64,`. Replaced with a short
|
|
72
|
+
* `[base64 stripped: NNN chars]` marker so the user sees that something
|
|
73
|
+
* was elided without being shown the wall of characters. 120 is
|
|
74
|
+
* conservative — real base64 in code (JWTs, auth tokens, sha refs) is
|
|
75
|
+
* rarely on a single unbroken line past ~100 chars; embedded images
|
|
76
|
+
* are 10k+.
|
|
77
|
+
*/
|
|
78
|
+
export function stripBase64Blobs(text) {
|
|
79
|
+
const BLOB = /(?:data:[\w/.+-]+;base64,)?[A-Za-z0-9+/]{120,}={0,2}/g;
|
|
80
|
+
return text.replace(BLOB, (match) => `[base64 stripped: ${match.length} chars]`);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Remove leaked control tokens and tidy up model output before rendering.
|
|
84
|
+
*/
|
|
85
|
+
export function sanitizeModelOutput(text) {
|
|
86
|
+
if (!text) {
|
|
87
|
+
return "";
|
|
88
|
+
}
|
|
89
|
+
let sanitized = text.replace(/\r\n/g, "\n");
|
|
90
|
+
sanitized = stripBase64Blobs(sanitized);
|
|
91
|
+
for (const regex of CONTROL_TOKEN_REGEXES) {
|
|
92
|
+
sanitized = sanitized.replace(regex, "");
|
|
93
|
+
}
|
|
94
|
+
// Strip complete `<tool_call>…</tool_call>` blocks *with their body*
|
|
95
|
+
// first, then any unclosed tool_call tail left mid-stream. Must run
|
|
96
|
+
// before the generic tag strip below, which would remove the tags
|
|
97
|
+
// and leave the JSON body visible.
|
|
98
|
+
sanitized = sanitized.replace(TOOL_CALL_BLOCK_REGEX, "");
|
|
99
|
+
sanitized = sanitized.replace(TOOL_CALL_OPEN_TAIL_REGEX, "");
|
|
100
|
+
// Convert HTML-wrapped prose into markdown before any other processing
|
|
101
|
+
// so the role-prefix / whitespace passes below operate on the stripped
|
|
102
|
+
// form.
|
|
103
|
+
sanitized = convertInlineHtmlToMarkdown(sanitized);
|
|
104
|
+
sanitized = sanitized.replace(STRAY_HTML_TAG_REGEX, "");
|
|
105
|
+
// Drop any trailing partial tag starter (`…foo<` or `…foo<tool_c`)
|
|
106
|
+
// left by a chunk boundary so the user never sees a bare `<` while
|
|
107
|
+
// streaming.
|
|
108
|
+
sanitized = sanitized.replace(PARTIAL_TAG_TAIL_REGEX, "");
|
|
109
|
+
// Strip the Gemma-family leading `>` artifact (e.g. `>Okay…`) while
|
|
110
|
+
// keeping real blockquotes (`> quoted text`) intact.
|
|
111
|
+
sanitized = sanitized.replace(GEMMA_LEADING_ANGLE_REGEX, "$1");
|
|
112
|
+
sanitized = sanitized.replace(ROLE_PREFIX_REGEX, "$1");
|
|
113
|
+
sanitized = sanitized.replace(/\u00a0/g, " "); // convert non-breaking spaces
|
|
114
|
+
sanitized = sanitized.replace(/[ \t]+\n/g, "\n");
|
|
115
|
+
sanitized = sanitized.replace(/\n{3,}/g, "\n\n");
|
|
116
|
+
return sanitized.trim();
|
|
117
|
+
}
|
|
118
|
+
export const controlTokenRegexes = CONTROL_TOKEN_REGEXES;
|
|
119
|
+
//# sourceMappingURL=sanitize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitize.js","sourceRoot":"","sources":["../src/sanitize.ts"],"names":[],"mappings":"AAAA,MAAM,qBAAqB,GAAa;IACtC,4DAA4D;IAC5D,uBAAuB;IACvB,qBAAqB;CACtB,CAAC;AAEF,MAAM,iBAAiB,GAAG,qDAAqD,CAAC;AAEhF,sEAAsE;AACtE,mEAAmE;AACnE,uEAAuE;AACvE,uEAAuE;AACvE,4EAA4E;AAC5E,oEAAoE;AACpE,oEAAoE;AACpE,sEAAsE;AACtE,uEAAuE;AACvE,uEAAuE;AACvE,qBAAqB;AACrB,SAAS,2BAA2B,CAAC,KAAa;IAChD,IAAI,GAAG,GAAG,KAAK,CAAC;IAChB,oEAAoE;IACpE,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,iCAAiC,EAAE,CAAC,EAAE,EAAE,KAAa,EAAE,EAAE,CACzE,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,GAAG,CACvC,CAAC;IACF,iCAAiC;IACjC,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,+BAA+B,EAAE,CAAC,EAAE,EAAE,KAAa,EAAE,EAAE,CACvE,SAAS,GAAG,KAAK,GAAG,SAAS,CAC9B,CAAC;IACF,gCAAgC;IAChC,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,qCAAqC,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,KAAa,EAAE,EAAE,CACnF,KAAK,KAAK,IAAI,CACf,CAAC;IACF,4BAA4B;IAC5B,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,iCAAiC,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,KAAa,EAAE,EAAE,CAC/E,IAAI,KAAK,GAAG,CACb,CAAC;IACF,iEAAiE;IACjE,GAAG,GAAG,GAAG,CAA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,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,CAAC,CAAC;IACjD,SAAS,GAAG,SAAS,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;IACjD,OAAO,SAAS,CAAC,IAAI,EAAE,CAAC;AAC1B,CAAC;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,qBAAqB,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type ChatMessageRole = "user" | "assistant" | "system" | "tool";
|
|
2
|
+
export interface ChatMessageFileReference {
|
|
3
|
+
path: string;
|
|
4
|
+
repoId?: string | number;
|
|
5
|
+
repoFullName?: string;
|
|
6
|
+
startLine?: number;
|
|
7
|
+
endLine?: number;
|
|
8
|
+
startColumn?: number;
|
|
9
|
+
endColumn?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface ChatMessageMetadata {
|
|
12
|
+
repoId?: string | number;
|
|
13
|
+
repoFullName?: string;
|
|
14
|
+
branch?: string;
|
|
15
|
+
taskId?: string;
|
|
16
|
+
workspaceId?: string;
|
|
17
|
+
spans?: Array<{
|
|
18
|
+
start: number;
|
|
19
|
+
end: number;
|
|
20
|
+
label?: string;
|
|
21
|
+
}>;
|
|
22
|
+
fileReferences?: ChatMessageFileReference[];
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
export interface ChatMessageContextFile {
|
|
26
|
+
path: string;
|
|
27
|
+
source?: "auto" | "manual";
|
|
28
|
+
}
|
|
29
|
+
export interface ChatMessage {
|
|
30
|
+
id?: string;
|
|
31
|
+
role: ChatMessageRole;
|
|
32
|
+
content: string;
|
|
33
|
+
rawModelText?: string;
|
|
34
|
+
metadata?: ChatMessageMetadata;
|
|
35
|
+
feedback?: {
|
|
36
|
+
submitted?: boolean;
|
|
37
|
+
rating?: "up" | "down";
|
|
38
|
+
};
|
|
39
|
+
contextFiles?: ChatMessageContextFile[];
|
|
40
|
+
images?: string[];
|
|
41
|
+
}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AM,CAAC;IACtB,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,QAAQ,CAAC,EAAE;QACT,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,MAAM,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;KACxB,CAAC;IACF,YAAY,CAAC,EAAE,sBAAsB,EAAE,CAAC;IACxC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@burtson-labs/core-chat",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Shared chat types and utilities for Bandit surfaces.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Burtson-Labs/bandit-agent-framework.git",
|
|
11
|
+
"directory": "packages/core-chat"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Burtson-Labs/bandit-agent-framework/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/Burtson-Labs/bandit-agent-framework/tree/main/packages/core-chat#readme",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc -p tsconfig.build.json",
|
|
23
|
+
"dev": "tsc -p tsconfig.build.json --watch",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"lint": "eslint \"src/**/*.ts\""
|
|
26
|
+
}
|
|
27
|
+
}
|