@cldmv/llm-translator 0.6.0 → 0.8.0

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.
Files changed (57) hide show
  1. package/README.md +6 -6
  2. package/dist/api/convert/anthropic/messages/to/openai/chat/request.mjs +11 -11
  3. package/dist/api/convert/anthropic/messages/to/openai/chat/response.mjs +1 -1
  4. package/dist/api/convert/anthropic/messages/to/openai/chat/stream.mjs +1 -1
  5. package/dist/api/convert/openai/chat/to/anthropic/messages/request.mjs +4 -4
  6. package/dist/api/convert/openai/chat/to/anthropic/messages/response.mjs +1 -1
  7. package/dist/api/convert/openai/chat/to/anthropic/messages/stream.mjs +1 -1
  8. package/dist/api/detect.mjs +1 -1
  9. package/dist/api/driver/anthropic/detect.mjs +1 -1
  10. package/dist/api/driver/anthropic/info.mjs +1 -1
  11. package/dist/api/driver/lmstudio/detect.mjs +2 -0
  12. package/dist/api/driver/lmstudio/info.mjs +2 -0
  13. package/dist/api/driver/lmstudio/request.mjs +2 -0
  14. package/dist/api/driver/lmstudio/settings.mjs +2 -0
  15. package/dist/api/driver/mistral/chunk.mjs +2 -0
  16. package/dist/api/driver/mistral/detect.mjs +2 -0
  17. package/dist/api/driver/mistral/info.mjs +2 -0
  18. package/dist/api/driver/mistral/request.mjs +2 -0
  19. package/dist/api/driver/mistral/response.mjs +2 -0
  20. package/dist/api/driver/mistral/settings.mjs +2 -0
  21. package/dist/api/embeddings/send.mjs +1 -1
  22. package/dist/api/request.mjs +2 -2
  23. package/dist/api/send.mjs +1 -1
  24. package/dist/lib/backends.mjs +1 -1
  25. package/dist/lib/fanout.mjs +2 -0
  26. package/dist/lib/finish.mjs +1 -1
  27. package/dist/lib/http.mjs +1 -1
  28. package/dist/lib/ids.mjs +1 -1
  29. package/dist/lib/record.mjs +2 -2
  30. package/dist/lib/routes.mjs +1 -1
  31. package/dist/lib/typed.mjs +2 -0
  32. package/dist/lib/usage.mjs +1 -1
  33. package/package.json +4 -3
  34. package/types/src/api/convert/anthropic/messages/to/openai/chat/request.d.mts +6 -1
  35. package/types/src/api/convert/openai/chat/to/anthropic/messages/request.d.mts +9 -2
  36. package/types/src/api/driver/anthropic/detect.d.mts +10 -23
  37. package/types/src/api/driver/anthropic/info.d.mts +3 -1
  38. package/types/src/api/driver/lmstudio/detect.d.mts +6 -0
  39. package/types/src/api/driver/lmstudio/info.d.mts +31 -0
  40. package/types/src/api/driver/lmstudio/request.d.mts +6 -0
  41. package/types/src/api/driver/lmstudio/settings.d.mts +23 -0
  42. package/types/src/api/driver/mistral/chunk.d.mts +8 -0
  43. package/types/src/api/driver/mistral/detect.d.mts +26 -0
  44. package/types/src/api/driver/mistral/info.d.mts +35 -0
  45. package/types/src/api/driver/mistral/request.d.mts +6 -0
  46. package/types/src/api/driver/mistral/response.d.mts +7 -0
  47. package/types/src/api/driver/mistral/settings.d.mts +24 -0
  48. package/types/src/api/embeddings/send.d.mts +10 -2
  49. package/types/src/api/send.d.mts +7 -0
  50. package/types/src/lib/backends.d.mts +7 -0
  51. package/types/src/lib/fanout.d.mts +68 -0
  52. package/types/src/lib/finish.d.mts +14 -3
  53. package/types/src/lib/http.d.mts +8 -2
  54. package/types/src/lib/ids.d.mts +40 -2
  55. package/types/src/lib/record.d.mts +7 -2
  56. package/types/src/lib/typed.d.mts +10 -0
  57. package/types/src/lib/usage.d.mts +20 -0
@@ -19,13 +19,51 @@ export function completionId(): string;
19
19
  */
20
20
  export function callId(): string;
