Spaces:
Runtime error
Runtime error
File size: 9,656 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | import { z } from "zod";
import {
ACCOUNT_FALLBACK_STRATEGY_VALUES,
ROUTING_STRATEGY_VALUES,
} from "@/shared/constants/routingStrategies";
import { SUPPORTED_BATCH_ENDPOINTS } from "@/shared/constants/batchEndpoints";
import { MAX_REQUEST_BODY_LIMIT_MB, MIN_REQUEST_BODY_LIMIT_MB } from "@/shared/constants/bodySize";
import { COMBO_CONFIG_MODES } from "@/shared/constants/comboConfigMode";
import { providerAllowsOptionalApiKey } from "@/shared/constants/providers";
import { HIDEABLE_SIDEBAR_ITEM_IDS } from "@/shared/constants/sidebarVisibility";
import {
isForbiddenUpstreamHeaderName,
isForbiddenCustomHeaderName,
} from "@/shared/constants/upstreamHeaders";
import { MAX_TIMER_TIMEOUT_MS } from "@/shared/utils/runtimeTimeouts";
import { modelIdSchema, nonEmptyStringSchema } from "./misc.ts";
export const embeddingTokenArraySchema = z
.array(z.number().int().min(0))
.min(1, "input token array must contain at least one item");
export const embeddingInputSchema = z.union([
nonEmptyStringSchema,
z.array(nonEmptyStringSchema).min(1, "input must contain at least one item"),
embeddingTokenArraySchema,
z.array(embeddingTokenArraySchema).min(1, "input must contain at least one item"),
]);
export const chatMessageSchema = z
.object({
role: z.string().trim().min(1, "messages[].role is required"),
content: z.union([nonEmptyStringSchema, z.array(z.unknown()).min(1), z.null()]).optional(),
})
.catchall(z.unknown());
export const countTokensMessageSchema = z
.object({
content: z.union([
nonEmptyStringSchema,
z
.array(
z
.object({
type: z.string().optional(),
text: z.string().optional(),
})
.catchall(z.unknown())
)
.min(1, "messages[].content must contain at least one item"),
]),
})
.catchall(z.unknown());
export const v1EmbeddingsSchema = z
.object({
model: modelIdSchema,
input: embeddingInputSchema,
dimensions: z.coerce.number().int().positive().optional(),
encoding_format: z.enum(["float", "base64"]).optional(),
})
.catchall(z.unknown());
export const v1ImageGenerationSchema = z
.object({
model: modelIdSchema,
prompt: nonEmptyStringSchema.optional(),
})
.catchall(z.unknown());
export const v1AudioSpeechSchema = z
.object({
model: modelIdSchema,
input: nonEmptyStringSchema,
})
.catchall(z.unknown());
export const v1ModerationSchema = z
.object({
model: modelIdSchema.optional(),
input: z.unknown().refine((value) => {
if (value === undefined || value === null) return false;
if (typeof value === "string") return value.trim().length > 0;
if (Array.isArray(value)) return value.length > 0;
return true;
}, "Input is required"),
})
.catchall(z.unknown());
export const v1RerankSchema = z
.object({
model: modelIdSchema,
query: nonEmptyStringSchema,
documents: z.array(z.unknown()).min(1, "documents must contain at least one item"),
})
.catchall(z.unknown());
export const providerChatCompletionSchema = z
.object({
model: modelIdSchema,
messages: z.array(chatMessageSchema).min(1).optional(),
input: z.union([nonEmptyStringSchema, z.array(z.unknown()).min(1)]).optional(),
prompt: nonEmptyStringSchema.optional(),
})
.catchall(z.unknown())
.superRefine((value, ctx) => {
if (value.messages === undefined && value.input === undefined && value.prompt === undefined) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "messages, input or prompt is required",
path: [],
});
}
});
export const v1CountTokensSchema = z
.object({
messages: z.array(countTokensMessageSchema).min(1, "messages must contain at least one item"),
})
.catchall(z.unknown());
// ββ Search Schemas βββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Unified search request/response schemas. Final contract β all fields optional
// with defaults. New features add implementations, not new fields.
// Multi-query deferred to POST /v1/search/batch (separate PRD).
export const v1SearchSchema = z
.object({
// Core
query: z
.string()
.trim()
.min(1, "Query is required")
.max(500, "Query must be 500 characters or fewer"),
provider: z
.enum([
"serper-search",
"brave-search",
"perplexity-search",
"exa-search",
"tavily-search",
"google-pse-search",
"linkup-search",
"ollama-search",
"searchapi-search",
"youcom-search",
"searxng-search",
"zai-search",
"duckduckgo-free",
])
.optional(),
max_results: z.coerce.number().int().min(1).max(100).default(5),
search_type: z.enum(["web", "news"]).default("web"),
offset: z.coerce.number().int().min(0).default(0),
// Locale
country: z.string().max(2).toUpperCase().optional(),
language: z.string().min(2).max(5).optional(),
time_range: z.enum(["any", "day", "week", "month", "year"]).optional(),
// Content control
content: z
.object({
snippet: z.boolean().default(true),
full_page: z.boolean().default(false),
format: z.enum(["text", "markdown"]).default("text"),
max_characters: z.coerce.number().int().min(100).max(100000).optional(),
})
.optional(),
// Filters
filters: z
.object({
include_domains: z.array(z.string().max(253)).max(20).optional(),
exclude_domains: z.array(z.string().max(253)).max(20).optional(),
safe_search: z.enum(["off", "moderate", "strict"]).optional(),
})
.optional(),
// Answer synthesis (Phase 2 β returns null until implemented)
synthesis: z
.object({
strategy: z.enum(["none", "auto", "provider", "internal"]).default("none"),
model: z.string().optional(),
max_tokens: z.coerce.number().int().min(1).max(4000).optional(),
})
.optional(),
// Provider-specific passthrough
provider_options: z.record(z.string(), z.unknown()).optional(),
// Strict mode β reject if provider doesn't support a requested filter
strict_filters: z.boolean().default(false),
})
.catchall(z.unknown());
export const searchResultSchema = z.object({
title: z.string(),
url: z.string(),
display_url: z.string().optional(),
snippet: z.string(),
position: z.number().int().positive(),
score: z.number().min(0).max(1).nullable().optional(),
published_at: z.string().nullable().optional(),
favicon_url: z.string().nullable().optional(),
content: z
.object({
format: z.enum(["text", "markdown"]).optional(),
text: z.string().optional(),
length: z.number().int().optional(),
})
.nullable()
.optional(),
metadata: z
.object({
author: z.string().nullable().optional(),
language: z.string().nullable().optional(),
source_type: z
.enum(["article", "blog", "forum", "video", "academic", "news", "other"])
.nullable()
.optional(),
image_url: z.string().nullable().optional(),
})
.nullable()
.optional(),
citation: z.object({
provider: z.string(),
retrieved_at: z.string(),
rank: z.number().int().positive(),
}),
provider_raw: z.record(z.string(), z.unknown()).nullable().optional(),
});
export const v1SearchResponseSchema = z.object({
id: z.string(),
provider: z.string(),
query: z.string(),
results: z.array(searchResultSchema),
cached: z.boolean(),
answer: z
.object({
source: z.enum(["none", "provider", "internal"]).optional(),
text: z.string().nullable().optional(),
model: z.string().nullable().optional(),
})
.nullable()
.optional(),
usage: z.object({
queries_used: z.number().int().min(0),
search_cost_usd: z.number().min(0),
llm_tokens: z.number().int().min(0).optional(),
}),
metrics: z.object({
response_time_ms: z.number().int().min(0),
upstream_latency_ms: z.number().int().min(0).optional(),
gateway_latency_ms: z.number().int().min(0).optional(),
total_results_available: z.number().int().nullable(),
}),
errors: z
.array(
z.object({
provider: z.string(),
code: z.string(),
message: z.string(),
})
)
.optional(),
});
export const v1BatchCreateSchema = z.object({
input_file_id: z.string().min(1),
endpoint: z.enum(SUPPORTED_BATCH_ENDPOINTS),
completion_window: z.enum(["24h"]),
metadata: z
.record(z.string().max(64), z.string().max(512))
.refine((m) => Object.keys(m).length <= 16, { message: "metadata may have at most 16 keys" })
.optional(),
output_expires_after: z
.object({
anchor: z.enum(["created_at"]),
seconds: z.number().int().min(3600).max(2592000),
})
.optional(),
});
// ββ Web Fetch βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export const v1WebFetchSchema = z.object({
url: z.string().url("url must be a valid URL (http/https)"),
provider: z.enum(["firecrawl", "jina-reader", "tavily-search"]).optional(),
format: z.enum(["markdown", "html", "links", "screenshot"]).default("markdown"),
depth: z.union([z.literal(0), z.literal(1), z.literal(2)]).default(0),
wait_for_selector: z.string().max(256).optional(),
include_metadata: z.boolean().default(false),
}); |