llm-stream-assemble 1.6.0 → 1.7.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.
- package/README.md +8 -6
- package/dist/adapters/anthropic.d.cts +1 -1
- package/dist/adapters/anthropic.d.ts +1 -1
- package/dist/adapters/bedrock.d.cts +1 -1
- package/dist/adapters/bedrock.d.ts +1 -1
- package/dist/adapters/cohere.d.cts +1 -1
- package/dist/adapters/cohere.d.ts +1 -1
- package/dist/adapters/gemini.d.cts +1 -1
- package/dist/adapters/gemini.d.ts +1 -1
- package/dist/adapters/openai-chat.cjs +81 -0
- package/dist/adapters/openai-chat.cjs.map +1 -1
- package/dist/adapters/openai-chat.d.cts +1 -1
- package/dist/adapters/openai-chat.d.ts +1 -1
- package/dist/adapters/openai-chat.js +81 -0
- package/dist/adapters/openai-chat.js.map +1 -1
- package/dist/adapters/openai-compatible.cjs +81 -0
- package/dist/adapters/openai-compatible.cjs.map +1 -1
- package/dist/adapters/openai-compatible.d.cts +1 -1
- package/dist/adapters/openai-compatible.d.ts +1 -1
- package/dist/adapters/openai-compatible.js +81 -0
- package/dist/adapters/openai-compatible.js.map +1 -1
- package/dist/adapters/openai-responses.d.cts +1 -1
- package/dist/adapters/openai-responses.d.ts +1 -1
- package/dist/core/index.cjs +6 -0
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +2 -2
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -1
- package/dist/index.cjs +146 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +44 -3
- package/dist/index.d.ts +44 -3
- package/dist/index.js +144 -2
- package/dist/index.js.map +1 -1
- package/dist/{types-CSHQ-Alb.d.cts → types-JvwK2_FV.d.cts} +31 -0
- package/dist/{types-CSHQ-Alb.d.ts → types-JvwK2_FV.d.ts} +31 -0
- package/package.json +2 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as StreamEvent, C as CollectedStream, T as ToSSEOptions, d as StreamEventHandlers } from './types-
|
|
2
|
-
export { A as AssembleFromFileOptions, a as AssembleOptions, F as FinishReason, P as PartialJSONResult, R as RawChunk, b as ReasoningVariant, S as StreamAdapter, e as StreamEventType } from './types-
|
|
1
|
+
import { c as StreamEvent, C as CollectedStream, T as ToSSEOptions, d as StreamEventHandlers } from './types-JvwK2_FV.cjs';
|
|
2
|
+
export { A as AssembleFromFileOptions, a as AssembleOptions, F as FinishReason, P as PartialJSONResult, R as RawChunk, b as ReasoningVariant, S as StreamAdapter, e as StreamEventType } from './types-JvwK2_FV.cjs';
|
|
3
3
|
export { assembleFromFile, assembleFromPayloads, assembleResponse, assembleStream, createAssemblyTransform, parsePartialJSON, parseSSE } from './core/index.cjs';
|
|
4
4
|
export { OpenAIChatAdapterOptions, openaiChatAdapter } from './adapters/openai-chat.cjs';
|
|
5
5
|
export { HOST_COMPATIBLE_PRESETS, LOOSE_HOST_PRESETS, OPENAI_COMPATIBLE_PROVIDERS, OpenAICompatibleAdapterOptions, OpenAICompatibleProvider, PRESET_OVERRIDES, PRESET_OVERRIDE_KEYS, ResolvedCompatibleAdapterConfig, STRICT_COMPATIBLE_PRESETS, compatibleProviderLabel, hasPresetOverride, isStrictCompatiblePreset, openaiCompatibleAdapter, providerPreset, resolveCompatibleAdapterConfig } from './adapters/openai-compatible.cjs';
|
|
@@ -31,6 +31,44 @@ interface CitationSpanAnchorResult {
|
|
|
31
31
|
}
|
|
32
32
|
declare function citationSpanAnchor(input: CitationSpanAnchorInput): CitationSpanAnchorResult;
|
|
33
33
|
|
|
34
|
+
interface LogprobConfidenceInput {
|
|
35
|
+
logprob: number;
|
|
36
|
+
topLogprobs?: Array<{
|
|
37
|
+
token: string;
|
|
38
|
+
logprob: number;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
interface LogprobConfidenceResult {
|
|
42
|
+
probability?: number;
|
|
43
|
+
margin?: number;
|
|
44
|
+
runnerUpToken?: string;
|
|
45
|
+
}
|
|
46
|
+
declare function logprobConfidence(input: LogprobConfidenceInput): LogprobConfidenceResult;
|
|
47
|
+
|
|
48
|
+
interface LogprobTextAlignmentInput {
|
|
49
|
+
assistantText: string;
|
|
50
|
+
logprobs: Array<{
|
|
51
|
+
token: string;
|
|
52
|
+
logprob: number;
|
|
53
|
+
position?: number;
|
|
54
|
+
choiceIndex?: number;
|
|
55
|
+
channel?: "content" | "refusal";
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
interface LogprobTextAlignmentEntry {
|
|
59
|
+
logprob: number;
|
|
60
|
+
token: string;
|
|
61
|
+
start: number;
|
|
62
|
+
end: number;
|
|
63
|
+
consistent: boolean;
|
|
64
|
+
position?: number;
|
|
65
|
+
}
|
|
66
|
+
interface LogprobTextAlignmentResult {
|
|
67
|
+
entries: LogprobTextAlignmentEntry[];
|
|
68
|
+
unaligned: number;
|
|
69
|
+
}
|
|
70
|
+
declare function alignLogprobsWithText(input: LogprobTextAlignmentInput): LogprobTextAlignmentResult;
|
|
71
|
+
|
|
34
72
|
declare function isMessageStart(event: StreamEvent): event is Extract<StreamEvent, {
|
|
35
73
|
type: "message.start";
|
|
36
74
|
}>;
|
|
@@ -76,6 +114,9 @@ declare function isCitation(event: StreamEvent): event is Extract<StreamEvent, {
|
|
|
76
114
|
declare function isGrounding(event: StreamEvent): event is Extract<StreamEvent, {
|
|
77
115
|
type: "grounding";
|
|
78
116
|
}>;
|
|
117
|
+
declare function isLogprob(event: StreamEvent): event is Extract<StreamEvent, {
|
|
118
|
+
type: "logprob";
|
|
119
|
+
}>;
|
|
79
120
|
declare function isUsage(event: StreamEvent): event is Extract<StreamEvent, {
|
|
80
121
|
type: "usage";
|
|
81
122
|
}>;
|
|
@@ -86,4 +127,4 @@ declare function isError(event: StreamEvent): event is Extract<StreamEvent, {
|
|
|
86
127
|
type: "error";
|
|
87
128
|
}>;
|
|
88
129
|
|
|
89
|
-
export { type CitationSpanAnchorInput, type CitationSpanAnchorResult, CollectedStream, StreamEvent, StreamEventHandlers, ToSSEOptions, citationSpanAnchor, collectStream, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, matchEvent, tapEvents, toSSE };
|
|
130
|
+
export { type CitationSpanAnchorInput, type CitationSpanAnchorResult, CollectedStream, type LogprobConfidenceInput, type LogprobConfidenceResult, type LogprobTextAlignmentEntry, type LogprobTextAlignmentInput, type LogprobTextAlignmentResult, StreamEvent, StreamEventHandlers, ToSSEOptions, alignLogprobsWithText, citationSpanAnchor, collectStream, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isLogprob, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, logprobConfidence, matchEvent, tapEvents, toSSE };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as StreamEvent, C as CollectedStream, T as ToSSEOptions, d as StreamEventHandlers } from './types-
|
|
2
|
-
export { A as AssembleFromFileOptions, a as AssembleOptions, F as FinishReason, P as PartialJSONResult, R as RawChunk, b as ReasoningVariant, S as StreamAdapter, e as StreamEventType } from './types-
|
|
1
|
+
import { c as StreamEvent, C as CollectedStream, T as ToSSEOptions, d as StreamEventHandlers } from './types-JvwK2_FV.js';
|
|
2
|
+
export { A as AssembleFromFileOptions, a as AssembleOptions, F as FinishReason, P as PartialJSONResult, R as RawChunk, b as ReasoningVariant, S as StreamAdapter, e as StreamEventType } from './types-JvwK2_FV.js';
|
|
3
3
|
export { assembleFromFile, assembleFromPayloads, assembleResponse, assembleStream, createAssemblyTransform, parsePartialJSON, parseSSE } from './core/index.js';
|
|
4
4
|
export { OpenAIChatAdapterOptions, openaiChatAdapter } from './adapters/openai-chat.js';
|
|
5
5
|
export { HOST_COMPATIBLE_PRESETS, LOOSE_HOST_PRESETS, OPENAI_COMPATIBLE_PROVIDERS, OpenAICompatibleAdapterOptions, OpenAICompatibleProvider, PRESET_OVERRIDES, PRESET_OVERRIDE_KEYS, ResolvedCompatibleAdapterConfig, STRICT_COMPATIBLE_PRESETS, compatibleProviderLabel, hasPresetOverride, isStrictCompatiblePreset, openaiCompatibleAdapter, providerPreset, resolveCompatibleAdapterConfig } from './adapters/openai-compatible.js';
|
|
@@ -31,6 +31,44 @@ interface CitationSpanAnchorResult {
|
|
|
31
31
|
}
|
|
32
32
|
declare function citationSpanAnchor(input: CitationSpanAnchorInput): CitationSpanAnchorResult;
|
|
33
33
|
|
|
34
|
+
interface LogprobConfidenceInput {
|
|
35
|
+
logprob: number;
|
|
36
|
+
topLogprobs?: Array<{
|
|
37
|
+
token: string;
|
|
38
|
+
logprob: number;
|
|
39
|
+
}>;
|
|
40
|
+
}
|
|
41
|
+
interface LogprobConfidenceResult {
|
|
42
|
+
probability?: number;
|
|
43
|
+
margin?: number;
|
|
44
|
+
runnerUpToken?: string;
|
|
45
|
+
}
|
|
46
|
+
declare function logprobConfidence(input: LogprobConfidenceInput): LogprobConfidenceResult;
|
|
47
|
+
|
|
48
|
+
interface LogprobTextAlignmentInput {
|
|
49
|
+
assistantText: string;
|
|
50
|
+
logprobs: Array<{
|
|
51
|
+
token: string;
|
|
52
|
+
logprob: number;
|
|
53
|
+
position?: number;
|
|
54
|
+
choiceIndex?: number;
|
|
55
|
+
channel?: "content" | "refusal";
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
interface LogprobTextAlignmentEntry {
|
|
59
|
+
logprob: number;
|
|
60
|
+
token: string;
|
|
61
|
+
start: number;
|
|
62
|
+
end: number;
|
|
63
|
+
consistent: boolean;
|
|
64
|
+
position?: number;
|
|
65
|
+
}
|
|
66
|
+
interface LogprobTextAlignmentResult {
|
|
67
|
+
entries: LogprobTextAlignmentEntry[];
|
|
68
|
+
unaligned: number;
|
|
69
|
+
}
|
|
70
|
+
declare function alignLogprobsWithText(input: LogprobTextAlignmentInput): LogprobTextAlignmentResult;
|
|
71
|
+
|
|
34
72
|
declare function isMessageStart(event: StreamEvent): event is Extract<StreamEvent, {
|
|
35
73
|
type: "message.start";
|
|
36
74
|
}>;
|
|
@@ -76,6 +114,9 @@ declare function isCitation(event: StreamEvent): event is Extract<StreamEvent, {
|
|
|
76
114
|
declare function isGrounding(event: StreamEvent): event is Extract<StreamEvent, {
|
|
77
115
|
type: "grounding";
|
|
78
116
|
}>;
|
|
117
|
+
declare function isLogprob(event: StreamEvent): event is Extract<StreamEvent, {
|
|
118
|
+
type: "logprob";
|
|
119
|
+
}>;
|
|
79
120
|
declare function isUsage(event: StreamEvent): event is Extract<StreamEvent, {
|
|
80
121
|
type: "usage";
|
|
81
122
|
}>;
|
|
@@ -86,4 +127,4 @@ declare function isError(event: StreamEvent): event is Extract<StreamEvent, {
|
|
|
86
127
|
type: "error";
|
|
87
128
|
}>;
|
|
88
129
|
|
|
89
|
-
export { type CitationSpanAnchorInput, type CitationSpanAnchorResult, CollectedStream, StreamEvent, StreamEventHandlers, ToSSEOptions, citationSpanAnchor, collectStream, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, matchEvent, tapEvents, toSSE };
|
|
130
|
+
export { type CitationSpanAnchorInput, type CitationSpanAnchorResult, CollectedStream, type LogprobConfidenceInput, type LogprobConfidenceResult, type LogprobTextAlignmentEntry, type LogprobTextAlignmentInput, type LogprobTextAlignmentResult, StreamEvent, StreamEventHandlers, ToSSEOptions, alignLogprobsWithText, citationSpanAnchor, collectStream, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isLogprob, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, logprobConfidence, matchEvent, tapEvents, toSSE };
|
package/dist/index.js
CHANGED
|
@@ -317,6 +317,8 @@ var EventAssembler = class {
|
|
|
317
317
|
return [this.citationEvent(chunk)];
|
|
318
318
|
case "grounding":
|
|
319
319
|
return [this.groundingEvent(chunk)];
|
|
320
|
+
case "logprob":
|
|
321
|
+
return [this.logprobEvent(chunk)];
|
|
320
322
|
case "metadata": {
|
|
321
323
|
const { kind: _kind, ...metadata } = chunk;
|
|
322
324
|
return [optionalEvent({ type: "metadata", ...metadata })];
|
|
@@ -542,6 +544,10 @@ var EventAssembler = class {
|
|
|
542
544
|
const { kind: _kind, ...rest } = chunk;
|
|
543
545
|
return optionalEvent({ type: "grounding", ...rest });
|
|
544
546
|
}
|
|
547
|
+
logprobEvent(chunk) {
|
|
548
|
+
const { kind: _kind, ...rest } = chunk;
|
|
549
|
+
return optionalEvent({ type: "logprob", ...rest });
|
|
550
|
+
}
|
|
545
551
|
errorEvent(error, recoverable) {
|
|
546
552
|
const source = errorFromUnknown(error);
|
|
547
553
|
if (!this.options.sanitizeErrors) {
|
|
@@ -1190,6 +1196,82 @@ function firstNumber(source, fields) {
|
|
|
1190
1196
|
return void 0;
|
|
1191
1197
|
}
|
|
1192
1198
|
|
|
1199
|
+
// src/adapters/shared/logprobs.ts
|
|
1200
|
+
function createLogprobPositionState() {
|
|
1201
|
+
return { positions: /* @__PURE__ */ new Map() };
|
|
1202
|
+
}
|
|
1203
|
+
function logprobPositionKey(choiceIndex, channel) {
|
|
1204
|
+
return `${choiceIndex ?? 0}:${channel}`;
|
|
1205
|
+
}
|
|
1206
|
+
function nextLogprobPosition(state, choiceIndex, channel) {
|
|
1207
|
+
const key = logprobPositionKey(choiceIndex, channel);
|
|
1208
|
+
const next = state.positions.get(key) ?? 0;
|
|
1209
|
+
state.positions.set(key, next + 1);
|
|
1210
|
+
return next;
|
|
1211
|
+
}
|
|
1212
|
+
function normalizeTopLogprobs(value) {
|
|
1213
|
+
if (!Array.isArray(value) || value.length === 0) return void 0;
|
|
1214
|
+
const result = [];
|
|
1215
|
+
for (const entry of value) {
|
|
1216
|
+
if (!isRecord(entry)) continue;
|
|
1217
|
+
const token = asString(entry.token);
|
|
1218
|
+
const logprob = asNumber(entry.logprob);
|
|
1219
|
+
if (token === void 0 || logprob === void 0) continue;
|
|
1220
|
+
const item = { token, logprob };
|
|
1221
|
+
if (Array.isArray(entry.bytes)) {
|
|
1222
|
+
const bytes = entry.bytes.filter((byte) => typeof byte === "number");
|
|
1223
|
+
if (bytes.length > 0) item.bytes = bytes;
|
|
1224
|
+
}
|
|
1225
|
+
result.push(item);
|
|
1226
|
+
}
|
|
1227
|
+
return result.length > 0 ? result : void 0;
|
|
1228
|
+
}
|
|
1229
|
+
function logprobEntryFromProvider(entry, channel, choiceIndex, position) {
|
|
1230
|
+
if (!isRecord(entry)) return void 0;
|
|
1231
|
+
const token = asString(entry.token);
|
|
1232
|
+
if (token === void 0 || token.length === 0) return void 0;
|
|
1233
|
+
const logprob = asNumber(entry.logprob);
|
|
1234
|
+
if (logprob === void 0) return void 0;
|
|
1235
|
+
const chunk = {
|
|
1236
|
+
kind: "logprob",
|
|
1237
|
+
channel,
|
|
1238
|
+
token,
|
|
1239
|
+
logprob,
|
|
1240
|
+
raw: entry
|
|
1241
|
+
};
|
|
1242
|
+
if (Array.isArray(entry.bytes)) {
|
|
1243
|
+
const bytes = entry.bytes.filter((byte) => typeof byte === "number");
|
|
1244
|
+
if (bytes.length > 0) chunk.bytes = bytes;
|
|
1245
|
+
}
|
|
1246
|
+
const topLogprobs = normalizeTopLogprobs(entry.top_logprobs);
|
|
1247
|
+
if (topLogprobs) chunk.topLogprobs = topLogprobs;
|
|
1248
|
+
if (choiceIndex !== void 0) chunk.choiceIndex = choiceIndex;
|
|
1249
|
+
if (position !== void 0) chunk.position = position;
|
|
1250
|
+
return optionalRawChunk(chunk);
|
|
1251
|
+
}
|
|
1252
|
+
function logprobChunksFromChoiceLogprobs(logprobs, choiceIndex, positionState) {
|
|
1253
|
+
if (logprobs === null || logprobs === void 0) return [];
|
|
1254
|
+
if (!isRecord(logprobs)) return [];
|
|
1255
|
+
const chunks = [];
|
|
1256
|
+
const content = logprobs.content;
|
|
1257
|
+
const refusal = logprobs.refusal;
|
|
1258
|
+
if (Array.isArray(content)) {
|
|
1259
|
+
for (let index = 0; index < content.length; index += 1) {
|
|
1260
|
+
const position = positionState ? nextLogprobPosition(positionState, choiceIndex, "content") : index;
|
|
1261
|
+
const chunk = logprobEntryFromProvider(content[index], "content", choiceIndex, position);
|
|
1262
|
+
if (chunk) chunks.push(chunk);
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
if (Array.isArray(refusal)) {
|
|
1266
|
+
for (let index = 0; index < refusal.length; index += 1) {
|
|
1267
|
+
const position = positionState ? nextLogprobPosition(positionState, choiceIndex, "refusal") : index;
|
|
1268
|
+
const chunk = logprobEntryFromProvider(refusal[index], "refusal", choiceIndex, position);
|
|
1269
|
+
if (chunk) chunks.push(chunk);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
return chunks;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1193
1275
|
// src/adapters/openai-chat/parser/stream-parser.ts
|
|
1194
1276
|
var OpenAIChatLikeParser = class {
|
|
1195
1277
|
constructor(options) {
|
|
@@ -1199,6 +1281,7 @@ var OpenAIChatLikeParser = class {
|
|
|
1199
1281
|
metadataEmitted = false;
|
|
1200
1282
|
tools = /* @__PURE__ */ new Map();
|
|
1201
1283
|
legacyStarted = /* @__PURE__ */ new Set();
|
|
1284
|
+
logprobPositions = createLogprobPositionState();
|
|
1202
1285
|
parseChunk(raw) {
|
|
1203
1286
|
const payload = parseAdapterObjectPayload(raw, `${this.options.errorPrefix}.parseChunk`);
|
|
1204
1287
|
if (!payload) return [];
|
|
@@ -1236,6 +1319,9 @@ var OpenAIChatLikeParser = class {
|
|
|
1236
1319
|
const choiceIndex = choiceIndexFor(choice, position, this.options);
|
|
1237
1320
|
const chunks = [];
|
|
1238
1321
|
const delta = isRecord(choice.delta) ? choice.delta : void 0;
|
|
1322
|
+
chunks.push(
|
|
1323
|
+
...logprobChunksFromChoiceLogprobs(choice.logprobs, choiceIndex, this.logprobPositions)
|
|
1324
|
+
);
|
|
1239
1325
|
if (delta) {
|
|
1240
1326
|
chunks.push(...this.deltaChunks(delta, choiceIndex, position));
|
|
1241
1327
|
}
|
|
@@ -1370,6 +1456,7 @@ function responseChoiceChunks(choice, position, options) {
|
|
|
1370
1456
|
const choiceIndex = choiceIndexFor(choice, position, options);
|
|
1371
1457
|
const message = isRecord(choice.message) ? choice.message : void 0;
|
|
1372
1458
|
const chunks = [];
|
|
1459
|
+
chunks.push(...logprobChunksFromChoiceLogprobs(choice.logprobs, choiceIndex));
|
|
1373
1460
|
if (message) {
|
|
1374
1461
|
const content = asString(message.content);
|
|
1375
1462
|
if (content && content.length > 0) {
|
|
@@ -3296,7 +3383,8 @@ async function collectStream(events) {
|
|
|
3296
3383
|
json: void 0,
|
|
3297
3384
|
toolCalls: [],
|
|
3298
3385
|
citations: [],
|
|
3299
|
-
grounding: []
|
|
3386
|
+
grounding: [],
|
|
3387
|
+
logprobs: []
|
|
3300
3388
|
};
|
|
3301
3389
|
const iterator = events[Symbol.asyncIterator]();
|
|
3302
3390
|
try {
|
|
@@ -3343,6 +3431,9 @@ function collectEvent(result, event) {
|
|
|
3343
3431
|
case "grounding":
|
|
3344
3432
|
result.grounding.push(event);
|
|
3345
3433
|
break;
|
|
3434
|
+
case "logprob":
|
|
3435
|
+
result.logprobs.push(event);
|
|
3436
|
+
break;
|
|
3346
3437
|
case "usage":
|
|
3347
3438
|
result.usage = event;
|
|
3348
3439
|
break;
|
|
@@ -3463,6 +3554,8 @@ function matchEvent(event, handlers) {
|
|
|
3463
3554
|
return handlers.citation?.(event);
|
|
3464
3555
|
case "grounding":
|
|
3465
3556
|
return handlers.grounding?.(event);
|
|
3557
|
+
case "logprob":
|
|
3558
|
+
return handlers.logprob?.(event);
|
|
3466
3559
|
case "usage":
|
|
3467
3560
|
return handlers.usage?.(event);
|
|
3468
3561
|
case "finish":
|
|
@@ -3501,6 +3594,52 @@ function citationSpanAnchor(input) {
|
|
|
3501
3594
|
return { anchorText, consistent: true };
|
|
3502
3595
|
}
|
|
3503
3596
|
|
|
3597
|
+
// src/helpers/logprob-confidence.ts
|
|
3598
|
+
function logprobConfidence(input) {
|
|
3599
|
+
const result = {};
|
|
3600
|
+
if (Number.isFinite(input.logprob) && input.logprob <= 0) {
|
|
3601
|
+
result.probability = Math.exp(input.logprob);
|
|
3602
|
+
}
|
|
3603
|
+
const top = input.topLogprobs;
|
|
3604
|
+
if (top && top.length >= 2) {
|
|
3605
|
+
const sorted = [...top].sort((left, right) => right.logprob - left.logprob);
|
|
3606
|
+
const best = sorted[0];
|
|
3607
|
+
const runnerUp = sorted[1];
|
|
3608
|
+
if (best && runnerUp && Number.isFinite(best.logprob) && Number.isFinite(runnerUp.logprob)) {
|
|
3609
|
+
result.margin = best.logprob - runnerUp.logprob;
|
|
3610
|
+
result.runnerUpToken = runnerUp.token;
|
|
3611
|
+
}
|
|
3612
|
+
}
|
|
3613
|
+
return result;
|
|
3614
|
+
}
|
|
3615
|
+
|
|
3616
|
+
// src/helpers/align-logprobs-with-text.ts
|
|
3617
|
+
function alignLogprobsWithText(input) {
|
|
3618
|
+
const entries = [];
|
|
3619
|
+
let cursor = 0;
|
|
3620
|
+
let unaligned = 0;
|
|
3621
|
+
for (const item of input.logprobs) {
|
|
3622
|
+
const index = input.assistantText.indexOf(item.token, cursor);
|
|
3623
|
+
if (index === -1) {
|
|
3624
|
+
unaligned += 1;
|
|
3625
|
+
continue;
|
|
3626
|
+
}
|
|
3627
|
+
const start = index;
|
|
3628
|
+
const end = index + item.token.length;
|
|
3629
|
+
const slice = input.assistantText.slice(start, end);
|
|
3630
|
+
entries.push({
|
|
3631
|
+
logprob: item.logprob,
|
|
3632
|
+
token: item.token,
|
|
3633
|
+
start,
|
|
3634
|
+
end,
|
|
3635
|
+
consistent: slice === item.token,
|
|
3636
|
+
...item.position !== void 0 ? { position: item.position } : {}
|
|
3637
|
+
});
|
|
3638
|
+
cursor = end;
|
|
3639
|
+
}
|
|
3640
|
+
return { entries, unaligned };
|
|
3641
|
+
}
|
|
3642
|
+
|
|
3504
3643
|
// src/helpers/type-guards.ts
|
|
3505
3644
|
function isMessageStart(event) {
|
|
3506
3645
|
return event.type === "message.start";
|
|
@@ -3547,6 +3686,9 @@ function isCitation(event) {
|
|
|
3547
3686
|
function isGrounding(event) {
|
|
3548
3687
|
return event.type === "grounding";
|
|
3549
3688
|
}
|
|
3689
|
+
function isLogprob(event) {
|
|
3690
|
+
return event.type === "logprob";
|
|
3691
|
+
}
|
|
3550
3692
|
function isUsage(event) {
|
|
3551
3693
|
return event.type === "usage";
|
|
3552
3694
|
}
|
|
@@ -3557,6 +3699,6 @@ function isError(event) {
|
|
|
3557
3699
|
return event.type === "error";
|
|
3558
3700
|
}
|
|
3559
3701
|
|
|
3560
|
-
export { HOST_COMPATIBLE_PRESETS, LOOSE_HOST_PRESETS, OPENAI_COMPATIBLE_PROVIDERS, PRESET_OVERRIDES, PRESET_OVERRIDE_KEYS, STRICT_COMPATIBLE_PRESETS, anthropicAdapter, assembleFromFile, assembleFromPayloads, assembleResponse, assembleStream, bedrockAdapter, citationSpanAnchor, cohereAdapter, collectStream, compatibleProviderLabel, createAssemblyTransform, geminiAdapter, hasPresetOverride, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isStrictCompatiblePreset, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, matchEvent, openaiChatAdapter, openaiCompatibleAdapter, openaiResponsesAdapter, parsePartialJSON, parseSSE, providerPreset, resolveCompatibleAdapterConfig, tapEvents, toSSE };
|
|
3702
|
+
export { HOST_COMPATIBLE_PRESETS, LOOSE_HOST_PRESETS, OPENAI_COMPATIBLE_PROVIDERS, PRESET_OVERRIDES, PRESET_OVERRIDE_KEYS, STRICT_COMPATIBLE_PRESETS, alignLogprobsWithText, anthropicAdapter, assembleFromFile, assembleFromPayloads, assembleResponse, assembleStream, bedrockAdapter, citationSpanAnchor, cohereAdapter, collectStream, compatibleProviderLabel, createAssemblyTransform, geminiAdapter, hasPresetOverride, isCitation, isError, isFinish, isGrounding, isJsonDelta, isJsonDone, isLogprob, isMessageStart, isMetadata, isReasoningDelta, isReasoningDone, isRefusalDelta, isRefusalDone, isStrictCompatiblePreset, isTextDelta, isTextDone, isToolCallArgsDelta, isToolCallDone, isToolCallStart, isUsage, logprobConfidence, matchEvent, openaiChatAdapter, openaiCompatibleAdapter, openaiResponsesAdapter, parsePartialJSON, parseSSE, providerPreset, resolveCompatibleAdapterConfig, tapEvents, toSSE };
|
|
3561
3703
|
//# sourceMappingURL=index.js.map
|
|
3562
3704
|
//# sourceMappingURL=index.js.map
|