21
21
  /**
22
- * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). Valid ids pass through unchanged, invalid characters
22
+ * Make a tool-call id valid for Anthropic (`[a-zA-Z0-9_-]+`). An id found in `map` (`fitted -> original`, from a driver's `toolCallIds`
23
+ * policy) is the client's own and is restored first. Otherwise valid ids pass through unchanged, invalid characters
23
24
  * (for example kimi's `functions.name:0`) become `_`, and a missing or empty id is replaced by a fresh one. The
24
25
  * mapping is deterministic so a tool call and its later tool result agree.
25
26
  * @param {unknown} id - Upstream id.
27
+ * @param {Record<string, string>} [map] - `fitted -> original` ids from the conversion state.
26
28
  * @returns {string} Anthropic-valid id.
27
29
  */
28
- export function anthropicToolId(id: unknown): string;
30
+ export function anthropicToolId(id: unknown, map?: Record<string, string>): string;
31
+ /**
32
+ * Whether a driver's `info().toolCallIds` is usable: `{ alphabet, min?, max? }` where `alphabet` is the body of a regular
33
+ * expression character class that allows at least one printable ASCII character (`"a-zA-Z0-9"`), and `min` and `max` are
34
+ * positive integers (`min <= max`) bounding the id length. Mistral's ids are `{ alphabet: "a-zA-Z0-9", min: 9, max: 9 }`.
35
+ * @param {unknown} policy - Value to check.
36
+ * @returns {boolean} True when the policy can be applied.
37
+ */
38
+ export function validToolIdPolicy(policy: unknown): boolean;
39
+ /**
40
+ * Create a tool-call id fitter for an upstream with a strict id format (`info().toolCallIds`, see `validToolIdPolicy`). The translator
41
+ * keeps no state between requests, so the mapping is a pure function of the request: an id that already satisfies the policy passes
42
+ * through unchanged (so history replayed from the upstream's own ids survives), and any other id becomes a hash of itself in the
43
+ * allowed alphabet, the same text on every request. A tool call and its tool result carry the same original and so get the same
44
+ * value. `ids` lists every original id of the request: the conforming ones are reserved first, then each other one takes its hash,
45
+ * and when that text is already taken by a different original the hash is taken again with a counter until it is free. Every
46
+ * rewritten id is recorded in `map` (`fitted -> original`) so the original can be restored when the upstream echoes it back.
47
+ * @param {{ policy: { alphabet: string, min?: number, max?: number }, ids: unknown[], map?: Record<string, string> }} options - Policy, the request's ids and the (JSON-serializable) map to record into.
48
+ * @returns {(id: unknown) => string} Fitter function.
49
+ */
50
+ export function createIdFitter({ policy, ids, map }: {
51
+ policy: {
52
+ alphabet: string;
53
+ min?: number;
54
+ max?: number;
55
+ };
56
+ ids: unknown[];
57
+ map?: Record<string, string>;
58
+ }): (id: unknown) => string;
59
+ /**
60
+ * Original tool-call id for one rewritten by `createIdFitter` (ids that were never rewritten, including ones the upstream made itself,
61
+ * map to themselves).
62
+ * @param {Record<string, string>} map - `fitted -> original` map from the conversion state.
63
+ * @param {string} id - Id seen on the wire.
64
+ * @returns {string} Original id.
65
+ */
66
+ export function restoreId(map: Record<string, string>, id: string): string;
29
67
  /**
30
68
  * Create a tool-name fitter. Names that already satisfy `pattern` and `max` pass through; others are sanitized and,
31
69
  * when too long, shortened with a hash suffix. Every changed name is recorded in `map` (`fitted -> original`) so the
@@ -14,8 +14,8 @@ export function valid(value: any): boolean;
14
14
  /**
15
15
  * Begin the record of one exchange. With `capture` unset this returns a stand-in that does nothing.
16
16
  * @param {{config: any, state: any}} ctx - Translator `{ config, state }`.
17
- * @param {{body: any, sent: any, front: string, requested: string | null, headers?: Record<string, string>, log?: any}} exchange - Client body, the upstream request (result of `request`), front and the logger of the request.
18
- * @returns {{raw: (source: any) => any, events: (source: any) => any, reply: (status: number, headers: Record<string, string>, body: any) => void}} Recorder: `raw` taps the upstream stream bytes, `events` taps the client events and writes the record when the stream ends, `reply` writes the record of a non-stream reply.
17
+ * @param {{body: any, sent: any, front: string, requested: string | null, headers?: Record<string, string>, log?: any, exchange?: string | null, attempt: number}} exchange - Client body, the upstream request (result of `request`), front, the logger of the request, and where this attempt sits: `exchange` is the id of the first attempt of the request (omit it for the first attempt) and `attempt` the 1-based number.
18
+ * @returns {{on: boolean, id: string, raw: (source: any) => any, events: (source: any) => any, reply: (status: number, headers: Record<string, string>, body: any) => void, attempt: (response: any, failover?: string) => void}} Recorder: `id` is the record id (pass it as `exchange` for the later attempts), `raw` taps the upstream stream bytes, `events` taps the client events and writes the record when the stream ends, `reply` writes the record of a non-stream reply, `attempt` writes the record of an attempt that did not produce the reply (`failover` is the reason the request moved on to the next endpoint, absent for the final failure).
19
19
  */
