mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 20:32:20 +03:00
* fix(notion-web): use real picker labels as primary model ids Catalog /v1/models now surfaces web-picker names (fable-5, gpt-5.6-sol) instead of Notion food codenames (acai-budino-high, orange-mousse). Food codenames stay internal via notionCodename + resolveNotionCodename for runInferenceTranscript. Legacy codename requests still work; responses echo the client-facing id. Also points discovery/inference at app.notion.com (same host as the AI picker). Follow-up to #7696. * fix(notion-web): explain plan-locked models like Fable 5 Notion returns Fable 5 (acai-budino-high) with isDisabled=true and disabledReason=business_or_enterprise_plan_required. Keep it out of the enabled catalog (requests would fail) but surface a discovery warning so operators know why it is missing. Also warn when space_id is resolved via getSpaces instead of the cookie. * feat(notion-web): auto-detect workspace without pasting space_id Operators only need the raw token_v2 value. When space_id is omitted: - getSpaces loads all workspaces (browser-like headers + user id) - each workspace is probed via getAvailableModels - the richest AI catalog wins Also softens auth hints so they no longer demand a cookie blob with =. * fix(notion-web): pick Business workspace so Fable 5 is listed Probe ALL workspaces instead of early-exiting on the first catalog with >=8 models. Prefer spaces where Fable is enabled over personal spaces where Notion returns isDisabled=business_or_enterprise_plan_required. Cache the chosen spaceId for inference when cookie has no space_id. * fix(notion-web): working inference + honest token estimates - runInferenceTranscript: createThread+threadId, config/context/user transcript, space/user headers (fixes ValidationError 400) - Parse modern NDJSON patch/record-map; strip lang tags - Estimate usage from text (Notion has no metering); mark estimated - Treat all-zero usage as missing; skip USAGE_TOKEN_BUFFER on estimated - Keep estimated flag through response sanitizer (was stripped -> flat 2000) Verified live: fable-5/gpt-5.6-sol chat 200; usage 7 / 65 not constant 2000. * refactor(notion-web): extract helpers to keep discoverNotionWebModels/execute under the complexity cap Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com> --------- Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com> Co-authored-by: artickc <artickc@users.noreply.github.com> Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
91 lines
3.8 KiB
TypeScript
91 lines
3.8 KiB
TypeScript
// Characterization of applyClientUsageBuffer — the non-streaming usage buffer/estimate block
|
|
// extracted from handleChatCore (chatCore god-file decomposition, #3501). Deps are injected so the
|
|
// buffer-vs-estimate branch and the in-place mutation of translatedResponse.usage are observable.
|
|
// Locks: usage present → buffer+filter; usage absent → estimate from content length; empty content
|
|
// (length 2 from JSON.stringify("")) still estimates; the mutation target is translatedResponse.
|
|
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
const { applyClientUsageBuffer } = await import(
|
|
"../../open-sse/handlers/chatCore/clientUsageBuffer.ts"
|
|
);
|
|
|
|
function makeDeps(overrides: Record<string, unknown> = {}) {
|
|
const calls = { buffer: [] as unknown[], estimate: [] as unknown[], filter: [] as unknown[] };
|
|
const deps = {
|
|
addBufferToUsage: (u: unknown) => {
|
|
calls.buffer.push(u);
|
|
return { ...(u as object), _buffered: true };
|
|
},
|
|
estimateUsage: (...a: unknown[]) => {
|
|
calls.estimate.push(a);
|
|
return { _estimated: true };
|
|
},
|
|
filterUsageForFormat: (u: unknown, _fmt: unknown) => {
|
|
calls.filter.push(u);
|
|
return { ...(u as object), _filtered: true };
|
|
},
|
|
...overrides,
|
|
} as Parameters<typeof applyClientUsageBuffer>[3];
|
|
return { deps, calls };
|
|
}
|
|
|
|
test("usage present → buffer then filter, mutates in place", () => {
|
|
const { deps, calls } = makeDeps();
|
|
const resp: Record<string, unknown> = { usage: { prompt_tokens: 5 } };
|
|
applyClientUsageBuffer(resp, { messages: [] }, "openai", deps);
|
|
assert.equal(calls.buffer.length, 1);
|
|
assert.equal(calls.estimate.length, 0);
|
|
assert.equal((resp.usage as Record<string, unknown>)._buffered, true);
|
|
assert.equal((resp.usage as Record<string, unknown>)._filtered, true);
|
|
});
|
|
|
|
test("all-zero usage stub → estimate (not constant buffer-only 2000)", () => {
|
|
const { deps, calls } = makeDeps();
|
|
const resp: Record<string, unknown> = {
|
|
usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
|
|
choices: [{ message: { content: "PONG" } }],
|
|
};
|
|
applyClientUsageBuffer(resp, { messages: [{ role: "user", content: "hi" }] }, "openai", deps);
|
|
assert.equal(calls.buffer.length, 0, "must not buffer zeros into USAGE_TOKEN_BUFFER");
|
|
assert.equal(calls.estimate.length, 1);
|
|
assert.equal((resp.usage as Record<string, unknown>)._estimated, true);
|
|
});
|
|
|
|
test("no usage but content present → estimate then filter", () => {
|
|
const { deps, calls } = makeDeps();
|
|
const resp: Record<string, unknown> = {
|
|
choices: [{ message: { content: "hello world" } }],
|
|
};
|
|
applyClientUsageBuffer(resp, { messages: [] }, "openai", deps);
|
|
assert.equal(calls.buffer.length, 0);
|
|
assert.equal(calls.estimate.length, 1);
|
|
assert.equal((resp.usage as Record<string, unknown>)._estimated, true);
|
|
assert.equal((resp.usage as Record<string, unknown>)._filtered, true);
|
|
// estimateUsage receives (body, contentLength, format)
|
|
const args = calls.estimate[0] as unknown[];
|
|
assert.equal(args[2], "openai");
|
|
assert.equal(typeof args[1], "number");
|
|
});
|
|
|
|
test("empty content → JSON.stringify('') length 2 > 0 still estimates", () => {
|
|
const { deps, calls } = makeDeps();
|
|
const resp: Record<string, unknown> = {};
|
|
applyClientUsageBuffer(resp, {}, "claude", deps);
|
|
// content "" → JSON.stringify("") = '""' length 2 → contentLength 2 > 0
|
|
assert.equal(calls.estimate.length, 1);
|
|
const args = calls.estimate[0] as unknown[];
|
|
assert.equal(args[1], 2);
|
|
});
|
|
|
|
test("content length is computed from choices[0].message.content", () => {
|
|
const { deps, calls } = makeDeps();
|
|
const resp: Record<string, unknown> = {
|
|
choices: [{ message: { content: "abc" } }],
|
|
};
|
|
applyClientUsageBuffer(resp, {}, "openai", deps);
|
|
// JSON.stringify("abc") = '"abc"' → length 5
|
|
const args = calls.estimate[0] as unknown[];
|
|
assert.equal(args[1], 5);
|
|
});
|