mycomesh-consumer 0.1.45 → 0.1.47

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mycomesh-consumer",
3
- "version": "0.1.45",
3
+ "version": "0.1.47",
4
4
  "description": "Native Node.js MycoMesh Consumer and OpenAI-compatible request CLI.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -1005,7 +1005,7 @@ export class NativeConsumerState {
1005
1005
  this.recordReceipt(selected.relayUrl, path, payment.model, settlement);
1006
1006
  headers["PAYMENT-RESPONSE"] = paymentResponse;
1007
1007
  }
1008
- return { payload, status: 200, headers };
1008
+ return { payload: restoreClientResponse(payload, body), status: 200, headers };
1009
1009
  } catch (error) {
1010
1010
  lastError = error.message;
1011
1011
  }
@@ -1029,6 +1029,49 @@ function normalizeOpenAiError(value, fallbackType) {
1029
1029
  return { ...value, error: { ...value.error, message: String(value.error.message || fallbackType), type: String(value.error.type || fallbackType), param: value.error.param ?? null, code: value.error.code || value.error.type || fallbackType } };
1030
1030
  }
1031
1031
 
1032
+ function clientToolSchema(tools, name) {
1033
+ if (!Array.isArray(tools)) return undefined;
1034
+ for (const tool of tools) {
1035
+ if (!tool || typeof tool !== "object") continue;
1036
+ const definition = tool.function && typeof tool.function === "object" ? tool.function : tool;
1037
+ if (definition.name === name && definition.parameters && typeof definition.parameters === "object") return definition.parameters;
1038
+ }
1039
+ return undefined;
1040
+ }
1041
+
1042
+ function orderBySchema(value, schema) {
1043
+ if (Array.isArray(value)) return value.map((item) => orderBySchema(item, schema?.items));
1044
+ if (!value || typeof value !== "object" || Array.isArray(schema)) return value;
1045
+ const properties = schema?.properties;
1046
+ if (!properties || typeof properties !== "object") return value;
1047
+ const ordered = {};
1048
+ for (const key of Object.keys(properties)) {
1049
+ if (Object.prototype.hasOwnProperty.call(value, key)) ordered[key] = orderBySchema(value[key], properties[key]);
1050
+ }
1051
+ for (const key of Object.keys(value)) {
1052
+ if (!Object.prototype.hasOwnProperty.call(ordered, key)) ordered[key] = value[key];
1053
+ }
1054
+ return ordered;
1055
+ }
1056
+
1057
+ function restoreClientResponse(payload, body) {
1058
+ if (!payload || typeof payload !== "object") return payload;
1059
+ if (typeof body.model === "string" && body.model) payload.model = body.model;
1060
+ const calls = [];
1061
+ for (const choice of Array.isArray(payload.choices) ? payload.choices : []) {
1062
+ calls.push(...(Array.isArray(choice?.message?.tool_calls) ? choice.message.tool_calls : []));
1063
+ }
1064
+ calls.push(...(Array.isArray(payload.output) ? payload.output.filter((item) => item?.type === "function_call") : []));
1065
+ for (const call of calls) {
1066
+ const fn = call?.function && typeof call.function === "object" ? call.function : call;
1067
+ if (typeof fn?.name !== "string" || typeof fn.arguments !== "string") continue;
1068
+ const schema = clientToolSchema(body.tools, fn.name);
1069
+ if (!schema) continue;
1070
+ try { fn.arguments = JSON.stringify(orderBySchema(JSON.parse(fn.arguments), schema)); } catch {}
1071
+ }
1072
+ return payload;
1073
+ }
1074
+
1032
1075
  function decodeRequestBody(request) {
1033
1076
  return new Promise((resolve, reject) => {
1034
1077
  let size = 0;
@@ -1360,7 +1403,16 @@ export function responseSse(payload, compact = false) {
1360
1403
  export function chatCompletionSse(payload, includeUsage = false) {
1361
1404
  const id = String(payload?.id || `chatcmpl_${bytesToHex(randomBytes(16))}`); const model = String(payload?.model || ""); const created = Number(payload?.created || Math.floor(Date.now() / 1000)); const chunks = [];
1362
1405
  const chunk = (index, delta, finishReason = null) => chunks.push(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model, choices: [{ index, delta, finish_reason: finishReason }] })}\n\n`);
1363
- (Array.isArray(payload?.choices) ? payload.choices : []).forEach((choice, fallbackIndex) => { const index = Number.isInteger(choice.index) ? choice.index : fallbackIndex; const message = choice.message || {}; chunk(index, { role: String(message.role || "assistant") }); if (message.content) chunk(index, { content: String(message.content) }); chunk(index, {}, choice.finish_reason || "stop"); });
1406
+ (Array.isArray(payload?.choices) ? payload.choices : []).forEach((choice, fallbackIndex) => {
1407
+ const index = Number.isInteger(choice.index) ? choice.index : fallbackIndex;
1408
+ const message = choice.message || {};
1409
+ chunk(index, { role: String(message.role || "assistant") });
1410
+ if (message.content) chunk(index, { content: String(message.content) });
1411
+ (Array.isArray(message.tool_calls) ? message.tool_calls : []).forEach((call, toolIndex) => {
1412
+ chunk(index, { tool_calls: [{ index: toolIndex, id: call.id, type: call.type || "function", function: { name: call.function?.name, arguments: String(call.function?.arguments || "") } }] });
1413
+ });
1414
+ chunk(index, {}, choice.finish_reason || "stop");
1415
+ });
1364
1416
  if (includeUsage && payload?.usage) chunks.push(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model, choices: [], usage: payload.usage })}\n\n`);
1365
1417
  chunks.push("data: [DONE]\n\n"); return chunks;
1366
1418
  }
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.45";
14
+ export const CONSUMER_RELEASE_VERSION = "0.1.47";
15
15
  export const API_COMMANDS = new Set(["health", "models", "responses", "chat"]);
16
16
  const API_VALUE_OPTIONS = new Set([
17
17
  "--base-url",