mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* fix(ai): keep surrogate pairs whole when truncating model context
Head/tail truncation sliced strings by UTF-16 code unit, so a cut could
split an emoji into a lone surrogate. JSON.stringify then emits a
`\udXXX` escape that strict providers reject ("lone leading surrogate in
hex escape"), and because the same cut is reapplied every step, the chat
stays stuck on that 400 for every later request.
- Add textBoundaries helpers (sliceHead, sliceTail, safeSliceBounds) and
use them at every truncation site on the model request path.
- Wrap provider models with a middleware that replaces any remaining lone
surrogate with U+FFFD before the request body is serialized.
* fix(ai): keep own __proto__ keys when normalizing prompt values
toWellFormedValue cloned changed objects by plain assignment, so an own
__proto__ key (e.g. from JSON.parse) hit the inherited setter: the field
was dropped from the request and the clone's prototype changed. Clone
with the source prototype and define own properties instead.
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import type { ToolOutputStore } from './toolOutputStore';
|
|
import { sliceHead, sliceTail } from '../textBoundaries';
|
|
|
|
const LARGE_USER_INPUT_THRESHOLD_CHARS = 25_000;
|
|
const LARGE_USER_INPUT_HEAD_CHARS = 12_000;
|
|
const LARGE_USER_INPUT_TAIL_CHARS = 4_000;
|
|
|
|
const handlesByStore = new WeakMap<ToolOutputStore, Map<string, string>>();
|
|
|
|
function hashInput(input: string): string {
|
|
let hash = 0x811c9dc5;
|
|
for (let index = 0; index < input.length; index += 1) {
|
|
hash ^= input.charCodeAt(index);
|
|
hash = Math.imul(hash, 0x01000193);
|
|
}
|
|
return (hash >>> 0).toString(36);
|
|
}
|
|
|
|
function getStableHandleId(
|
|
input: string,
|
|
chatSessionId: string,
|
|
toolOutputStore: ToolOutputStore,
|
|
): string {
|
|
const handles = handlesByStore.get(toolOutputStore) ?? new Map<string, string>();
|
|
handlesByStore.set(toolOutputStore, handles);
|
|
const key = `${chatSessionId}:${input.length}:${hashInput(input)}`;
|
|
const existingId = handles.get(key);
|
|
if (existingId && toolOutputStore.get(existingId, chatSessionId)) return existingId;
|
|
|
|
const handleId = toolOutputStore.store({
|
|
chatSessionId,
|
|
capabilityId: 'user.input',
|
|
content: input,
|
|
}).id;
|
|
handles.set(key, handleId);
|
|
return handleId;
|
|
}
|
|
|
|
export function fitLargeUserInputForModel(
|
|
input: string,
|
|
chatSessionId: string,
|
|
toolOutputStore: ToolOutputStore,
|
|
): string {
|
|
if (input.length <= LARGE_USER_INPUT_THRESHOLD_CHARS) return input;
|
|
const handleId = getStableHandleId(input, chatSessionId, toolOutputStore);
|
|
return [
|
|
sliceHead(input, LARGE_USER_INPUT_HEAD_CHARS),
|
|
`\n\n[... large user input moved to saved output: ${input.length} chars, handleId=${handleId}. Use tool_output_read with range or search for omitted details ...]\n\n`,
|
|
sliceTail(input, LARGE_USER_INPUT_TAIL_CHARS),
|
|
].join('');
|
|
}
|