mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-04 19:45:34 +08:00
fix(pi): fail closed on unrepresentable compat spread
This commit is contained in:
@@ -91,6 +91,7 @@ writeFileSync(
|
||||
`export { streamSimple as openaiResponses } from "${PI}/packages/ai/src/api/openai-responses.ts";`,
|
||||
`export { streamSimple as openaiCompletions } from "${PI}/packages/ai/src/api/openai-completions.ts";`,
|
||||
`export { composeModelProvider } from "${PI}/packages/coding-agent/src/core/provider-composer.ts";`,
|
||||
`export { resolveConfigValueOrThrow } from "${PI}/packages/coding-agent/src/core/resolve-config-value.ts";`,
|
||||
].join("\n"),
|
||||
);
|
||||
const bundlePath = join(harnessDirectory, "bundle.mjs");
|
||||
@@ -242,6 +243,115 @@ const compatProvider = adapters.composeModelProvider(
|
||||
);
|
||||
const compatSpread = compatProvider.getModels()[0].compat;
|
||||
|
||||
function jsonSafeJavaScriptValue(value) {
|
||||
if (typeof value === "string") {
|
||||
const codeUnits = Array.from({ length: value.length }, (_, index) =>
|
||||
value.charCodeAt(index),
|
||||
);
|
||||
const hasLoneSurrogate = codeUnits.some((unit, index) => {
|
||||
if (unit >= 0xd800 && unit <= 0xdbff) {
|
||||
return !(
|
||||
index + 1 < codeUnits.length &&
|
||||
codeUnits[index + 1] >= 0xdc00 &&
|
||||
codeUnits[index + 1] <= 0xdfff
|
||||
);
|
||||
}
|
||||
if (unit >= 0xdc00 && unit <= 0xdfff) {
|
||||
return !(
|
||||
index > 0 &&
|
||||
codeUnits[index - 1] >= 0xd800 &&
|
||||
codeUnits[index - 1] <= 0xdbff
|
||||
);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
return hasLoneSurrogate
|
||||
? {
|
||||
$javascriptStringUtf16: codeUnits.map((unit) =>
|
||||
unit.toString(16).padStart(4, "0"),
|
||||
),
|
||||
}
|
||||
: value;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(jsonSafeJavaScriptValue);
|
||||
}
|
||||
if (value && typeof value === "object") {
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).map(([key, child]) => [
|
||||
key,
|
||||
jsonSafeJavaScriptValue(child),
|
||||
]),
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function captureCompatSpread(label, baseValue, overlayValue) {
|
||||
const providerId = `compat-${label}`;
|
||||
const provider = adapters.composeModelProvider(
|
||||
providerId,
|
||||
undefined,
|
||||
{
|
||||
getProvider(candidate) {
|
||||
if (candidate !== providerId) return undefined;
|
||||
return {
|
||||
api: "openai-responses",
|
||||
baseUrl: "https://compat.example/v1",
|
||||
apiKey: "literal",
|
||||
compat: { chatTemplateKwargs: baseValue },
|
||||
models: [{ id: "m" }],
|
||||
modelOverrides: {
|
||||
m: { compat: { chatTemplateKwargs: overlayValue } },
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
undefined,
|
||||
);
|
||||
return {
|
||||
label,
|
||||
baseValue,
|
||||
overlayValue,
|
||||
result: jsonSafeJavaScriptValue(
|
||||
provider.getModels()[0].compat.chatTemplateKwargs,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
const compatEdgeCases = [
|
||||
captureCompatSpread("ascii-string-to-string", "ab", "cd"),
|
||||
captureCompatSpread("astral-string-to-object", "😀", { named: true }),
|
||||
captureCompatSpread("string-to-array", "ab", ["first", "second"]),
|
||||
];
|
||||
|
||||
const resolverInputs = [
|
||||
"literal-secret",
|
||||
"cash$money",
|
||||
"café$literal",
|
||||
"$$literal-$!bang",
|
||||
"prefix-${PI_CAPTURE_MISSING}-suffix",
|
||||
];
|
||||
const resolverCases = resolverInputs.map((input) => {
|
||||
try {
|
||||
return {
|
||||
input,
|
||||
status: "success",
|
||||
result: adapters.resolveConfigValueOrThrow(
|
||||
input,
|
||||
"transport capture",
|
||||
{},
|
||||
),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
input,
|
||||
status: "error",
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
server.close();
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
@@ -252,6 +362,8 @@ console.log(
|
||||
baseUrl,
|
||||
results,
|
||||
compatSpread,
|
||||
compatEdgeCases,
|
||||
resolverCases,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
|
||||
Reference in New Issue
Block a user