@bastani/pi-ai 0.9.37 → 0.9.38-alpha.1
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/CHANGELOG.md +27 -0
- package/README.md +1 -1
- package/dist/api/anthropic-messages.js +8 -3
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/bedrock-converse-stream.js +2 -2
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +1 -0
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/mistral-conversations.js +2 -2
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +5 -1
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts +1 -1
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +31 -29
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts +10 -0
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +15 -2
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +25 -6
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/openai-retry-hint-client.d.ts +10 -0
- package/dist/api/openai-retry-hint-client.d.ts.map +1 -0
- package/dist/api/openai-retry-hint-client.js +17 -0
- package/dist/api/openai-retry-hint-client.js.map +1 -0
- package/dist/auth/oauth/kimi-coding.d.ts +1 -1
- package/dist/auth/oauth/kimi-coding.js +1 -1
- package/dist/auth/oauth/kimi-coding.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/model-catalog.d.ts +1 -1
- package/dist/model-catalog.d.ts.map +1 -1
- package/dist/model-catalog.js +32 -2
- package/dist/model-catalog.js.map +1 -1
- package/dist/providers/compat-schema.d.ts +3 -0
- package/dist/providers/compat-schema.d.ts.map +1 -1
- package/dist/providers/compat-schema.js +8 -0
- package/dist/providers/compat-schema.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/kimi-coding.json +1 -1
- package/dist/providers/data/nvidia.json +1 -1
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/radius.json +1 -1
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
- package/dist/providers/data/xiaomi.json +1 -1
- package/dist/providers/kimi-coding.d.ts +1 -1
- package/dist/providers/kimi-coding.js +3 -3
- package/dist/providers/kimi-coding.js.map +1 -1
- package/dist/providers/openai-codex.js.map +1 -1
- package/dist/providers/openai-mid-convo-effort.d.ts +2 -0
- package/dist/providers/openai-mid-convo-effort.d.ts.map +1 -0
- package/dist/providers/openai-mid-convo-effort.js +7 -0
- package/dist/providers/openai-mid-convo-effort.js.map +1 -0
- package/dist/providers/openai.d.ts.map +1 -1
- package/dist/providers/openai.js +11 -2
- package/dist/providers/openai.js.map +1 -1
- package/dist/providers/opencode-go.d.ts +3 -1
- package/dist/providers/opencode-go.d.ts.map +1 -1
- package/dist/providers/opencode-go.js +2 -0
- package/dist/providers/opencode-go.js.map +1 -1
- package/dist/types.d.ts +3 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/json-parse.d.ts +2 -0
- package/dist/utils/json-parse.d.ts.map +1 -1
- package/dist/utils/json-parse.js +18 -1
- package/dist/utils/json-parse.js.map +1 -1
- package/dist/utils/retry-hints.d.ts +12 -0
- package/dist/utils/retry-hints.d.ts.map +1 -0
- package/dist/utils/retry-hints.js +50 -0
- package/dist/utils/retry-hints.js.map +1 -0
- package/dist/utils/retry.d.ts +20 -2
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +28 -3
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +3 -0
- package/dist/utils/validation.js.map +1 -1
- package/package.json +1 -1
- package/dist/utils/assistant-message-frame.d.ts +0 -77
- package/dist/utils/assistant-message-frame.d.ts.map +0 -1
- package/dist/utils/assistant-message-frame.js +0 -439
- package/dist/utils/assistant-message-frame.js.map +0 -1
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import OpenAI from "openai";
|
|
2
1
|
import { calculateCost, clampThinkingLevel } from "../models.js";
|
|
3
2
|
import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
|
|
4
3
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
4
|
import { shortHash } from "../utils/hash.js";
|
|
6
5
|
import { headersToRecord } from "../utils/headers.js";
|
|
7
|
-
import { parseStreamingJson } from "../utils/json-parse.js";
|
|
6
|
+
import { parseStreamingJson, parseToolCallArguments } from "../utils/json-parse.js";
|
|
8
7
|
import { getPiUserAgent } from "../utils/pi-user-agent.js";
|
|
9
8
|
import { getProviderEnvValue } from "../utils/provider-env.js";
|
|
10
9
|
import { retryProviderRequest } from "../utils/provider-retry.js";
|
|
10
|
+
import { getProviderRetryAfterMs } from "../utils/retry-hints.js";
|
|
11
11
|
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
12
12
|
import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
|
|
13
13
|
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
|
|
@@ -15,6 +15,7 @@ import { getDeclaredTools, resolveTranscript, resolveTranscriptTools, } from "..
|
|
|
15
15
|
import { appendGrammarToolInputJsonDelta, createGrammarToolInputProperties, getGrammarToolInput, getJsonSchemaToolParameters, resolveGrammarConstrainedSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
|
|
16
16
|
import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
|
|
17
17
|
import { clampOpenAIPromptCacheKey } from "./openai-prompt-cache.js";
|
|
18
|
+
import { RetryHintOpenAI } from "./openai-retry-hint-client.js";
|
|
18
19
|
import { buildBaseOptions, clampThinkingBudgetToAnswerRoom, resolveSamplingParams, thinkingBudgetForLevel, } from "./simple-options.js";
|
|
19
20
|
import { transformMessages } from "./transform-messages.js";
|
|
20
21
|
/**
|
|
@@ -271,7 +272,7 @@ export const stream = (model, context, options) => {
|
|
|
271
272
|
}
|
|
272
273
|
}
|
|
273
274
|
else {
|
|
274
|
-
block
|
|
275
|
+
Object.assign(block, parseToolCallArguments(block.partialArgs));
|
|
275
276
|
}
|
|
276
277
|
// Finalize in-place and strip the scratch buffers so replay only
|
|
277
278
|
// carries parsed arguments.
|
|
@@ -394,18 +395,6 @@ export const stream = (model, context, options) => {
|
|
|
394
395
|
hasFinishReason = true;
|
|
395
396
|
}
|
|
396
397
|
if (choice.delta) {
|
|
397
|
-
if (choice.delta.content !== null &&
|
|
398
|
-
choice.delta.content !== undefined &&
|
|
399
|
-
choice.delta.content.length > 0) {
|
|
400
|
-
const block = ensureTextBlock();
|
|
401
|
-
block.text += choice.delta.content;
|
|
402
|
-
stream.push({
|
|
403
|
-
type: "text_delta",
|
|
404
|
-
contentIndex: getContentIndex(block),
|
|
405
|
-
delta: choice.delta.content,
|
|
406
|
-
partial: output,
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
398
|
// Some endpoints return reasoning in reasoning_content (llama.cpp),
|
|
410
399
|
// or reasoning (other openai compatible endpoints)
|
|
411
400
|
// Use the first non-empty reasoning field to avoid duplication
|
|
@@ -436,6 +425,31 @@ export const stream = (model, context, options) => {
|
|
|
436
425
|
});
|
|
437
426
|
}
|
|
438
427
|
}
|
|
428
|
+
const reasoningDetails = choice.delta.reasoning_details;
|
|
429
|
+
if (Array.isArray(reasoningDetails)) {
|
|
430
|
+
for (const detail of reasoningDetails) {
|
|
431
|
+
if (!isOpenAIReasoningDetail(detail))
|
|
432
|
+
continue;
|
|
433
|
+
ensureThinkingBlock("");
|
|
434
|
+
streamedReasoningDetails ??= [];
|
|
435
|
+
// Keep provider replay data in the existing signature slot. OpenRouter streams
|
|
436
|
+
// reasoning_details as deltas: consecutive text/summary deltas are merged into
|
|
437
|
+
// logical entries, while encrypted entries remain opaque and discrete.
|
|
438
|
+
appendOpenAIReasoningDetail(streamedReasoningDetails, detail);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (choice.delta.content !== null &&
|
|
442
|
+
choice.delta.content !== undefined &&
|
|
443
|
+
choice.delta.content.length > 0) {
|
|
444
|
+
const block = ensureTextBlock();
|
|
445
|
+
block.text += choice.delta.content;
|
|
446
|
+
stream.push({
|
|
447
|
+
type: "text_delta",
|
|
448
|
+
contentIndex: getContentIndex(block),
|
|
449
|
+
delta: choice.delta.content,
|
|
450
|
+
partial: output,
|
|
451
|
+
});
|
|
452
|
+
}
|
|
439
453
|
if (choice?.delta?.tool_calls) {
|
|
440
454
|
for (const toolCall of choice.delta.tool_calls) {
|
|
441
455
|
const block = ensureToolCallBlock(toolCall);
|
|
@@ -465,19 +479,6 @@ export const stream = (model, context, options) => {
|
|
|
465
479
|
});
|
|
466
480
|
}
|
|
467
481
|
}
|
|
468
|
-
const reasoningDetails = choice.delta.reasoning_details;
|
|
469
|
-
if (Array.isArray(reasoningDetails)) {
|
|
470
|
-
for (const detail of reasoningDetails) {
|
|
471
|
-
if (!isOpenAIReasoningDetail(detail))
|
|
472
|
-
continue;
|
|
473
|
-
ensureThinkingBlock("");
|
|
474
|
-
streamedReasoningDetails ??= [];
|
|
475
|
-
// Keep provider replay data in the existing signature slot. OpenRouter streams
|
|
476
|
-
// reasoning_details as deltas: consecutive text/summary deltas are merged into
|
|
477
|
-
// logical entries, while encrypted entries remain opaque and discrete.
|
|
478
|
-
appendOpenAIReasoningDetail(streamedReasoningDetails, detail);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
482
|
}
|
|
482
483
|
}
|
|
483
484
|
for (const block of blocks) {
|
|
@@ -514,6 +515,7 @@ export const stream = (model, context, options) => {
|
|
|
514
515
|
}
|
|
515
516
|
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
516
517
|
output.errorMessage = formatProviderError(normalizeProviderError(error));
|
|
518
|
+
output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
|
|
517
519
|
// Some providers via OpenRouter give additional information in this field.
|
|
518
520
|
// normalizeProviderError already stringifies the parsed body (error.error)
|
|
519
521
|
// into errorMessage, so only append the raw metadata when it is not already
|
|
@@ -573,7 +575,7 @@ function createClient(model, context, apiKey, optionsHeaders, fetch, sessionId,
|
|
|
573
575
|
if (optionsHeaders) {
|
|
574
576
|
Object.assign(headers, optionsHeaders);
|
|
575
577
|
}
|
|
576
|
-
return new
|
|
578
|
+
return new RetryHintOpenAI({
|
|
577
579
|
apiKey,
|
|
578
580
|
baseURL: model.baseUrl,
|
|
579
581
|
dangerouslyAllowBrowser: true,
|