UNPKG

@latitude-data/sdk

Version:
1,259 lines (1,230 loc) 86.3 kB
import { z } from 'zod'; import nodeFetch from 'node-fetch'; import { EventSourceParserStream } from 'eventsource-parser/stream'; import { Adapters, render, MessageRole as MessageRole$1, Chain, ContentType } from 'promptl-ai'; export { Adapters } from 'promptl-ai'; var StreamEventTypes; (function (StreamEventTypes) { StreamEventTypes["Latitude"] = "latitude-event"; StreamEventTypes["Provider"] = "provider-event"; })(StreamEventTypes || (StreamEventTypes = {})); z.object({ id: z.string(), name: z.string(), result: z.unknown(), isError: z.boolean().optional(), text: z.string().optional(), }); var ParameterType; (function (ParameterType) { ParameterType["Text"] = "text"; ParameterType["Image"] = "image"; ParameterType["File"] = "file"; })(ParameterType || (ParameterType = {})); var LatitudeTool; (function (LatitudeTool) { LatitudeTool["RunCode"] = "code"; LatitudeTool["WebSearch"] = "search"; LatitudeTool["WebExtract"] = "extract"; })(LatitudeTool || (LatitudeTool = {})); var LatitudeToolInternalName; (function (LatitudeToolInternalName) { LatitudeToolInternalName["RunCode"] = "lat_tool_run_code"; LatitudeToolInternalName["WebSearch"] = "lat_tool_web_search"; LatitudeToolInternalName["WebExtract"] = "lat_tool_web_extract"; })(LatitudeToolInternalName || (LatitudeToolInternalName = {})); const actualOutputConfiguration = z.object({ messageSelection: z.enum(['last', 'all']), // Which assistant messages to select contentFilter: z.enum(['text', 'image', 'file', 'tool_call']).optional(), parsingFormat: z.enum(['string', 'json']), fieldAccessor: z.string().optional(), // Field accessor to get the output from if it's a key-value format }); const expectedOutputConfiguration = z.object({ parsingFormat: z.enum(['string', 'json']), fieldAccessor: z.string().optional(), // Field accessor to get the output from if it's a key-value format }); const baseEvaluationConfiguration = z.object({ reverseScale: z.boolean(), // If true, lower is better, otherwise, higher is better actualOutput: actualOutputConfiguration.optional(), // Optional for backwards compatibility expectedOutput: expectedOutputConfiguration.optional(), // Optional for backwards compatibility }); const baseEvaluationResultMetadata = z.object({ // Configuration snapshot is defined in every metric specification actualOutput: z.string(), expectedOutput: z.string().optional(), datasetLabel: z.string().optional(), }); const baseEvaluationResultError = z.object({ message: z.string(), }); const humanEvaluationConfiguration = baseEvaluationConfiguration.extend({ criteria: z.string().optional(), }); const humanEvaluationResultMetadata = baseEvaluationResultMetadata.extend({ reason: z.string().optional(), }); const humanEvaluationResultError = baseEvaluationResultError.extend({}); // BINARY const humanEvaluationBinaryConfiguration = humanEvaluationConfiguration.extend({ passDescription: z.string().optional(), failDescription: z.string().optional(), }); humanEvaluationResultMetadata.extend({ configuration: humanEvaluationBinaryConfiguration, }); humanEvaluationResultError.extend({}); const HumanEvaluationBinarySpecification = { }; // RATING const humanEvaluationRatingConfiguration = humanEvaluationConfiguration.extend({ minRating: z.number(), minRatingDescription: z.string().optional(), maxRating: z.number(), maxRatingDescription: z.string().optional(), minThreshold: z.number().optional(), // Threshold in rating range maxThreshold: z.number().optional(), // Threshold in rating range }); humanEvaluationResultMetadata.extend({ configuration: humanEvaluationRatingConfiguration, }); humanEvaluationResultError.extend({}); const HumanEvaluationRatingSpecification = { }; /* ------------------------------------------------------------------------- */ var HumanEvaluationMetric; (function (HumanEvaluationMetric) { HumanEvaluationMetric["Binary"] = "binary"; HumanEvaluationMetric["Rating"] = "rating"; })(HumanEvaluationMetric || (HumanEvaluationMetric = {})); const HumanEvaluationSpecification = { // prettier-ignore metrics: { [HumanEvaluationMetric.Binary]: HumanEvaluationBinarySpecification, [HumanEvaluationMetric.Rating]: HumanEvaluationRatingSpecification, }, }; const llmEvaluationConfiguration = baseEvaluationConfiguration.extend({ provider: z.string(), model: z.string(), }); const llmEvaluationResultMetadata = baseEvaluationResultMetadata.extend({ evaluationLogId: z.number(), reason: z.string(), tokens: z.number(), cost: z.number(), duration: z.number(), }); const llmEvaluationResultError = baseEvaluationResultError.extend({ runErrorId: z.number().optional(), }); // BINARY const llmEvaluationBinaryConfiguration = llmEvaluationConfiguration.extend({ criteria: z.string(), passDescription: z.string(), failDescription: z.string(), }); llmEvaluationResultMetadata.extend({ configuration: llmEvaluationBinaryConfiguration, }); llmEvaluationResultError.extend({}); const LlmEvaluationBinarySpecification = { }; // RATING const llmEvaluationRatingConfiguration = llmEvaluationConfiguration.extend({ criteria: z.string(), minRating: z.number(), minRatingDescription: z.string(), maxRating: z.number(), maxRatingDescription: z.string(), minThreshold: z.number().optional(), // Threshold in rating range maxThreshold: z.number().optional(), // Threshold in rating range }); llmEvaluationResultMetadata.extend({ configuration: llmEvaluationRatingConfiguration, }); llmEvaluationResultError.extend({}); const LlmEvaluationRatingSpecification = { }; // COMPARISON const llmEvaluationComparisonConfiguration = llmEvaluationConfiguration.extend({ criteria: z.string(), passDescription: z.string(), failDescription: z.string(), minThreshold: z.number().optional(), // Threshold percentage maxThreshold: z.number().optional(), // Threshold percentage }); llmEvaluationResultMetadata.extend({ configuration: llmEvaluationComparisonConfiguration, }); llmEvaluationResultError.extend({}); const LlmEvaluationComparisonSpecification = { }; // CUSTOM const llmEvaluationCustomConfiguration = llmEvaluationConfiguration.extend({ prompt: z.string(), minScore: z.number(), maxScore: z.number(), minThreshold: z.number().optional(), // Threshold percentage maxThreshold: z.number().optional(), // Threshold percentage }); llmEvaluationResultMetadata.extend({ configuration: llmEvaluationCustomConfiguration, }); llmEvaluationResultError.extend({}); const LlmEvaluationCustomSpecification = { }; // CUSTOM LABELED const LlmEvaluationCustomLabeledSpecification = { }; /* ------------------------------------------------------------------------- */ var LlmEvaluationMetric; (function (LlmEvaluationMetric) { LlmEvaluationMetric["Binary"] = "binary"; LlmEvaluationMetric["Rating"] = "rating"; LlmEvaluationMetric["Comparison"] = "comparison"; LlmEvaluationMetric["Custom"] = "custom"; LlmEvaluationMetric["CustomLabeled"] = "custom_labeled"; })(LlmEvaluationMetric || (LlmEvaluationMetric = {})); const LlmEvaluationSpecification = { // prettier-ignore metrics: { [LlmEvaluationMetric.Binary]: LlmEvaluationBinarySpecification, [LlmEvaluationMetric.Rating]: LlmEvaluationRatingSpecification, [LlmEvaluationMetric.Comparison]: LlmEvaluationComparisonSpecification, [LlmEvaluationMetric.Custom]: LlmEvaluationCustomSpecification, [LlmEvaluationMetric.CustomLabeled]: LlmEvaluationCustomLabeledSpecification, }, }; const ruleEvaluationConfiguration = baseEvaluationConfiguration.extend({}); const ruleEvaluationResultMetadata = baseEvaluationResultMetadata.extend({}); const ruleEvaluationResultError = baseEvaluationResultError.extend({}); // EXACT MATCH const ruleEvaluationExactMatchConfiguration = ruleEvaluationConfiguration.extend({ caseInsensitive: z.boolean(), }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationExactMatchConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationExactMatchSpecification = { }; // REGULAR EXPRESSION const ruleEvaluationRegularExpressionConfiguration = ruleEvaluationConfiguration.extend({ pattern: z.string(), }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationRegularExpressionConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationRegularExpressionSpecification = { }; // SCHEMA VALIDATION const ruleEvaluationSchemaValidationConfiguration = ruleEvaluationConfiguration.extend({ format: z.enum(['json']), schema: z.string(), }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationSchemaValidationConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationSchemaValidationSpecification = { }; // LENGTH COUNT const ruleEvaluationLengthCountConfiguration = ruleEvaluationConfiguration.extend({ algorithm: z.enum(['character', 'word', 'sentence']), minLength: z.number().optional(), maxLength: z.number().optional(), }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationLengthCountConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationLengthCountSpecification = { }; // LEXICAL OVERLAP const ruleEvaluationLexicalOverlapConfiguration = ruleEvaluationConfiguration.extend({ algorithm: z.enum(['substring', 'levenshtein_distance', 'rouge']), minOverlap: z.number().optional(), // Percentage of overlap maxOverlap: z.number().optional(), // Percentage of overlap }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationLexicalOverlapConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationLexicalOverlapSpecification = { }; // SEMANTIC SIMILARITY const ruleEvaluationSemanticSimilarityConfiguration = ruleEvaluationConfiguration.extend({ algorithm: z.enum(['cosine_distance']), minSimilarity: z.number().optional(), // Percentage of similarity maxSimilarity: z.number().optional(), // Percentage of similarity }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationSemanticSimilarityConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationSemanticSimilaritySpecification = { }; // NUMERIC SIMILARITY const ruleEvaluationNumericSimilarityConfiguration = ruleEvaluationConfiguration.extend({ algorithm: z.enum(['relative_difference']), minSimilarity: z.number().optional(), // Percentage of similarity maxSimilarity: z.number().optional(), // Percentage of similarity }); ruleEvaluationResultMetadata.extend({ configuration: ruleEvaluationNumericSimilarityConfiguration, }); ruleEvaluationResultError.extend({}); const RuleEvaluationNumericSimilaritySpecification = { }; /* ------------------------------------------------------------------------- */ var RuleEvaluationMetric; (function (RuleEvaluationMetric) { RuleEvaluationMetric["ExactMatch"] = "exact_match"; RuleEvaluationMetric["RegularExpression"] = "regular_expression"; RuleEvaluationMetric["SchemaValidation"] = "schema_validation"; RuleEvaluationMetric["LengthCount"] = "length_count"; RuleEvaluationMetric["LexicalOverlap"] = "lexical_overlap"; RuleEvaluationMetric["SemanticSimilarity"] = "semantic_similarity"; RuleEvaluationMetric["NumericSimilarity"] = "numeric_similarity"; })(RuleEvaluationMetric || (RuleEvaluationMetric = {})); const RuleEvaluationSpecification = { // prettier-ignore metrics: { [RuleEvaluationMetric.ExactMatch]: RuleEvaluationExactMatchSpecification, [RuleEvaluationMetric.RegularExpression]: RuleEvaluationRegularExpressionSpecification, [RuleEvaluationMetric.SchemaValidation]: RuleEvaluationSchemaValidationSpecification, [RuleEvaluationMetric.LengthCount]: RuleEvaluationLengthCountSpecification, [RuleEvaluationMetric.LexicalOverlap]: RuleEvaluationLexicalOverlapSpecification, [RuleEvaluationMetric.SemanticSimilarity]: RuleEvaluationSemanticSimilaritySpecification, [RuleEvaluationMetric.NumericSimilarity]: RuleEvaluationNumericSimilaritySpecification, }, }; var EvaluationType; (function (EvaluationType) { EvaluationType["Rule"] = "rule"; EvaluationType["Llm"] = "llm"; EvaluationType["Human"] = "human"; })(EvaluationType || (EvaluationType = {})); const EvaluationTypeSchema = z.nativeEnum(EvaluationType); const EvaluationMetricSchema = z.union([ z.nativeEnum(RuleEvaluationMetric), z.nativeEnum(LlmEvaluationMetric), z.nativeEnum(HumanEvaluationMetric), ]); const EvaluationConfigurationSchema = z.custom(); // prettier-ignore z.custom(); // prettier-ignore z.custom(); ({ [EvaluationType.Rule]: RuleEvaluationSpecification, [EvaluationType.Llm]: LlmEvaluationSpecification, [EvaluationType.Human]: HumanEvaluationSpecification, }); z.object({ name: z.string(), description: z.string(), type: EvaluationTypeSchema, metric: EvaluationMetricSchema, configuration: EvaluationConfigurationSchema, }); z.object({ evaluateLiveLogs: z.boolean().nullable().optional(), enableSuggestions: z.boolean().nullable().optional(), autoApplySuggestions: z.boolean().nullable().optional(), }); var LegacyChainEventTypes; (function (LegacyChainEventTypes) { LegacyChainEventTypes["Error"] = "chain-error"; LegacyChainEventTypes["Step"] = "chain-step"; LegacyChainEventTypes["Complete"] = "chain-complete"; LegacyChainEventTypes["StepComplete"] = "chain-step-complete"; })(LegacyChainEventTypes || (LegacyChainEventTypes = {})); var ChainEventTypes; (function (ChainEventTypes) { ChainEventTypes["ChainCompleted"] = "chain-completed"; ChainEventTypes["ChainError"] = "chain-error"; ChainEventTypes["ChainStarted"] = "chain-started"; ChainEventTypes["IntegrationWakingUp"] = "integration-waking-up"; ChainEventTypes["ProviderCompleted"] = "provider-completed"; ChainEventTypes["ProviderStarted"] = "provider-started"; ChainEventTypes["StepCompleted"] = "step-completed"; ChainEventTypes["StepStarted"] = "step-started"; ChainEventTypes["ToolCompleted"] = "tool-completed"; ChainEventTypes["ToolsRequested"] = "tools-requested"; ChainEventTypes["ToolResult"] = "tool-result"; ChainEventTypes["ToolsStarted"] = "tools-started"; })(ChainEventTypes || (ChainEventTypes = {})); var IntegrationType; (function (IntegrationType) { IntegrationType["Latitude"] = "latitude"; IntegrationType["ExternalMCP"] = "custom_mcp"; IntegrationType["HostedMCP"] = "mcp_server"; IntegrationType["Pipedream"] = "pipedream"; })(IntegrationType || (IntegrationType = {})); var HostedIntegrationType; (function (HostedIntegrationType) { HostedIntegrationType["Stripe"] = "stripe"; HostedIntegrationType["Slack"] = "slack"; HostedIntegrationType["Github"] = "github"; HostedIntegrationType["Notion"] = "notion"; HostedIntegrationType["Twitter"] = "twitter"; HostedIntegrationType["Airtable"] = "airtable"; HostedIntegrationType["Linear"] = "linear"; HostedIntegrationType["YoutubeCaptions"] = "youtube_captions"; HostedIntegrationType["Reddit"] = "reddit"; HostedIntegrationType["Telegram"] = "telegram"; HostedIntegrationType["Tinybird"] = "tinybird"; HostedIntegrationType["Perplexity"] = "perplexity"; HostedIntegrationType["AwsKbRetrieval"] = "aws_kb_retrieval"; HostedIntegrationType["BraveSearch"] = "brave_search"; HostedIntegrationType["EverArt"] = "ever_art"; HostedIntegrationType["Fetch"] = "fetch"; HostedIntegrationType["GitLab"] = "gitlab"; HostedIntegrationType["GoogleMaps"] = "google_maps"; HostedIntegrationType["Sentry"] = "sentry"; HostedIntegrationType["Puppeteer"] = "puppeteer"; HostedIntegrationType["Time"] = "time"; HostedIntegrationType["browserbase"] = "browserbase"; HostedIntegrationType["Neon"] = "neon"; HostedIntegrationType["Postgres"] = "postgres"; HostedIntegrationType["Supabase"] = "supabase"; HostedIntegrationType["Redis"] = "redis"; HostedIntegrationType["Jira"] = "jira"; HostedIntegrationType["Attio"] = "attio"; HostedIntegrationType["Ghost"] = "ghost"; HostedIntegrationType["Figma"] = "figma"; HostedIntegrationType["Hyperbrowser"] = "hyperbrowser"; HostedIntegrationType["Audiense"] = "audiense"; HostedIntegrationType["Apify"] = "apify"; HostedIntegrationType["Exa"] = "exa"; HostedIntegrationType["YepCode"] = "yepcode"; HostedIntegrationType["Monday"] = "monday"; HostedIntegrationType["AgentQL"] = "agentql"; HostedIntegrationType["AgentRPC"] = "agentrpc"; HostedIntegrationType["AstraDB"] = "astra_db"; HostedIntegrationType["Bankless"] = "bankless"; HostedIntegrationType["Bicscan"] = "bicscan"; HostedIntegrationType["Chargebee"] = "chargebee"; HostedIntegrationType["Chronulus"] = "chronulus"; HostedIntegrationType["CircleCI"] = "circleci"; HostedIntegrationType["Codacy"] = "codacy"; HostedIntegrationType["CodeLogic"] = "codelogic"; HostedIntegrationType["Convex"] = "convex"; HostedIntegrationType["Dart"] = "dart"; HostedIntegrationType["DevHubCMS"] = "devhub_cms"; HostedIntegrationType["Elasticsearch"] = "elasticsearch"; HostedIntegrationType["ESignatures"] = "esignatures"; HostedIntegrationType["Fewsats"] = "fewsats"; HostedIntegrationType["Firecrawl"] = "firecrawl"; HostedIntegrationType["Graphlit"] = "graphlit"; HostedIntegrationType["Heroku"] = "heroku"; HostedIntegrationType["IntegrationAppHubspot"] = "integration_app_hubspot"; HostedIntegrationType["LaraTranslate"] = "lara_translate"; HostedIntegrationType["Logfire"] = "logfire"; HostedIntegrationType["Langfuse"] = "langfuse"; HostedIntegrationType["LingoSupabase"] = "lingo_supabase"; HostedIntegrationType["Make"] = "make"; HostedIntegrationType["Meilisearch"] = "meilisearch"; HostedIntegrationType["Momento"] = "momento"; HostedIntegrationType["Neo4jAura"] = "neo4j_aura"; HostedIntegrationType["Octagon"] = "octagon"; HostedIntegrationType["Paddle"] = "paddle"; HostedIntegrationType["PayPal"] = "paypal"; HostedIntegrationType["Qdrant"] = "qdrant"; HostedIntegrationType["Raygun"] = "raygun"; HostedIntegrationType["Rember"] = "rember"; HostedIntegrationType["Riza"] = "riza"; HostedIntegrationType["Search1API"] = "search1api"; HostedIntegrationType["Semgrep"] = "semgrep"; HostedIntegrationType["Tavily"] = "tavily"; HostedIntegrationType["Unstructured"] = "unstructured"; HostedIntegrationType["Vectorize"] = "vectorize"; HostedIntegrationType["Xero"] = "xero"; HostedIntegrationType["Readwise"] = "readwise"; HostedIntegrationType["Airbnb"] = "airbnb"; HostedIntegrationType["Mintlify"] = "mintlify"; // Require all auth file :point_down: // Gmail = 'google_drive', // GoogleCalendar = 'google_drive', // GoogleDrive = 'google_drive', // GoogleWorkspace = 'google_workspace', // env vars not supported (?) // TODO: implement these // Wordpress = 'wordpress', // Not on OpenTools // Discord = 'discord', // Not on OpenTools // Intercom = 'intercom', // Not on OpenTools // Hubspot = 'hubspot', // Docker based // Loops = 'loops', // Does not exist })(HostedIntegrationType || (HostedIntegrationType = {})); var LogSources$1; (function (LogSources) { LogSources["API"] = "api"; LogSources["AgentAsTool"] = "agent_as_tool"; LogSources["Copilot"] = "copilot"; LogSources["EmailTrigger"] = "email_trigger"; LogSources["Evaluation"] = "evaluation"; LogSources["Experiment"] = "experiment"; LogSources["IntegrationTrigger"] = "integration_trigger"; LogSources["Playground"] = "playground"; LogSources["ScheduledTrigger"] = "scheduled_trigger"; LogSources["SharedPrompt"] = "shared_prompt"; LogSources["User"] = "user"; })(LogSources$1 || (LogSources$1 = {})); // TODO(evalsv2): Remove var EvaluationResultableType; (function (EvaluationResultableType) { EvaluationResultableType["Boolean"] = "evaluation_resultable_booleans"; EvaluationResultableType["Text"] = "evaluation_resultable_texts"; EvaluationResultableType["Number"] = "evaluation_resultable_numbers"; })(EvaluationResultableType || (EvaluationResultableType = {})); var MessageRole; (function (MessageRole) { MessageRole["system"] = "system"; MessageRole["user"] = "user"; MessageRole["assistant"] = "assistant"; MessageRole["tool"] = "tool"; })(MessageRole || (MessageRole = {})); var SpanKind; (function (SpanKind) { SpanKind["Internal"] = "internal"; SpanKind["Server"] = "server"; SpanKind["Client"] = "client"; SpanKind["Producer"] = "producer"; SpanKind["Consumer"] = "consumer"; })(SpanKind || (SpanKind = {})); // Note: loosely based on OpenTelemetry GenAI semantic conventions var SpanType; (function (SpanType) { SpanType["Tool"] = "tool"; SpanType["Completion"] = "completion"; SpanType["Embedding"] = "embedding"; SpanType["Retrieval"] = "retrieval"; SpanType["Reranking"] = "reranking"; SpanType["Http"] = "http"; SpanType["Unknown"] = "unknown"; SpanType["Prompt"] = "prompt"; SpanType["Step"] = "step"; })(SpanType || (SpanType = {})); ({ [SpanType.Tool]: { }, [SpanType.Completion]: { }, [SpanType.Embedding]: { }, [SpanType.Retrieval]: { }, [SpanType.Reranking]: { }, [SpanType.Http]: { }, [SpanType.Unknown]: { }, [SpanType.Prompt]: { }, [SpanType.Step]: { }, }); var SpanStatus; (function (SpanStatus) { SpanStatus["Unset"] = "unset"; SpanStatus["Ok"] = "ok"; SpanStatus["Error"] = "error"; })(SpanStatus || (SpanStatus = {})); // Note: Traces are unmaterialized but this context is used to propagate the trace // See www.w3.org/TR/trace-context and w3c.github.io/baggage z.object({ traceparent: z.string(), // <version>-<trace-id>-<span-id>-<trace-flags> tracestate: z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]* baggage: z.string().optional(), // <key>=urlencoded(<value>)[,<key>=urlencoded(<value>)]* }); var InstrumentationScope; (function (InstrumentationScope) { InstrumentationScope["Manual"] = "manual"; InstrumentationScope["Latitude"] = "latitude"; InstrumentationScope["OpenAI"] = "openai"; InstrumentationScope["Anthropic"] = "anthropic"; InstrumentationScope["AzureOpenAI"] = "azure"; InstrumentationScope["VercelAI"] = "vercelai"; InstrumentationScope["VertexAI"] = "vertexai"; InstrumentationScope["AIPlatform"] = "aiplatform"; InstrumentationScope["MistralAI"] = "mistralai"; InstrumentationScope["Bedrock"] = "bedrock"; InstrumentationScope["Sagemaker"] = "sagemaker"; InstrumentationScope["TogetherAI"] = "togetherai"; InstrumentationScope["Replicate"] = "replicate"; InstrumentationScope["Groq"] = "groq"; InstrumentationScope["Cohere"] = "cohere"; InstrumentationScope["LiteLLM"] = "litellm"; InstrumentationScope["Langchain"] = "langchain"; InstrumentationScope["LlamaIndex"] = "llamaindex"; InstrumentationScope["DSPy"] = "dspy"; InstrumentationScope["Haystack"] = "haystack"; InstrumentationScope["Ollama"] = "ollama"; InstrumentationScope["Transformers"] = "transformers"; InstrumentationScope["AlephAlpha"] = "alephalpha"; })(InstrumentationScope || (InstrumentationScope = {})); /* Note: Schemas for span ingestion following OpenTelemetry service request specification */ var Otlp; (function (Otlp) { Otlp.attributeValueSchema = z.object({ stringValue: z.string().optional(), intValue: z.number().optional(), boolValue: z.boolean().optional(), arrayValue: z .object({ values: z.array(z.object({ stringValue: z.string().optional(), intValue: z.number().optional(), boolValue: z.boolean().optional(), })), }) .optional(), }); Otlp.attributeSchema = z.object({ key: z.string(), value: Otlp.attributeValueSchema, }); Otlp.eventSchema = z.object({ name: z.string(), timeUnixNano: z.string(), attributes: z.array(Otlp.attributeSchema).optional(), }); Otlp.linkSchema = z.object({ traceId: z.string(), spanId: z.string(), attributes: z.array(Otlp.attributeSchema).optional(), }); (function (StatusCode) { StatusCode[StatusCode["Unset"] = 0] = "Unset"; StatusCode[StatusCode["Ok"] = 1] = "Ok"; StatusCode[StatusCode["Error"] = 2] = "Error"; })(Otlp.StatusCode || (Otlp.StatusCode = {})); Otlp.statusSchema = z.object({ code: z.number(), message: z.string().optional(), }); (function (SpanKind) { SpanKind[SpanKind["Internal"] = 0] = "Internal"; SpanKind[SpanKind["Server"] = 1] = "Server"; SpanKind[SpanKind["Client"] = 2] = "Client"; SpanKind[SpanKind["Producer"] = 3] = "Producer"; SpanKind[SpanKind["Consumer"] = 4] = "Consumer"; })(Otlp.SpanKind || (Otlp.SpanKind = {})); Otlp.spanSchema = z.object({ traceId: z.string(), spanId: z.string(), parentSpanId: z.string().optional(), name: z.string(), kind: z.number(), startTimeUnixNano: z.string(), endTimeUnixNano: z.string(), status: Otlp.statusSchema.optional(), events: z.array(Otlp.eventSchema).optional(), links: z.array(Otlp.linkSchema).optional(), attributes: z.array(Otlp.attributeSchema).optional(), }); Otlp.scopeSchema = z.object({ name: z.string(), version: z.string().optional(), }); Otlp.scopeSpanSchema = z.object({ scope: Otlp.scopeSchema, spans: z.array(Otlp.spanSchema), }); Otlp.resourceSchema = z.object({ attributes: z.array(Otlp.attributeSchema), }); Otlp.resourceSpanSchema = z.object({ resource: Otlp.resourceSchema, scopeSpans: z.array(Otlp.scopeSpanSchema), }); Otlp.serviceRequestSchema = z.object({ resourceSpans: z.array(Otlp.resourceSpanSchema), }); })(Otlp || (Otlp = {})); var ModifiedDocumentType; (function (ModifiedDocumentType) { ModifiedDocumentType["Created"] = "created"; ModifiedDocumentType["Updated"] = "updated"; ModifiedDocumentType["UpdatedPath"] = "updated_path"; ModifiedDocumentType["Deleted"] = "deleted"; })(ModifiedDocumentType || (ModifiedDocumentType = {})); // TODO(tracing): deprecated const HEAD_COMMIT = 'live'; var Providers; (function (Providers) { Providers["OpenAI"] = "openai"; Providers["Anthropic"] = "anthropic"; Providers["Groq"] = "groq"; Providers["Mistral"] = "mistral"; Providers["Azure"] = "azure"; Providers["Google"] = "google"; Providers["GoogleVertex"] = "google_vertex"; Providers["AnthropicVertex"] = "anthropic_vertex"; Providers["Custom"] = "custom"; Providers["XAI"] = "xai"; Providers["AmazonBedrock"] = "amazon_bedrock"; Providers["DeepSeek"] = "deepseek"; Providers["Perplexity"] = "perplexity"; })(Providers || (Providers = {})); var DocumentType; (function (DocumentType) { DocumentType["Prompt"] = "prompt"; DocumentType["Agent"] = "agent"; })(DocumentType || (DocumentType = {})); var DocumentTriggerType; (function (DocumentTriggerType) { DocumentTriggerType["Email"] = "email"; DocumentTriggerType["Scheduled"] = "scheduled"; DocumentTriggerType["Integration"] = "integration"; })(DocumentTriggerType || (DocumentTriggerType = {})); var DocumentTriggerParameters; (function (DocumentTriggerParameters) { DocumentTriggerParameters["SenderEmail"] = "senderEmail"; DocumentTriggerParameters["SenderName"] = "senderName"; DocumentTriggerParameters["Subject"] = "subject"; DocumentTriggerParameters["Body"] = "body"; DocumentTriggerParameters["Attachments"] = "attachments"; })(DocumentTriggerParameters || (DocumentTriggerParameters = {})); function generateEnv() { const isProd = "production" === 'production'; const defaultHostname = isProd ? 'gateway.latitude.so' : 'localhost'; const defaultPort = undefined; const defaultSsl = true ; return { GATEWAY_HOSTNAME: process.env.GATEWAY_HOSTNAME ?? defaultHostname, GATEWAY_PORT: Number(process.env.GATEWAY_PORT ?? defaultPort), GATEWAY_SSL: Boolean(process.env.GATEWAY_SSL ?? defaultSsl), }; } let sdkEnv; function createEnv() { if (sdkEnv) return sdkEnv; sdkEnv = generateEnv(); return sdkEnv; } var env = createEnv(); // TODO: Right now it takes a lot of work to add a simple new route to this file // We should refactor this to make it easier to add new routes var HandlerType; (function (HandlerType) { HandlerType["Annotate"] = "annotate"; HandlerType["Chat"] = "chat"; HandlerType["CreateDocument"] = "create-document"; HandlerType["CreateProject"] = "create-project"; HandlerType["CreateVersion"] = "create-version"; HandlerType["GetAllDocuments"] = "get-all-documents"; HandlerType["GetAllProjects"] = "get-all-projects"; HandlerType["GetDocument"] = "get-document"; HandlerType["GetOrCreateDocument"] = "get-or-create-document"; HandlerType["GetVersion"] = "get-version"; HandlerType["PushVersion"] = "push-version"; HandlerType["RunDocument"] = "run-document"; HandlerType["Log"] = "log"; HandlerType["ToolResults"] = "tool-results"; })(HandlerType || (HandlerType = {})); var LogSources; (function (LogSources) { LogSources["API"] = "api"; LogSources["Playground"] = "playground"; LogSources["Evaluation"] = "evaluation"; })(LogSources || (LogSources = {})); class RouteResolver { basePath; apiVersion; constructor({ apiVersion = 'v1', gateway, }) { const protocol = gateway.ssl ? 'https' : 'http'; const domain = gateway.port ? `${gateway.host}:${gateway.port}` : gateway.host; this.basePath = `${protocol}://${domain}`; this.apiVersion = apiVersion; } // TODO: FIXME: This can be done without asserting types resolve({ handler, params }) { switch (handler) { case HandlerType.GetDocument: { const p = params; return this.projects .project(p.projectId) .versions.version(p.versionUuid ?? 'live') .documents.document(p.path); } case HandlerType.GetAllDocuments: { const p = params; return this.projects .project(p.projectId) .versions.version(p.versionUuid ?? 'live').documents.root; } case HandlerType.GetOrCreateDocument: { const p = params; return this.projects .project(p.projectId) .versions.version(p.versionUuid ?? 'live').documents.getOrCreate; } case HandlerType.CreateDocument: { const p = params; return this.projects .project(p.projectId) .versions.version(p.versionUuid ?? 'live').documents.root; } case HandlerType.RunDocument: { const p = params; return this.projects .project(p.projectId) .versions.version(p.versionUuid ?? 'live').documents.run; } case HandlerType.Chat: { const p = params; return this.conversations().chat(p.conversationUuid); } case HandlerType.Annotate: { const p = params; return this.conversations().annotate(p.conversationUuid, p.evaluationUuid); } case HandlerType.GetAllProjects: return this.projects.root; case HandlerType.CreateProject: return this.projects.root; case HandlerType.CreateVersion: return this.projects.project(params.projectId).versions.root; case HandlerType.GetVersion: return this.projects .project(params.projectId) .versions.version(params.versionUuid).root; case HandlerType.PushVersion: return this.projects .project(params.projectId) .versions.version(params.commitUuid).push; case HandlerType.Log: return this.projects .project(params.projectId) .versions.version(params.versionUuid ?? HEAD_COMMIT).documents.logs; case HandlerType.ToolResults: return this.tools().results; default: throw new Error(`Unknown handler: ${handler}`); } } tools() { const base = `${this.baseUrl}/tools`; return { results: `${base}/results`, }; } conversations() { const base = `${this.baseUrl}/conversations`; return { chat: (uuid) => `${base}/${uuid}/chat`, annotate: (uuid, evaluationUuid) => `${base}/${uuid}/evaluations/${evaluationUuid}/annotate`, }; } get projects() { const base = `${this.baseUrl}/projects`; return { root: base, project: (projectId) => ({ root: `${this.baseUrl}/projects/${projectId}`, documents: `${this.baseUrl}/projects/${projectId}/documents`, versions: { root: `${this.baseUrl}/projects/${projectId}/versions`, version: (versionUuid) => ({ root: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}`, push: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/push`, documents: { root: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/documents`, document: (path) => `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/documents/${path}`, getOrCreate: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/documents/get-or-create`, run: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/documents/run`, logs: `${this.baseUrl}/projects/${projectId}/versions/${versionUuid}/documents/logs`, }, }), }, }), }; } get baseUrl() { return `${this.basePath}/api/${this.apiVersion}`; } } var LatitudeErrorCodes; (function (LatitudeErrorCodes) { LatitudeErrorCodes["UnexpectedError"] = "UnexpectedError"; LatitudeErrorCodes["OverloadedError"] = "OverloadedError"; LatitudeErrorCodes["RateLimitError"] = "RateLimitError"; LatitudeErrorCodes["UnauthorizedError"] = "UnauthorizedError"; LatitudeErrorCodes["ForbiddenError"] = "ForbiddenError"; LatitudeErrorCodes["BadRequestError"] = "BadRequestError"; LatitudeErrorCodes["NotFoundError"] = "NotFoundError"; LatitudeErrorCodes["ConflictError"] = "ConflictError"; LatitudeErrorCodes["UnprocessableEntityError"] = "UnprocessableEntityError"; LatitudeErrorCodes["NotImplementedError"] = "NotImplementedError"; })(LatitudeErrorCodes || (LatitudeErrorCodes = {})); // NOTE: If you add a new error code, please add it to the pg enum in models/runErrors.ts var RunErrorCodes; (function (RunErrorCodes) { RunErrorCodes["AIProviderConfigError"] = "ai_provider_config_error"; RunErrorCodes["AIRunError"] = "ai_run_error"; RunErrorCodes["ChainCompileError"] = "chain_compile_error"; RunErrorCodes["DefaultProviderExceededQuota"] = "default_provider_exceeded_quota_error"; RunErrorCodes["DefaultProviderInvalidModel"] = "default_provider_invalid_model_error"; RunErrorCodes["DocumentConfigError"] = "document_config_error"; RunErrorCodes["ErrorGeneratingMockToolResult"] = "error_generating_mock_tool_result"; RunErrorCodes["EvaluationRunMissingProviderLogError"] = "ev_run_missing_provider_log_error"; RunErrorCodes["EvaluationRunMissingWorkspaceError"] = "ev_run_missing_workspace_error"; RunErrorCodes["EvaluationRunResponseJsonFormatError"] = "ev_run_response_json_format_error"; RunErrorCodes["EvaluationRunUnsupportedResultTypeError"] = "ev_run_unsupported_result_type_error"; RunErrorCodes["FailedToWakeUpIntegrationError"] = "failed_to_wake_up_integration_error"; RunErrorCodes["InvalidResponseFormatError"] = "invalid_response_format_error"; RunErrorCodes["MaxStepCountExceededError"] = "max_step_count_exceeded_error"; RunErrorCodes["MissingProvider"] = "missing_provider_error"; RunErrorCodes["RateLimit"] = "rate_limit_error"; RunErrorCodes["Unknown"] = "unknown_error"; RunErrorCodes["UnsupportedProviderResponseTypeError"] = "unsupported_provider_response_type_error"; })(RunErrorCodes || (RunErrorCodes = {})); var ApiErrorCodes; (function (ApiErrorCodes) { ApiErrorCodes["HTTPException"] = "http_exception"; ApiErrorCodes["InternalServerError"] = "internal_server_error"; })(ApiErrorCodes || (ApiErrorCodes = {})); function getErrorMessage({ status, message, errorCode, }) { const httpExeception = ApiErrorCodes.HTTPException; const internalServerError = ApiErrorCodes.InternalServerError; const isUnexpectedError = errorCode === httpExeception || errorCode === internalServerError; if (isUnexpectedError) { return `Unexpected API Error: ${status} ${message}`; } return message; } class LatitudeApiError extends Error { status; message; serverResponse; errorCode; dbErrorRef; constructor({ status, message, serverResponse, errorCode, dbErrorRef, }) { const msg = getErrorMessage({ status, message, errorCode }); super(message); this.status = status; this.message = msg; this.serverResponse = serverResponse; this.errorCode = errorCode; this.dbErrorRef = dbErrorRef; } } // This file uses build-time replacement to inject the version from package.json const SDK_VERSION = '4.1.14'; const MAX_RETRIES = 2; function getAuthHeader(apiKey) { return { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json', 'X-Latitude-SDK-Version': SDK_VERSION, }; } function bodyToString(body = {}) { return JSON.stringify(body); } async function makeRequest({ method, handler, params, body, retries = 0, options, }) { const { routeResolver, apiKey, source, retryMs } = options; const url = routeResolver.resolve({ handler, params }); const response = await nodeFetch(url, { method, headers: getAuthHeader(apiKey), body: method === 'POST' ? bodyToString({ ...body, __internal: { source }, }) : undefined, signal: options.signal, }); if (!response.ok && response.status > 500 && retries < MAX_RETRIES) { await new Promise((resolve) => setTimeout(resolve, retryMs)); return makeRequest({ handler, params, method, body, options, retries: retries + 1, }); } return response; } function nodeFetchResponseToReadableStream(nodeStream, onError) { return new ReadableStream({ start(controller) { /** * chunk <Buffer> | <string> | <any> The chunk of data. * For streams that are not operating in object mode, the chunk will be either a string or Buffer. * For streams that are in object mode, the chunk can be any JavaScript value other than null. * * The 'data' event is emitted whenever the stream is relinquishing ownership of a chunk of data to a consumer. */ nodeStream.on('data', (chunk) => { controller.enqueue(chunk); }); /** * The 'end' event is emitted when there is no more data to be consumed from the stream. * The 'end' event will not be emitted unless the data is completely consumed. * This can be accomplished by switching the stream into flowing mode, or by calling stream.read() repeatedly until all data has been consumed. */ nodeStream.on('end', () => { controller.close(); }); /** * The 'close' event is emitted when the stream and any of its * underlying resources (a file descriptor, for example) have been closed. * The event indicates that no more events will be emitted, and no further computation will occur. * * A Readable stream will always emit the 'close' event if it is created with the emitClose option. */ nodeStream.on('close', () => { // Optionally handle the case when the stream closes unexpectedly if (!nodeStream.readableEnded && !nodeStream.errored) { try { controller.close(); } catch (e) { // controller might be closed already } } }); /** * The 'error' event may be emitted by a Readable implementation at any time. * Typically, this may occur if the underlying stream is unable to generate data due to * an underlying internal failure, or when a stream implementation attempts to push an invalid chunk of data. */ nodeStream.on('error', (err) => { try { controller.error(err); } catch (e) { // controller might be closed already if (onError) { const error = new LatitudeApiError({ status: 500, message: err.message, serverResponse: err.stack ?? '', errorCode: ApiErrorCodes.InternalServerError, }); onError(error); } } }); }, }); } async function handleStream({ body, onEvent, onError, onToolCall, }) { let conversation = []; let uuid; let chainResponse; const parser = new EventSourceParserStream(); const stream = nodeFetchResponseToReadableStream(body, onError); const eventStream = stream .pipeThrough(new TextDecoderStream()) .pipeThrough(parser); const reader = eventStream.getReader(); try { while (true) { const { done, value } = await reader.read(); if (done) break; // Skip empty values if (!value) continue; const parsedEvent = value; if (parsedEvent.type === 'event') { const data = parseJSON(parsedEvent.data); if (!data) { throw new LatitudeApiError({ status: 402, message: `Invalid JSON in server event:\n${parsedEvent.data}`, serverResponse: parsedEvent.data, errorCode: ApiErrorCodes.InternalServerError, }); } onEvent?.({ event: parsedEvent.event, data }); if (parsedEvent.event === StreamEventTypes.Latitude) { uuid = data.uuid; conversation = data.messages; if (data.type === ChainEventTypes.ChainError) { throw new LatitudeApiError({ status: 402, message: data.error.message, serverResponse: data.error.message, errorCode: RunErrorCodes.AIRunError, }); } if (data.type === ChainEventTypes.ProviderCompleted) { chainResponse = data.response; } } else if (parsedEvent.event === StreamEventTypes.Provider) { if (data.type === 'tool-call') await onToolCall(data); } } } if (!uuid || !chainResponse) { throw new Error('Stream ended without returning a provider response.'); } const finalResponse = { conversation, uuid, response: chainResponse, }; return finalResponse; } catch (e) { let error = e; if (!(e instanceof LatitudeApiError)) { const err = e; error = new LatitudeApiError({ status: 402, message: err.message, serverResponse: err.stack ?? '', errorCode: ApiErrorCodes.InternalServerError, }); } onError?.(error); throw error; } finally { try { reader.releaseLock(); } catch (e) { // Ignore errors during cleanup console.warn('Error releasing stream reader lock:', e instanceof Error ? e.message : String(e)); } } } function parseJSON(line) { try { return JSON.parse(line); } catch (e) { // do nothing } } async function streamRun(path, { projectId, versionUuid, parameters, stream = false, tools, customIdentifier, onEvent, onFinished, onError, options, }) { projectId = projectId ?? options.projectId; if (!projectId) { const error = new LatitudeApiError({ status: 404, message: 'Project ID is required', serverResponse: 'Project ID is required', errorCode: LatitudeErrorCodes.NotFoundError, }); onError?.(error); return Promise.reject(error); } versionUuid = versionUuid ?? options.versionUuid; try { const response = await makeRequest({ method: 'POST', handler: HandlerType.RunDocument, params: { projectId, versionUuid }, options, body: { stream, path, parameters, customIdentifier, tools: waitForTools(tools), }, }); if (!response.ok) { const json = (await response.json()); const error = new LatitudeApiError({ status: response.status, serverResponse: response.statusText, message: json.message, errorCode: json.errorCode, dbErrorRef: json.dbErrorRef, }); onError?.(error); return; } const finalResponse = await handleStream({ body: response.body, onEvent, onError, onToolCall: handleToolCallFactory({ tools, options, }), }); if (!finalResponse) return; onFinished?.(finalResponse); return finalResponse; } catch (e) { let error = e; if (!(e instanceof LatitudeApiError)) { const err = e; error = new LatitudeApiError({ status: 500, message: err.message, serverResponse: err.stack ?? '', errorCode: ApiErrorCodes.InternalServerError, }); } onError?.(error); return; } } function handleToolCallFactory({ tools, options, }) { return async (data) => { if (data.type !== 'tool-call') return; const tool = tools?.[data.toolName]; if (!tool) return; const result = await tool(data.args, { id: data.toolCallId, name: data.toolName, arguments: data.args, }); const response = await makeRequest({ method: 'POST', handler: HandlerType.ToolResults, body: { toolCallId: data.toolCallId, result, }, options, }); if (!response.ok) { const json = (await response.json()); const message = `Failed to execute tool ${data.toolName}. Latitude API returned the following error: ${json.message}`; const error = new LatitudeApiError({ status: response.status, serverResponse: response.statusText, message, errorCode: json.errorCode, dbErrorRef: json.dbErrorRef, }); throw error; } }; } function waitForTools(tools) { return Object.keys(tools ?? {}); } async function streamChat(uuid, { messages, onEvent, onFinished, onError, tools, options, }) { try { const response = await makeRequest({ method: 'POST', handler: HandlerType.Chat, params: { conversationUuid: uuid }, options: options, body: { messages, tools: w