mycomesh-consumer 0.1.47 → 0.1.48
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/package.json +1 -1
- package/src/consumer-runtime.mjs +48 -2
- package/src/consumer.mjs +1 -1
package/package.json
CHANGED
package/src/consumer-runtime.mjs
CHANGED
|
@@ -356,6 +356,38 @@ export function inferenceRequestHash({
|
|
|
356
356
|
return `0x${createHash("sha256").update(stableStringify(envelope), "utf8").digest("hex")}`;
|
|
357
357
|
}
|
|
358
358
|
|
|
359
|
+
export function derivePromptCacheKey({ endpoint, model, input, messages, options }) {
|
|
360
|
+
const explicit = options && typeof options === "object" ? options.prompt_cache_key : undefined;
|
|
361
|
+
if (typeof explicit === "string" && explicit.trim()) return explicit.trim();
|
|
362
|
+
const normalizedEndpoint = String(endpoint || "").trim().toLowerCase();
|
|
363
|
+
if (!["responses", "chat"].includes(normalizedEndpoint)) return null;
|
|
364
|
+
const seed = { model: String(model || "") };
|
|
365
|
+
for (const field of ["reasoning", "tool_choice", "tools", "functions", "instructions"]) {
|
|
366
|
+
const value = options && typeof options === "object" ? options[field] : undefined;
|
|
367
|
+
if (value !== undefined && value !== null && value !== "" && !(Array.isArray(value) && value.length === 0)) {
|
|
368
|
+
seed[field] = value;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const source = normalizedEndpoint === "chat" ? (messages ?? [{ role: "user", content: String(input || "") }]) : (input ?? "");
|
|
372
|
+
let firstUser;
|
|
373
|
+
const system = [];
|
|
374
|
+
if (Array.isArray(source)) {
|
|
375
|
+
for (const item of source) {
|
|
376
|
+
if (!item || typeof item !== "object") continue;
|
|
377
|
+
const role = String(item.role || "").trim().toLowerCase();
|
|
378
|
+
if (role === "system" || role === "developer") system.push(item.content);
|
|
379
|
+
if (firstUser === undefined && role === "user") firstUser = item.content;
|
|
380
|
+
if (firstUser === undefined && item.type === "input_text") firstUser = item.text;
|
|
381
|
+
}
|
|
382
|
+
} else if (typeof source === "string" && source.trim()) {
|
|
383
|
+
firstUser = source;
|
|
384
|
+
}
|
|
385
|
+
if (system.length) seed.system = system;
|
|
386
|
+
if (firstUser === undefined || firstUser === null || firstUser === "" || (Array.isArray(firstUser) && !firstUser.length)) return null;
|
|
387
|
+
seed.first_user = firstUser;
|
|
388
|
+
return `myco_csp_${createHash("sha256").update(stableStringify(seed), "utf8").digest("hex")}`;
|
|
389
|
+
}
|
|
390
|
+
|
|
359
391
|
function normalizeInferenceOptions(endpoint, options) {
|
|
360
392
|
if (options === undefined || options === null) return null;
|
|
361
393
|
if (typeof options !== "object" || Array.isArray(options)) {
|
|
@@ -981,8 +1013,22 @@ export class NativeConsumerState {
|
|
|
981
1013
|
try { selected = await this.chooseRelay(used); } catch (error) { lastError = error.message; break; }
|
|
982
1014
|
used.add(selected.relayUrl);
|
|
983
1015
|
try {
|
|
984
|
-
const
|
|
985
|
-
|
|
1016
|
+
const requestBody = {
|
|
1017
|
+
...body,
|
|
1018
|
+
model: selected.health.v8.model || body.model,
|
|
1019
|
+
};
|
|
1020
|
+
if (!String(requestBody.prompt_cache_key || "").trim()) {
|
|
1021
|
+
const cacheKey = derivePromptCacheKey({
|
|
1022
|
+
endpoint: path.endsWith("/chat/completions") ? "chat" : "responses",
|
|
1023
|
+
model: requestBody.model,
|
|
1024
|
+
input: requestBody.input,
|
|
1025
|
+
messages: requestBody.messages,
|
|
1026
|
+
options: requestBody,
|
|
1027
|
+
});
|
|
1028
|
+
if (cacheKey) requestBody.prompt_cache_key = cacheKey;
|
|
1029
|
+
}
|
|
1030
|
+
const payment = this.buildRelayPayment(path, requestBody, selected.health, requestId);
|
|
1031
|
+
requestBody.model = payment.model;
|
|
986
1032
|
const encodedPayment = base64Url(Buffer.from(stableStringify(payment.payment), "utf8"));
|
|
987
1033
|
const response = await fetchWithTimeout(`${selected.relayUrl}${path}`, {
|
|
988
1034
|
method: "POST",
|
package/src/consumer.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
createConsumerServer,
|
|
12
12
|
} from "./consumer-runtime.mjs";
|
|
13
13
|
|
|
14
|
-
export const CONSUMER_RELEASE_VERSION = "0.1.
|
|
14
|
+
export const CONSUMER_RELEASE_VERSION = "0.1.48";
|
|
15
15
|
export const API_COMMANDS = new Set(["health", "models", "responses", "chat"]);
|
|
16
16
|
const API_VALUE_OPTIONS = new Set([
|
|
17
17
|
"--base-url",
|