@code-yeongyu/senpi 2026.7.14-2 → 2026.7.14
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 +0 -17
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.d.ts.map +1 -1
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.js +5 -14
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.js +2 -3
- package/dist/core/extensions/builtin/terminal/runtime-session.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +0 -1
- package/dist/core/model-registry.js.map +1 -1
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +0 -8
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +3 -3
- package/node_modules/@earendil-works/pi-agent-core/package.json +2 -2
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +23 -2
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js +10 -10
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.js +0 -9
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js +2 -6
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.d.ts +1 -3
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +1 -1
- package/node_modules/@earendil-works/pi-pty/native/prebuilds/darwin-arm64/senpi_pty.darwin-arm64.node +0 -0
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/package.json +6 -6
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.d.ts +0 -8
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.js +0 -92
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.d.ts +0 -6
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.js +0 -54
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.js +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.d.ts +0 -7
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.js +0 -16
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts +0 -19
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js +0 -217
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.js +0 -46
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.d.ts +0 -13
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js +0 -144
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.d.ts +0 -5
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js +0 -208
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.js +0 -17
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.d.ts +0 -5
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.js +0 -34
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.js.map +0 -1
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { coerceParameters } from "./coerce-parameters.js";
|
|
2
|
-
import { findNextInvokeMatch } from "./invoke-match.js";
|
|
3
|
-
import { findIncompleteInvokeOpenTag, findInvokeOpenTag, getSafeInvokeTextLength, scanInvokeBlock, } from "./invoke-tag-scanner.js";
|
|
4
|
-
import { parseAnthropicXmlGeneratedText } from "./parse.js";
|
|
5
|
-
import { ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH, createPendingFragment, } from "./stream-boundary.js";
|
|
6
|
-
import { createToolResolver } from "./tool-resolver.js";
|
|
7
|
-
export { ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH } from "./stream-boundary.js";
|
|
8
|
-
const FUNCTION_CALLS_OPEN_TAG = /<\s*function_calls\s*>/;
|
|
9
|
-
const FUNCTION_CALLS_CLOSE_TAG = /<\s*\/\s*function_calls\s*>/;
|
|
10
|
-
const FUNCTION_CALLS_COMPLETE_TAG = /^<\s*\/?\s*function_calls\s*>/;
|
|
11
|
-
const FUNCTION_CALLS_OPEN_PREFIX = "<function_calls>";
|
|
12
|
-
const FUNCTION_CALLS_CLOSE_PREFIX = "</function_calls>";
|
|
13
|
-
const EAGER_OPEN_TAG_SCAN_LENGTH = 128;
|
|
14
|
-
const RETAINED_FRAGMENT_OVERFLOW_MESSAGE = `Anthropic XML streaming fragment exceeded the ${ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH}-character retained-input limit.`;
|
|
15
|
-
function shouldEmitRawToolCallTextOnError(options) {
|
|
16
|
-
return options?.emitRawToolCallTextOnError === true;
|
|
17
|
-
}
|
|
18
|
-
function emitText(events, text) {
|
|
19
|
-
if (text.length > 0) {
|
|
20
|
-
events.push({ type: "text", text });
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function findFunctionCallsTag(pattern, text, fromIndex) {
|
|
24
|
-
const match = pattern.exec(text.slice(fromIndex));
|
|
25
|
-
if (!match || match.index === undefined) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
return { index: fromIndex + match.index, length: match[0].length };
|
|
29
|
-
}
|
|
30
|
-
function isPotentialFunctionCallsTag(candidate) {
|
|
31
|
-
if (FUNCTION_CALLS_COMPLETE_TAG.test(candidate)) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
const compactCandidate = candidate.replace(/\s/g, "");
|
|
35
|
-
return (FUNCTION_CALLS_OPEN_PREFIX.startsWith(compactCandidate) ||
|
|
36
|
-
FUNCTION_CALLS_CLOSE_PREFIX.startsWith(compactCandidate));
|
|
37
|
-
}
|
|
38
|
-
function getSafeStreamTextLength(text) {
|
|
39
|
-
const scannerSafeLength = getSafeInvokeTextLength(text);
|
|
40
|
-
const lastTagIndex = text.lastIndexOf("<");
|
|
41
|
-
if (lastTagIndex === -1) {
|
|
42
|
-
return scannerSafeLength;
|
|
43
|
-
}
|
|
44
|
-
return isPotentialFunctionCallsTag(text.slice(lastTagIndex))
|
|
45
|
-
? Math.min(scannerSafeLength, lastTagIndex)
|
|
46
|
-
: scannerSafeLength;
|
|
47
|
-
}
|
|
48
|
-
function reportError(options, message, toolCall) {
|
|
49
|
-
options?.onError?.(message, { toolCall });
|
|
50
|
-
}
|
|
51
|
-
export function createAnthropicXmlStreamParser(tools, options) {
|
|
52
|
-
const resolveTool = createToolResolver(tools);
|
|
53
|
-
let buffer = "";
|
|
54
|
-
let nextToolCallIndex = 0;
|
|
55
|
-
let pendingFragment = null;
|
|
56
|
-
function retainPendingFragment(kind) {
|
|
57
|
-
pendingFragment = createPendingFragment(kind, buffer);
|
|
58
|
-
}
|
|
59
|
-
function overflowPendingFragment() {
|
|
60
|
-
const retainedFragment = buffer;
|
|
61
|
-
buffer = "";
|
|
62
|
-
pendingFragment = null;
|
|
63
|
-
reportError(options, RETAINED_FRAGMENT_OVERFLOW_MESSAGE, retainedFragment);
|
|
64
|
-
const events = [];
|
|
65
|
-
if (shouldEmitRawToolCallTextOnError(options)) {
|
|
66
|
-
emitText(events, retainedFragment);
|
|
67
|
-
}
|
|
68
|
-
return events;
|
|
69
|
-
}
|
|
70
|
-
function processBuffer() {
|
|
71
|
-
const events = [];
|
|
72
|
-
pendingFragment = null;
|
|
73
|
-
while (buffer.length > 0) {
|
|
74
|
-
const invokeOpenTag = findInvokeOpenTag(buffer, 0);
|
|
75
|
-
const functionCallsOpenTag = findFunctionCallsTag(FUNCTION_CALLS_OPEN_TAG, buffer, 0);
|
|
76
|
-
if (functionCallsOpenTag && (!invokeOpenTag || functionCallsOpenTag.index <= invokeOpenTag.index)) {
|
|
77
|
-
if (functionCallsOpenTag.index > 0) {
|
|
78
|
-
emitText(events, buffer.slice(0, functionCallsOpenTag.index));
|
|
79
|
-
buffer = buffer.slice(functionCallsOpenTag.index);
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
const functionCallsCloseTag = findFunctionCallsTag(FUNCTION_CALLS_CLOSE_TAG, buffer, functionCallsOpenTag.length);
|
|
83
|
-
if (!functionCallsCloseTag) {
|
|
84
|
-
retainPendingFragment("function-calls");
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
const wrapperEnd = functionCallsCloseTag.index + functionCallsCloseTag.length;
|
|
88
|
-
const wrapperContent = buffer.slice(functionCallsOpenTag.length, functionCallsCloseTag.index);
|
|
89
|
-
if (parseAnthropicXmlGeneratedText(wrapperContent, tools).length > 0) {
|
|
90
|
-
buffer = wrapperContent + buffer.slice(wrapperEnd);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
emitText(events, buffer.slice(0, wrapperEnd));
|
|
94
|
-
buffer = buffer.slice(wrapperEnd);
|
|
95
|
-
}
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
if (invokeOpenTag) {
|
|
99
|
-
if (invokeOpenTag.index > 0) {
|
|
100
|
-
emitText(events, buffer.slice(0, invokeOpenTag.index));
|
|
101
|
-
buffer = buffer.slice(invokeOpenTag.index);
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
const tool = resolveTool(invokeOpenTag.toolName);
|
|
105
|
-
const block = scanInvokeBlock(buffer, invokeOpenTag);
|
|
106
|
-
if (!tool) {
|
|
107
|
-
const nextKnownInvoke = findNextInvokeMatch(buffer, invokeOpenTag.index + invokeOpenTag.length, (toolName) => resolveTool(toolName) !== undefined);
|
|
108
|
-
if (nextKnownInvoke &&
|
|
109
|
-
(block === null || (nextKnownInvoke.block !== null && nextKnownInvoke.block.end === block.end))) {
|
|
110
|
-
emitText(events, buffer.slice(0, nextKnownInvoke.openingTag.index));
|
|
111
|
-
buffer = buffer.slice(nextKnownInvoke.openingTag.index);
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (!block) {
|
|
116
|
-
retainPendingFragment("invoke");
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
const originalCallText = buffer.slice(0, block.end);
|
|
120
|
-
buffer = buffer.slice(block.end);
|
|
121
|
-
if (!tool) {
|
|
122
|
-
emitText(events, originalCallText);
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
const index = nextToolCallIndex;
|
|
126
|
-
nextToolCallIndex += 1;
|
|
127
|
-
const id = `anthropic-xml-tool-${index}`;
|
|
128
|
-
const argumentsRecord = block.parameters ? coerceParameters(block.parameters, tool) : null;
|
|
129
|
-
if (argumentsRecord === null) {
|
|
130
|
-
reportError(options, "Could not process streaming Anthropic XML tool call, keeping original text.", originalCallText);
|
|
131
|
-
if (shouldEmitRawToolCallTextOnError(options)) {
|
|
132
|
-
emitText(events, originalCallText);
|
|
133
|
-
}
|
|
134
|
-
continue;
|
|
135
|
-
}
|
|
136
|
-
events.push({ type: "toolcall_start", index, name: tool.name, id });
|
|
137
|
-
events.push({
|
|
138
|
-
type: "toolcall_delta",
|
|
139
|
-
index,
|
|
140
|
-
argumentsDelta: JSON.stringify(argumentsRecord),
|
|
141
|
-
});
|
|
142
|
-
events.push({ type: "toolcall_end", index, name: tool.name, id, arguments: argumentsRecord });
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
const safeLength = getSafeStreamTextLength(buffer);
|
|
146
|
-
if (safeLength === 0) {
|
|
147
|
-
if (buffer.length >= EAGER_OPEN_TAG_SCAN_LENGTH) {
|
|
148
|
-
retainPendingFragment("open-tag");
|
|
149
|
-
}
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
emitText(events, buffer.slice(0, safeLength));
|
|
153
|
-
buffer = buffer.slice(safeLength);
|
|
154
|
-
}
|
|
155
|
-
return events;
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
feed(textDelta) {
|
|
159
|
-
if (textDelta.length === 0) {
|
|
160
|
-
return [];
|
|
161
|
-
}
|
|
162
|
-
const events = [];
|
|
163
|
-
let offset = 0;
|
|
164
|
-
while (offset < textDelta.length) {
|
|
165
|
-
const capacity = ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH - buffer.length;
|
|
166
|
-
if (capacity === 0) {
|
|
167
|
-
events.push(...overflowPendingFragment());
|
|
168
|
-
continue;
|
|
169
|
-
}
|
|
170
|
-
const nextOffset = Math.min(textDelta.length, offset + capacity);
|
|
171
|
-
const chunk = textDelta.slice(offset, nextOffset);
|
|
172
|
-
offset = nextOffset;
|
|
173
|
-
buffer += chunk;
|
|
174
|
-
if (pendingFragment) {
|
|
175
|
-
if (pendingFragment.matcher.feed(chunk)) {
|
|
176
|
-
events.push(...processBuffer());
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
events.push(...processBuffer());
|
|
181
|
-
}
|
|
182
|
-
if (pendingFragment && buffer.length === ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH) {
|
|
183
|
-
events.push(...overflowPendingFragment());
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return events;
|
|
187
|
-
},
|
|
188
|
-
finish() {
|
|
189
|
-
const events = processBuffer();
|
|
190
|
-
if (buffer.length === 0) {
|
|
191
|
-
return events;
|
|
192
|
-
}
|
|
193
|
-
const invokeOpenTag = findInvokeOpenTag(buffer, 0) ?? findIncompleteInvokeOpenTag(buffer, 0);
|
|
194
|
-
if (invokeOpenTag && invokeOpenTag.index === 0 && resolveTool(invokeOpenTag.toolName)) {
|
|
195
|
-
reportError(options, "Could not complete streaming Anthropic XML tool call at finish.", buffer);
|
|
196
|
-
if (shouldEmitRawToolCallTextOnError(options)) {
|
|
197
|
-
emitText(events, buffer);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
emitText(events, buffer);
|
|
202
|
-
}
|
|
203
|
-
buffer = "";
|
|
204
|
-
return events;
|
|
205
|
-
},
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
//# sourceMappingURL=stream.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/anthropic-xml/stream.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EACN,2BAA2B,EAC3B,iBAAiB,EACjB,uBAAuB,EACvB,eAAe,GACf,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,8BAA8B,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EACN,0CAA0C,EAC1C,qBAAqB,GAErB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAExD,OAAO,EAAE,0CAA0C,EAAE,MAAM,sBAAsB,CAAC;AAElF,MAAM,uBAAuB,GAAG,wBAAwB,CAAC;AACzD,MAAM,wBAAwB,GAAG,6BAA6B,CAAC;AAC/D,MAAM,2BAA2B,GAAG,+BAA+B,CAAC;AACpE,MAAM,0BAA0B,GAAG,kBAAkB,CAAC;AACtD,MAAM,2BAA2B,GAAG,mBAAmB,CAAC;AACxD,MAAM,0BAA0B,GAAG,GAAG,CAAC;AACvC,MAAM,kCAAkC,GAAG,iDAAiD,0CAA0C,kCAAkC,CAAC;AAOzK,SAAS,gCAAgC,CAAC,OAAuB;IAChE,OAAO,OAAO,EAAE,0BAA0B,KAAK,IAAI,CAAC;AACrD,CAAC;AAED,SAAS,QAAQ,CAAC,MAA2B,EAAE,IAAY;IAC1D,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACrB,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,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AAK,0CAA0C,EAAE,CAAC;oBACrF,MAAM,CAAC,IAAI,CAAC,GAAG,uBAAuB,EAAE,CAAC,CAAC;gBAC3C,CAAC;YACF,CAAC;YACD,OAAO,MAAM,CAAC;QACf,CAAC;QACD,MAAM;YACL,MAAM,MAAM,GAAG,aAAa,EAAE,CAAC;YAC/B,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;gBACzB,OAAO,MAAM,CAAC;YACf,CAAC;YAED,MAAM,aAAa,GAAG,iBAAiB,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,2BAA2B,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;YAC7F,IAAI,aAAa,IAAI,aAAa,CAAC,KAAK,KAAK,CAAC,IAAI,WAAW,CAAC,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACvF,WAAW,CAAC,OAAO,EAAE,iEAAiE,EAAE,MAAM,CAAC,CAAC;gBAChG,IAAI,gCAAgC,CAAC,OAAO,CAAC,EAAE,CAAC;oBAC/C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBAC1B,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAC1B,CAAC;YACD,MAAM,GAAG,EAAE,CAAC;YACZ,OAAO,MAAM,CAAC;QACf,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { Tool } from \"../../../types.ts\";\nimport type { ParserOptions, StreamParser, StreamParserEvent } from \"../../types.ts\";\nimport { coerceParameters } from \"./coerce-parameters.ts\";\nimport { findNextInvokeMatch } from \"./invoke-match.ts\";\nimport {\n\tfindIncompleteInvokeOpenTag,\n\tfindInvokeOpenTag,\n\tgetSafeInvokeTextLength,\n\tscanInvokeBlock,\n} from \"./invoke-tag-scanner.ts\";\nimport { parseAnthropicXmlGeneratedText } from \"./parse.ts\";\nimport {\n\tANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH,\n\tcreatePendingFragment,\n\ttype PendingFragment,\n} from \"./stream-boundary.ts\";\nimport { createToolResolver } from \"./tool-resolver.ts\";\n\nexport { ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH } from \"./stream-boundary.ts\";\n\nconst FUNCTION_CALLS_OPEN_TAG = /<\\s*function_calls\\s*>/;\nconst FUNCTION_CALLS_CLOSE_TAG = /<\\s*\\/\\s*function_calls\\s*>/;\nconst FUNCTION_CALLS_COMPLETE_TAG = /^<\\s*\\/?\\s*function_calls\\s*>/;\nconst FUNCTION_CALLS_OPEN_PREFIX = \"<function_calls>\";\nconst FUNCTION_CALLS_CLOSE_PREFIX = \"</function_calls>\";\nconst EAGER_OPEN_TAG_SCAN_LENGTH = 128;\nconst RETAINED_FRAGMENT_OVERFLOW_MESSAGE = `Anthropic XML streaming fragment exceeded the ${ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH}-character retained-input limit.`;\n\ntype FunctionCallsTag = {\n\treadonly index: number;\n\treadonly length: number;\n};\n\nfunction shouldEmitRawToolCallTextOnError(options?: ParserOptions): boolean {\n\treturn options?.emitRawToolCallTextOnError === true;\n}\n\nfunction emitText(events: StreamParserEvent[], text: string): void {\n\tif (text.length > 0) {\n\t\tevents.push({ type: \"text\", text });\n\t}\n}\n\nfunction findFunctionCallsTag(pattern: RegExp, text: string, fromIndex: number): FunctionCallsTag | null {\n\tconst match = pattern.exec(text.slice(fromIndex));\n\tif (!match || match.index === undefined) {\n\t\treturn null;\n\t}\n\n\treturn { index: fromIndex + match.index, length: match[0].length };\n}\n\nfunction isPotentialFunctionCallsTag(candidate: string): boolean {\n\tif (FUNCTION_CALLS_COMPLETE_TAG.test(candidate)) {\n\t\treturn false;\n\t}\n\tconst compactCandidate = candidate.replace(/\\s/g, \"\");\n\treturn (\n\t\tFUNCTION_CALLS_OPEN_PREFIX.startsWith(compactCandidate) ||\n\t\tFUNCTION_CALLS_CLOSE_PREFIX.startsWith(compactCandidate)\n\t);\n}\n\nfunction getSafeStreamTextLength(text: string): number {\n\tconst scannerSafeLength = getSafeInvokeTextLength(text);\n\tconst lastTagIndex = text.lastIndexOf(\"<\");\n\tif (lastTagIndex === -1) {\n\t\treturn scannerSafeLength;\n\t}\n\n\treturn isPotentialFunctionCallsTag(text.slice(lastTagIndex))\n\t\t? Math.min(scannerSafeLength, lastTagIndex)\n\t\t: scannerSafeLength;\n}\n\nfunction reportError(options: ParserOptions | undefined, message: string, toolCall: string): void {\n\toptions?.onError?.(message, { toolCall });\n}\n\nexport function createAnthropicXmlStreamParser(tools: Tool[], options?: ParserOptions): StreamParser {\n\tconst resolveTool = createToolResolver(tools);\n\tlet buffer = \"\";\n\tlet nextToolCallIndex = 0;\n\tlet pendingFragment: PendingFragment | null = null;\n\n\tfunction retainPendingFragment(kind: PendingFragment[\"kind\"]): void {\n\t\tpendingFragment = createPendingFragment(kind, buffer);\n\t}\n\n\tfunction overflowPendingFragment(): StreamParserEvent[] {\n\t\tconst retainedFragment = buffer;\n\t\tbuffer = \"\";\n\t\tpendingFragment = null;\n\t\treportError(options, RETAINED_FRAGMENT_OVERFLOW_MESSAGE, retainedFragment);\n\n\t\tconst events: StreamParserEvent[] = [];\n\t\tif (shouldEmitRawToolCallTextOnError(options)) {\n\t\t\temitText(events, retainedFragment);\n\t\t}\n\t\treturn events;\n\t}\n\n\tfunction processBuffer(): StreamParserEvent[] {\n\t\tconst events: StreamParserEvent[] = [];\n\t\tpendingFragment = null;\n\n\t\twhile (buffer.length > 0) {\n\t\t\tconst invokeOpenTag = findInvokeOpenTag(buffer, 0);\n\t\t\tconst functionCallsOpenTag = findFunctionCallsTag(FUNCTION_CALLS_OPEN_TAG, buffer, 0);\n\n\t\t\tif (functionCallsOpenTag && (!invokeOpenTag || functionCallsOpenTag.index <= invokeOpenTag.index)) {\n\t\t\t\tif (functionCallsOpenTag.index > 0) {\n\t\t\t\t\temitText(events, buffer.slice(0, functionCallsOpenTag.index));\n\t\t\t\t\tbuffer = buffer.slice(functionCallsOpenTag.index);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst functionCallsCloseTag = findFunctionCallsTag(\n\t\t\t\t\tFUNCTION_CALLS_CLOSE_TAG,\n\t\t\t\t\tbuffer,\n\t\t\t\t\tfunctionCallsOpenTag.length,\n\t\t\t\t);\n\t\t\t\tif (!functionCallsCloseTag) {\n\t\t\t\t\tretainPendingFragment(\"function-calls\");\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tconst wrapperEnd = functionCallsCloseTag.index + functionCallsCloseTag.length;\n\t\t\t\tconst wrapperContent = buffer.slice(functionCallsOpenTag.length, functionCallsCloseTag.index);\n\t\t\t\tif (parseAnthropicXmlGeneratedText(wrapperContent, tools).length > 0) {\n\t\t\t\t\tbuffer = wrapperContent + buffer.slice(wrapperEnd);\n\t\t\t\t} else {\n\t\t\t\t\temitText(events, buffer.slice(0, wrapperEnd));\n\t\t\t\t\tbuffer = buffer.slice(wrapperEnd);\n\t\t\t\t}\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif (invokeOpenTag) {\n\t\t\t\tif (invokeOpenTag.index > 0) {\n\t\t\t\t\temitText(events, buffer.slice(0, invokeOpenTag.index));\n\t\t\t\t\tbuffer = buffer.slice(invokeOpenTag.index);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst tool = resolveTool(invokeOpenTag.toolName);\n\t\t\t\tconst block = scanInvokeBlock(buffer, invokeOpenTag);\n\t\t\t\tif (!tool) {\n\t\t\t\t\tconst nextKnownInvoke = findNextInvokeMatch(\n\t\t\t\t\t\tbuffer,\n\t\t\t\t\t\tinvokeOpenTag.index + invokeOpenTag.length,\n\t\t\t\t\t\t(toolName) => resolveTool(toolName) !== undefined,\n\t\t\t\t\t);\n\t\t\t\t\tif (\n\t\t\t\t\t\tnextKnownInvoke &&\n\t\t\t\t\t\t(block === null || (nextKnownInvoke.block !== null && nextKnownInvoke.block.end === block.end))\n\t\t\t\t\t) {\n\t\t\t\t\t\temitText(events, buffer.slice(0, nextKnownInvoke.openingTag.index));\n\t\t\t\t\t\tbuffer = buffer.slice(nextKnownInvoke.openingTag.index);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!block) {\n\t\t\t\t\tretainPendingFragment(\"invoke\");\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\tconst originalCallText = buffer.slice(0, block.end);\n\t\t\t\tbuffer = buffer.slice(block.end);\n\n\t\t\t\tif (!tool) {\n\t\t\t\t\temitText(events, originalCallText);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst index = nextToolCallIndex;\n\t\t\t\tnextToolCallIndex += 1;\n\t\t\t\tconst id = `anthropic-xml-tool-${index}`;\n\t\t\t\tconst argumentsRecord = block.parameters ? coerceParameters(block.parameters, tool) : null;\n\t\t\t\tif (argumentsRecord === null) {\n\t\t\t\t\treportError(\n\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\"Could not process streaming Anthropic XML tool call, keeping original text.\",\n\t\t\t\t\t\toriginalCallText,\n\t\t\t\t\t);\n\t\t\t\t\tif (shouldEmitRawToolCallTextOnError(options)) {\n\t\t\t\t\t\temitText(events, originalCallText);\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tevents.push({ type: \"toolcall_start\", index, name: tool.name, id });\n\t\t\t\tevents.push({\n\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\tindex,\n\t\t\t\t\targumentsDelta: JSON.stringify(argumentsRecord),\n\t\t\t\t});\n\t\t\t\tevents.push({ type: \"toolcall_end\", index, name: tool.name, id, arguments: argumentsRecord });\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tconst safeLength = getSafeStreamTextLength(buffer);\n\t\t\tif (safeLength === 0) {\n\t\t\t\tif (buffer.length >= EAGER_OPEN_TAG_SCAN_LENGTH) {\n\t\t\t\t\tretainPendingFragment(\"open-tag\");\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\temitText(events, buffer.slice(0, safeLength));\n\t\t\tbuffer = buffer.slice(safeLength);\n\t\t}\n\n\t\treturn events;\n\t}\n\n\treturn {\n\t\tfeed(textDelta: string): StreamParserEvent[] {\n\t\t\tif (textDelta.length === 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tlet offset = 0;\n\t\t\twhile (offset < textDelta.length) {\n\t\t\t\tconst capacity = ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH - buffer.length;\n\t\t\t\tif (capacity === 0) {\n\t\t\t\t\tevents.push(...overflowPendingFragment());\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tconst nextOffset = Math.min(textDelta.length, offset + capacity);\n\t\t\t\tconst chunk = textDelta.slice(offset, nextOffset);\n\t\t\t\toffset = nextOffset;\n\t\t\t\tbuffer += chunk;\n\n\t\t\t\tif (pendingFragment) {\n\t\t\t\t\tif (pendingFragment.matcher.feed(chunk)) {\n\t\t\t\t\t\tevents.push(...processBuffer());\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\tevents.push(...processBuffer());\n\t\t\t\t}\n\n\t\t\t\tif (pendingFragment && buffer.length === ANTHROPIC_XML_MAX_RETAINED_FRAGMENT_LENGTH) {\n\t\t\t\t\tevents.push(...overflowPendingFragment());\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn events;\n\t\t},\n\t\tfinish(): StreamParserEvent[] {\n\t\t\tconst events = processBuffer();\n\t\t\tif (buffer.length === 0) {\n\t\t\t\treturn events;\n\t\t\t}\n\n\t\t\tconst invokeOpenTag = findInvokeOpenTag(buffer, 0) ?? findIncompleteInvokeOpenTag(buffer, 0);\n\t\t\tif (invokeOpenTag && invokeOpenTag.index === 0 && resolveTool(invokeOpenTag.toolName)) {\n\t\t\t\treportError(options, \"Could not complete streaming Anthropic XML tool call at finish.\", buffer);\n\t\t\t\tif (shouldEmitRawToolCallTextOnError(options)) {\n\t\t\t\t\temitText(events, buffer);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\temitText(events, buffer);\n\t\t\t}\n\t\t\tbuffer = \"\";\n\t\t\treturn events;\n\t\t},\n\t};\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tool-resolver.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/anthropic-xml/tool-resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,MAAM,MAAM,YAAY,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,GAAG,SAAS,CAAC;AAElE,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,YAAY,CAiBvE"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export function createToolResolver(tools) {
|
|
2
|
-
const exactToolMap = new Map(tools.map((tool) => [tool.name, tool]));
|
|
3
|
-
const insensitiveToolMap = new Map();
|
|
4
|
-
for (const tool of tools) {
|
|
5
|
-
const normalizedName = tool.name.toLowerCase();
|
|
6
|
-
const existing = insensitiveToolMap.get(normalizedName);
|
|
7
|
-
insensitiveToolMap.set(normalizedName, existing === undefined ? tool : existing === tool ? tool : null);
|
|
8
|
-
}
|
|
9
|
-
return (toolName) => {
|
|
10
|
-
const exactTool = exactToolMap.get(toolName);
|
|
11
|
-
if (exactTool) {
|
|
12
|
-
return exactTool;
|
|
13
|
-
}
|
|
14
|
-
return insensitiveToolMap.get(toolName.toLowerCase()) ?? undefined;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=tool-resolver.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"tool-resolver.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/anthropic-xml/tool-resolver.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,kBAAkB,CAAC,KAAsB;IACxD,MAAM,YAAY,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IACrE,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC1D,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;QAC/C,MAAM,QAAQ,GAAG,kBAAkB,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;QACxD,kBAAkB,CAAC,GAAG,CAAC,cAAc,EAAE,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IACzG,CAAC;IAED,OAAO,CAAC,QAAgB,EAAoB,EAAE;QAC7C,MAAM,SAAS,GAAG,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,SAAS,EAAE,CAAC;YACf,OAAO,SAAS,CAAC;QAClB,CAAC;QAED,OAAO,kBAAkB,CAAC,GAAG,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,IAAI,SAAS,CAAC;IACpE,CAAC,CAAC;AACH,CAAC","sourcesContent":["import type { Tool } from \"../../../types.ts\";\n\nexport type ToolResolver = (toolName: string) => Tool | undefined;\n\nexport function createToolResolver(tools: readonly Tool[]): ToolResolver {\n\tconst exactToolMap = new Map(tools.map((tool) => [tool.name, tool]));\n\tconst insensitiveToolMap = new Map<string, Tool | null>();\n\tfor (const tool of tools) {\n\t\tconst normalizedName = tool.name.toLowerCase();\n\t\tconst existing = insensitiveToolMap.get(normalizedName);\n\t\tinsensitiveToolMap.set(normalizedName, existing === undefined ? tool : existing === tool ? tool : null);\n\t}\n\n\treturn (toolName: string): Tool | undefined => {\n\t\tconst exactTool = exactToolMap.get(toolName);\n\t\tif (exactTool) {\n\t\t\treturn exactTool;\n\t\t}\n\n\t\treturn insensitiveToolMap.get(toolName.toLowerCase()) ?? undefined;\n\t};\n}\n"]}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function encodeXmlText(value: string): string;
|
|
2
|
-
export declare function encodeXmlParameterText(value: string): string;
|
|
3
|
-
export declare function encodeXmlAttribute(value: string): string;
|
|
4
|
-
export declare function decodeXmlEntities(value: string): string;
|
|
5
|
-
//# sourceMappingURL=xml-entities.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"xml-entities.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/anthropic-xml/xml-entities.ts"],"names":[],"mappings":"AAAA,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnD;AAMD,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiB5D;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CASvD"}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
export function encodeXmlText(value) {
|
|
2
|
-
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
3
|
-
}
|
|
4
|
-
function encodeXmlLineBreaks(value) {
|
|
5
|
-
return value.replaceAll("\r", " ").replaceAll("\n", " ");
|
|
6
|
-
}
|
|
7
|
-
export function encodeXmlParameterText(value) {
|
|
8
|
-
const encoded = encodeXmlText(value);
|
|
9
|
-
let contentStart = 0;
|
|
10
|
-
while (encoded[contentStart] === "\r" || encoded[contentStart] === "\n") {
|
|
11
|
-
contentStart += 1;
|
|
12
|
-
}
|
|
13
|
-
let contentEnd = encoded.length;
|
|
14
|
-
while (contentEnd > contentStart && (encoded[contentEnd - 1] === "\r" || encoded[contentEnd - 1] === "\n")) {
|
|
15
|
-
contentEnd -= 1;
|
|
16
|
-
}
|
|
17
|
-
return (encodeXmlLineBreaks(encoded.slice(0, contentStart)) +
|
|
18
|
-
encoded.slice(contentStart, contentEnd) +
|
|
19
|
-
encodeXmlLineBreaks(encoded.slice(contentEnd)));
|
|
20
|
-
}
|
|
21
|
-
export function encodeXmlAttribute(value) {
|
|
22
|
-
return encodeXmlText(value).replaceAll('"', """).replaceAll("'", "'");
|
|
23
|
-
}
|
|
24
|
-
export function decodeXmlEntities(value) {
|
|
25
|
-
return value
|
|
26
|
-
.replaceAll(" ", "\r")
|
|
27
|
-
.replaceAll(" ", "\n")
|
|
28
|
-
.replaceAll(""", '"')
|
|
29
|
-
.replaceAll("'", "'")
|
|
30
|
-
.replaceAll("<", "<")
|
|
31
|
-
.replaceAll(">", ">")
|
|
32
|
-
.replaceAll("&", "&");
|
|
33
|
-
}
|
|
34
|
-
//# sourceMappingURL=xml-entities.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"xml-entities.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/anthropic-xml/xml-entities.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,aAAa,CAAC,KAAa;IAC1C,OAAO,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AACvF,CAAC;AAED,SAAS,mBAAmB,CAAC,KAAa;IACzC,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AAClE,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,KAAa;IACnD,MAAM,OAAO,GAAG,aAAa,CAAC,KAAK,C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encodeXmlText(value: string): string {\n\treturn value.replaceAll(\"&\", \"&\").replaceAll(\"<\", \"<\").replaceAll(\">\", \">\");\n}\n\nfunction encodeXmlLineBreaks(value: string): string {\n\treturn value.replaceAll(\"\\r\", \" \").replaceAll(\"\\n\", \" \");\n}\n\nexport function encodeXmlParameterText(value: string): string {\n\tconst encoded = encodeXmlText(value);\n\tlet contentStart = 0;\n\twhile (encoded[contentStart] === \"\\r\" || encoded[contentStart] === \"\\n\") {\n\t\tcontentStart += 1;\n\t}\n\n\tlet contentEnd = encoded.length;\n\twhile (contentEnd > contentStart && (encoded[contentEnd - 1] === \"\\r\" || encoded[contentEnd - 1] === \"\\n\")) {\n\t\tcontentEnd -= 1;\n\t}\n\n\treturn (\n\t\tencodeXmlLineBreaks(encoded.slice(0, contentStart)) +\n\t\tencoded.slice(contentStart, contentEnd) +\n\t\tencodeXmlLineBreaks(encoded.slice(contentEnd))\n\t);\n}\n\nexport function encodeXmlAttribute(value: string): string {\n\treturn encodeXmlText(value).replaceAll('\"', \""\").replaceAll(\"'\", \"'\");\n}\n\nexport function decodeXmlEntities(value: string): string {\n\treturn value\n\t\t.replaceAll(\" \", \"\\r\")\n\t\t.replaceAll(\" \", \"\\n\")\n\t\t.replaceAll(\""\", '\"')\n\t\t.replaceAll(\"'\", \"'\")\n\t\t.replaceAll(\"<\", \"<\")\n\t\t.replaceAll(\">\", \">\")\n\t\t.replaceAll(\"&\", \"&\");\n}\n"]}
|