@f5-sales-demo/pi-ai 19.51.2
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 +1997 -0
- package/README.md +1160 -0
- package/package.json +135 -0
- package/src/api-registry.ts +95 -0
- package/src/auth-storage.ts +2694 -0
- package/src/cli.ts +493 -0
- package/src/index.ts +42 -0
- package/src/model-cache.ts +97 -0
- package/src/model-manager.ts +349 -0
- package/src/model-thinking.ts +561 -0
- package/src/models.json +49439 -0
- package/src/models.json.d.ts +9 -0
- package/src/models.ts +56 -0
- package/src/prompts/turn-aborted-guidance.md +4 -0
- package/src/provider-details.ts +81 -0
- package/src/provider-models/descriptors.ts +285 -0
- package/src/provider-models/google.ts +90 -0
- package/src/provider-models/index.ts +4 -0
- package/src/provider-models/openai-compat.ts +2074 -0
- package/src/provider-models/special.ts +106 -0
- package/src/providers/amazon-bedrock.ts +706 -0
- package/src/providers/anthropic.ts +1682 -0
- package/src/providers/azure-openai-responses.ts +391 -0
- package/src/providers/cursor/gen/agent_pb.ts +15274 -0
- package/src/providers/cursor/proto/agent.proto +3526 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor.ts +2218 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +381 -0
- package/src/providers/google-gemini-cli.ts +1133 -0
- package/src/providers/google-shared.ts +354 -0
- package/src/providers/google-vertex.ts +436 -0
- package/src/providers/google.ts +381 -0
- package/src/providers/kimi.ts +151 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +158 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +2345 -0
- package/src/providers/openai-completions-compat.ts +159 -0
- package/src/providers/openai-completions.ts +1290 -0
- package/src/providers/openai-responses-shared.ts +452 -0
- package/src/providers/openai-responses.ts +519 -0
- package/src/providers/register-builtins.ts +329 -0
- package/src/providers/synthetic.ts +154 -0
- package/src/providers/transform-messages.ts +234 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +728 -0
- package/src/types.ts +546 -0
- package/src/usage/claude.ts +337 -0
- package/src/usage/gemini.ts +248 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +200 -0
- package/src/usage/kimi.ts +286 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +387 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +130 -0
- package/src/utils/abort.ts +36 -0
- package/src/utils/anthropic-auth.ts +293 -0
- package/src/utils/discovery/antigravity.ts +261 -0
- package/src/utils/discovery/codex.ts +371 -0
- package/src/utils/discovery/cursor.ts +306 -0
- package/src/utils/discovery/gemini.ts +248 -0
- package/src/utils/discovery/index.ts +5 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +209 -0
- package/src/utils/http-inspector.ts +165 -0
- package/src/utils/idle-iterator.ts +176 -0
- package/src/utils/json-parse.ts +28 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +134 -0
- package/src/utils/oauth/api-key-validation.ts +92 -0
- package/src/utils/oauth/callback-server.ts +276 -0
- package/src/utils/oauth/cerebras.ts +59 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/github-copilot.ts +358 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +275 -0
- package/src/utils/oauth/google-gemini-cli.ts +334 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +512 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +251 -0
- package/src/utils/oauth/litellm.ts +81 -0
- package/src/utils/oauth/lm-studio.ts +40 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +59 -0
- package/src/utils/oauth/nanogpt.ts +51 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +199 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +190 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +200 -0
- package/src/utils/oauth/pkce.ts +18 -0
- package/src/utils/oauth/qianfan.ts +58 -0
- package/src/utils/oauth/qwen-portal.ts +60 -0
- package/src/utils/oauth/synthetic.ts +60 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +59 -0
- package/src/utils/oauth/types.ts +89 -0
- package/src/utils/oauth/venice.ts +59 -0
- package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
- package/src/utils/oauth/vllm.ts +40 -0
- package/src/utils/oauth/xiaomi.ts +88 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +51 -0
- package/src/utils/overflow.ts +134 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +93 -0
- package/src/utils/schema/CONSTRAINTS.md +160 -0
- package/src/utils/schema/adapt.ts +20 -0
- package/src/utils/schema/compatibility.ts +397 -0
- package/src/utils/schema/dereference.ts +93 -0
- package/src/utils/schema/equality.ts +93 -0
- package/src/utils/schema/fields.ts +147 -0
- package/src/utils/schema/index.ts +9 -0
- package/src/utils/schema/normalize-cca.ts +479 -0
- package/src/utils/schema/sanitize-google.ts +212 -0
- package/src/utils/schema/strict-mode.ts +385 -0
- package/src/utils/schema/types.ts +5 -0
- package/src/utils/tool-choice.ts +81 -0
- package/src/utils/validation.ts +664 -0
- package/src/utils.ts +147 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { $env } from "@f5xc-salesdemos/pi-utils";
|
|
2
|
+
import type { ResponseInput } from "openai/resources/responses/responses";
|
|
3
|
+
import type { CacheRetention, OpenAIResponsesHistoryPayload, ProviderPayload } from "./types";
|
|
4
|
+
|
|
5
|
+
type OpenAIResponsesReplayItem = ResponseInput[number];
|
|
6
|
+
|
|
7
|
+
export { isRecord } from "@f5xc-salesdemos/pi-utils";
|
|
8
|
+
|
|
9
|
+
export function toNumber(value: unknown): number | undefined {
|
|
10
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
11
|
+
if (typeof value === "string" && value.trim()) {
|
|
12
|
+
const parsed = Number(value);
|
|
13
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
14
|
+
}
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function toPositiveNumber(value: unknown, fallback: number): number {
|
|
19
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) {
|
|
20
|
+
return fallback;
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function toBoolean(value: unknown): boolean | undefined {
|
|
26
|
+
return typeof value === "boolean" ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function normalizeToolCallId(id: string): string {
|
|
30
|
+
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
31
|
+
return sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function normalizeResponsesToolCallId(id: string): { callId: string; itemId: string } {
|
|
35
|
+
const [callId, itemId] = id.split("|");
|
|
36
|
+
if (callId && itemId) {
|
|
37
|
+
const normalizedCallId = truncateResponseItemId(callId, getIdPrefix(callId, "call"));
|
|
38
|
+
const normalizedItemId = normalizeResponsesItemId(itemId);
|
|
39
|
+
return { callId: normalizedCallId, itemId: normalizedItemId };
|
|
40
|
+
}
|
|
41
|
+
const hash = Bun.hash(id).toString(36);
|
|
42
|
+
const normalizedCallId = id.startsWith("call_") ? truncateResponseItemId(id, "call") : `call_${hash}`;
|
|
43
|
+
return { callId: normalizedCallId, itemId: `fc_${hash}` };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getIdPrefix(id: string, fallback: string): string {
|
|
47
|
+
const prefix = id.match(/^([a-zA-Z][a-zA-Z0-9]*)_/)?.[1];
|
|
48
|
+
return prefix || fallback;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeResponsesItemId(itemId: string): string {
|
|
52
|
+
const prefix = getIdPrefix(itemId, "fc");
|
|
53
|
+
if (prefix !== "fc" && prefix !== "fcr") {
|
|
54
|
+
return `fc_${Bun.hash(itemId).toString(36)}`;
|
|
55
|
+
}
|
|
56
|
+
return truncateResponseItemId(itemId, prefix);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Truncate an OpenAI Responses API item ID to 64 characters.
|
|
61
|
+
* IDs exceeding the limit are replaced with a hash-based ID using the given prefix.
|
|
62
|
+
*/
|
|
63
|
+
export function truncateResponseItemId(id: string, prefix: string): string {
|
|
64
|
+
if (id.length <= 64) return id;
|
|
65
|
+
return `${prefix}_${Bun.hash(id).toString(36)}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function sanitizeOpenAIResponsesHistoryItemsForReplay(items: Array<Record<string, unknown>>): ResponseInput {
|
|
69
|
+
const normalizedCallIds = new Map<string, string>();
|
|
70
|
+
return items.flatMap(item => {
|
|
71
|
+
const sanitized = sanitizeOpenAIResponsesHistoryItemForReplay(item, normalizedCallIds);
|
|
72
|
+
return sanitized ? [sanitized] : [];
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function sanitizeOpenAIResponsesHistoryItemForReplay(
|
|
77
|
+
item: Record<string, unknown>,
|
|
78
|
+
normalizedCallIds: Map<string, string>,
|
|
79
|
+
): OpenAIResponsesReplayItem | undefined {
|
|
80
|
+
if (item.type === "item_reference") return undefined;
|
|
81
|
+
|
|
82
|
+
// providerPayload stores raw output items; replay strips item ids and keeps only normalized call_id.
|
|
83
|
+
const { id: _id, ...sanitizedItem } = item;
|
|
84
|
+
if (typeof item.call_id === "string") {
|
|
85
|
+
sanitizedItem.call_id = normalizeReplayedResponsesHistoryCallId(item.call_id, normalizedCallIds);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return sanitizedItem as unknown as OpenAIResponsesReplayItem;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function normalizeReplayedResponsesHistoryCallId(value: string, normalizedValues: Map<string, string>): string {
|
|
92
|
+
const normalized = normalizedValues.get(value);
|
|
93
|
+
if (normalized) return normalized;
|
|
94
|
+
const next = truncateResponseItemId(value, getIdPrefix(value, "call"));
|
|
95
|
+
normalizedValues.set(value, next);
|
|
96
|
+
return next;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function createOpenAIResponsesHistoryPayload(
|
|
100
|
+
provider: string,
|
|
101
|
+
items: Array<Record<string, unknown>>,
|
|
102
|
+
incremental = true,
|
|
103
|
+
): OpenAIResponsesHistoryPayload {
|
|
104
|
+
return {
|
|
105
|
+
type: "openaiResponsesHistory",
|
|
106
|
+
provider,
|
|
107
|
+
...(incremental ? { dt: true } : {}),
|
|
108
|
+
items,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function getOpenAIResponsesHistoryPayload(
|
|
113
|
+
providerPayload: ProviderPayload | undefined,
|
|
114
|
+
currentProvider: string,
|
|
115
|
+
fallbackProvider?: string,
|
|
116
|
+
): OpenAIResponsesHistoryPayload | undefined {
|
|
117
|
+
if (providerPayload?.type !== "openaiResponsesHistory" || !Array.isArray(providerPayload.items)) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
const payloadProvider = providerPayload.provider ?? fallbackProvider;
|
|
121
|
+
if (!payloadProvider || payloadProvider !== currentProvider) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return { ...providerPayload, provider: payloadProvider };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function getOpenAIResponsesHistoryItems(
|
|
128
|
+
providerPayload: ProviderPayload | undefined,
|
|
129
|
+
currentProvider: string,
|
|
130
|
+
fallbackProvider?: string,
|
|
131
|
+
): Array<Record<string, unknown>> | undefined {
|
|
132
|
+
return getOpenAIResponsesHistoryPayload(providerPayload, currentProvider, fallbackProvider)?.items;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Resolve cache retention preference.
|
|
137
|
+
* Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
|
|
138
|
+
*/
|
|
139
|
+
export function resolveCacheRetention(cacheRetention?: CacheRetention): CacheRetention {
|
|
140
|
+
if (cacheRetention) return cacheRetention;
|
|
141
|
+
if ($env.PI_CACHE_RETENTION === "long") return "long";
|
|
142
|
+
return "short";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function isAnthropicOAuthToken(key: string): boolean {
|
|
146
|
+
return key.includes("sk-ant-oat");
|
|
147
|
+
}
|