20
20
  export function start({ config, state }: {
21
21
  config: any;
@@ -27,10 +27,15 @@ export function start({ config, state }: {
27
27
  requested: string | null;
28
28
  headers?: Record<string, string>;
29
29
  log?: any;
30
+ exchange?: string | null;
31
+ attempt: number;
30
32
  }): {
33
+ on: boolean;
34
+ id: string;
31
35
  raw: (source: any) => any;
32
36
  events: (source: any) => any;
33
37
  reply: (status: number, headers: Record<string, string>, body: any) => void;
38
+ attempt: (response: any, failover?: string) => void;
34
39
  };
35
40
  /** Most characters of a stream kept per exchange, for the raw upstream text and for the client events each. */
36
41
  export const STREAM_LIMIT: 1000000;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Split typed content chunks into answer text and thinking. `{type:"text"}` chunks make the text, `{type:"thinking"}` chunks make the thinking
3
+ * (their `thinking` field is a list of text chunks, or a string); other chunk types are skipped.
4
+ * @param {any} content - Message or delta `content`.
5
+ * @returns {{ text: string, thinking: string } | null} The parts, or null when `content` is not an array.
6
+ */
7
+ export function flatten(content: any): {
8
+ text: string;
9
+ thinking: string;
10
+ } | null;
@@ -6,6 +6,26 @@
6
6
  * converter writes when the upstream reported nothing and no real reply has).
7
7
  */
8
8
  export function normalize(protocol: "anthropic" | "openai" | "folded", usage: any): Record<string, number | null> | null;
9
+ /**
10
+ * Hand a report to the caller's `onUsage`. An exception or a rejection becomes a warning and never reaches the reply.
11
+ * @param {UsageReport} report - The report.
12
+ * @param {((report: UsageReport) => any) | undefined} onUsage - Caller's callback.
13
+ * @param {(message: string, details: {kind: "threw" | "rejected"}) => void} warn - Receives a message when `onUsage` throws or rejects.
14
+ * @returns {void}
15
+ */
16
+ export function deliver(report: UsageReport, onUsage: ((report: UsageReport) => any) | undefined, warn: (message: string, details: {
17
+ kind: "threw" | "rejected";
18
+ }) => void): void;
19
+ /**
20
+ * Combine the reports of the branches of one fanned-out request (`n` above 1) into the single report the caller gets. Token fields and
21
+ * estimates are summed (null stays null when no branch knew the field), `reported` is true when any branch reported, `complete` only
22
+ * when every branch did, and the reason is the lead branch's (the one that failed first) when there is one, else the first branch that
23
+ * did not finish normally, else `"done"`. The request facts (`model`, `upstream`) come from the first branch that knew them.
24
+ * @param {UsageReport[]} reports - One report per branch, in choice order.
25
+ * @param {number} [lead] - Index of the branch whose failure ended the request, or -1 when none did.
26
+ * @returns {UsageReport} The combined report.
27
+ */
28
+ export function combine(reports: UsageReport[], lead?: number): UsageReport;
9
29
  /**
10
30
  * Create the meter for one request.
11
31
  * @param {{front: "anthropic" | "openai", requested: string | null, model: string | null, upstream: string | null, stream: boolean}} meta - Request facts; `model` and `upstream` may be filled in later.