-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6218b9b
commit acffc2c
Showing
81 changed files
with
1,578 additions
and
757 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { OpenAPIHono } from '@hono/zod-openapi' | ||
|
||
import { swaggerUI } from '@hono/swagger-ui' | ||
|
||
import packageJson from '../../package.json' | ||
import { tags } from '$/openApi/tags' | ||
|
||
const isDev = process.env.NODE_ENV === 'development' | ||
let servers = [ | ||
{ url: 'https://gateway.latitude.so', description: 'Latitude production' }, | ||
] | ||
|
||
if (isDev) { | ||
servers = [ | ||
{ url: 'http://localhost:8787', description: 'Latitude development' }, | ||
...servers, | ||
] | ||
} | ||
|
||
export const openAPIObjectConfig = { | ||
openapi: '3.1.0', | ||
info: { title: 'Latitude API', version: packageJson.version }, | ||
tags: tags, | ||
security: [{ Bearer: [] }], | ||
externalDocs: { | ||
url: 'https://docs.latitude.so', | ||
description: 'Latitude Documentation', | ||
}, | ||
servers, | ||
} | ||
|
||
export default function configureOpenAPI(app: OpenAPIHono) { | ||
app.openAPIRegistry.registerComponent('securitySchemes', 'Bearer', { | ||
type: 'http', | ||
scheme: 'bearer', | ||
bearerFormat: 'token', | ||
description: 'Latitude API Key', | ||
}) | ||
|
||
app.doc31('/doc', openAPIObjectConfig) | ||
app.get( | ||
'/ui', | ||
swaggerUI({ | ||
url: '/doc', | ||
docExpansion: 'list', | ||
requestSnippetsEnabled: true, | ||
syntaxHighlight: { | ||
activated: true, | ||
theme: ['nord'], | ||
}, | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { OpenAPIHono } from '@hono/zod-openapi' | ||
|
||
export function createRouter() { | ||
return new OpenAPIHono({ strict: false }) | ||
} | ||
|
||
export default function createApp() { | ||
return new OpenAPIHono({ strict: false }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import http from '$/common/http' | ||
import { | ||
BadRequestErrorSchema, | ||
HTTPExceptionErrorSchema, | ||
InternalServerErrorSchema, | ||
UnprocessableEntityErrorSchema, | ||
} from '$/openApi/schemas' | ||
|
||
export const GENERIC_ERROR_RESPONSES = { | ||
[http.Status.NOT_FOUND]: { | ||
description: 'The requested resource was not found.', | ||
content: { | ||
[http.MediaTypes.JSON]: { | ||
schema: HTTPExceptionErrorSchema, | ||
}, | ||
}, | ||
}, | ||
[http.Status.UNPROCESSABLE_ENTITY]: { | ||
description: 'The request was valid but could not be processed.', | ||
content: { | ||
[http.MediaTypes.JSON]: { | ||
schema: UnprocessableEntityErrorSchema, | ||
}, | ||
}, | ||
}, | ||
[http.Status.BAD_REQUEST]: { | ||
description: 'The request was invalid or cannot be processed.', | ||
content: { | ||
[http.MediaTypes.JSON]: { | ||
schema: BadRequestErrorSchema, | ||
}, | ||
}, | ||
}, | ||
[http.Status.INTERNAL_SERVER_ERROR]: { | ||
description: 'An unexpected error occurred.', | ||
content: { | ||
[http.MediaTypes.JSON]: { | ||
schema: InternalServerErrorSchema, | ||
}, | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { z } from '@hono/zod-openapi' | ||
import { ChainEventTypes, StreamEventTypes } from '@latitude-data/constants' | ||
import { messageSchema } from '@latitude-data/core/browser' | ||
|
||
export const languageModelUsageSchema = z.object({ | ||
completionTokens: z.number().optional(), | ||
promptTokens: z.number().optional(), | ||
totalTokens: z.number().optional(), | ||
}) | ||
|
||
export const toolCallSchema = z.object({ | ||
id: z.string(), | ||
name: z.string(), | ||
arguments: z.record(z.any()), | ||
}) | ||
|
||
export const configSchema = z.object({}).passthrough() | ||
export const providerLogSchema = z.object({}).passthrough() | ||
export const chainStepResponseSchema = z.discriminatedUnion('streamType', [ | ||
z.object({ | ||
streamType: z.literal('text'), | ||
text: z.string(), | ||
usage: languageModelUsageSchema, | ||
toolCalls: z.array(toolCallSchema), | ||
documentLogUuid: z.string().optional(), | ||
providerLog: providerLogSchema.optional(), | ||
}), | ||
z.object({ | ||
streamType: z.literal('object'), | ||
object: z.any(), | ||
text: z.string(), | ||
usage: languageModelUsageSchema, | ||
documentLogUuid: z.string().optional(), | ||
providerLog: providerLogSchema.optional(), | ||
}), | ||
]) | ||
|
||
export const chainCallResponseDtoSchema = z.discriminatedUnion('streamType', [ | ||
chainStepResponseSchema.options[0].omit({ | ||
documentLogUuid: true, | ||
providerLog: true, | ||
}), | ||
chainStepResponseSchema.options[1].omit({ | ||
documentLogUuid: true, | ||
providerLog: true, | ||
}), | ||
]) | ||
|
||
export const chainEventDtoResponseSchema = z.discriminatedUnion('streamType', [ | ||
chainStepResponseSchema.options[0].omit({ providerLog: true }), | ||
chainStepResponseSchema.options[1].omit({ providerLog: true }), | ||
]) | ||
|
||
export const chainEventDtoSchema = z.discriminatedUnion('event', [ | ||
z.object({ | ||
event: z.literal(StreamEventTypes.Provider), | ||
data: z.object({}).passthrough(), | ||
}), | ||
z.object({ | ||
event: z.literal(StreamEventTypes.Latitude), | ||
data: z.discriminatedUnion('type', [ | ||
z.object({ | ||
type: z.literal(ChainEventTypes.Step), | ||
config: configSchema, | ||
isLastStep: z.boolean(), | ||
messages: z.array(messageSchema), | ||
uuid: z.string().optional(), | ||
}), | ||
z.object({ | ||
type: z.literal(ChainEventTypes.StepComplete), | ||
response: chainEventDtoResponseSchema, | ||
uuid: z.string().optional(), | ||
}), | ||
z.object({ | ||
type: z.literal(ChainEventTypes.Complete), | ||
config: configSchema, | ||
messages: z.array(messageSchema).optional(), | ||
object: z.object({}).passthrough().optional(), | ||
response: chainEventDtoResponseSchema, | ||
uuid: z.string().optional(), | ||
}), | ||
z.object({ | ||
type: z.literal(ChainEventTypes.Error), | ||
error: z.object({ | ||
name: z.string(), | ||
message: z.string(), | ||
stack: z.string().optional(), | ||
}), | ||
}), | ||
]), | ||
}), | ||
]) | ||
|
||
export const runSyncAPIResponseSchema = z.object({ | ||
uuid: z.string(), | ||
conversation: z.array(messageSchema), | ||
response: chainCallResponseDtoSchema, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { z } from '@hono/zod-openapi' | ||
|
||
const ChainErrorDetailSchema = z.object({ | ||
entityUuid: z | ||
.string() | ||
.uuid() | ||
.openapi({ description: 'UUID of the related entity' }), | ||
entityType: z.string().openapi({ description: 'Type of the related entity' }), | ||
}) | ||
|
||
const BaseErrorSchema = z.object({ | ||
name: z.string().openapi({ description: 'The name of the error' }), | ||
errorCode: z.string().openapi({ description: 'The error code identifier' }), | ||
message: z.string().openapi({ description: 'Detailed error message' }), | ||
details: z | ||
.object({}) | ||
.passthrough() | ||
.optional() | ||
.openapi({ description: 'Additional error details' }), | ||
}) | ||
|
||
const HTTPExceptionErrorSchema = BaseErrorSchema.extend({ | ||
details: z.object({ cause: z.any().optional() }).optional(), | ||
}).openapi({ | ||
description: 'Error response for HTTP exceptions', | ||
example: { | ||
name: 'HTTPException', | ||
errorCode: 'HTTPException', | ||
message: 'Not Found', | ||
details: { cause: 'Resource not found' }, | ||
}, | ||
}) | ||
|
||
const UnprocessableEntityErrorSchema = BaseErrorSchema.extend({ | ||
details: z.any().optional(), | ||
}) | ||
.and(z.object({ dbErrorRef: ChainErrorDetailSchema }).optional()) | ||
.openapi({ | ||
description: 'Error response for unprocessable entities', | ||
example: { | ||
name: 'DocumentRunError', | ||
errorCode: 'SomeErrorCode', | ||
message: 'Validation failed', | ||
details: {}, | ||
dbErrorRef: { | ||
entityUuid: '123e4567-e89b-12d3-a456-426614174000', | ||
entityType: 'Document', | ||
}, | ||
}, | ||
}) | ||
|
||
const BadRequestErrorSchema = BaseErrorSchema.extend({ | ||
details: z.any().optional(), | ||
}).openapi({ | ||
description: 'Error response for Latitude-specific errors', | ||
example: { | ||
name: 'LatitudeError', | ||
errorCode: 'LatitudeError', | ||
message: 'A latitude-specific error occurred', | ||
details: {}, | ||
}, | ||
}) | ||
|
||
const InternalServerErrorSchema = BaseErrorSchema.extend({ | ||
details: z | ||
.object({ | ||
cause: z.any().optional(), // Adjust `z.any()` to a more specific schema if possible | ||
}) | ||
.optional(), | ||
}).openapi({ | ||
description: 'Error response for internal server errors', | ||
example: { | ||
name: 'InternalServerError', | ||
errorCode: 'InternalServerError', | ||
message: 'An unexpected error occurred', | ||
details: { cause: 'Null reference exception' }, | ||
}, | ||
}) | ||
|
||
export { | ||
HTTPExceptionErrorSchema, | ||
UnprocessableEntityErrorSchema, | ||
BadRequestErrorSchema, | ||
InternalServerErrorSchema, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './errors' | ||
export * from './utils' | ||
export * from './ai' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from '@hono/zod-openapi' | ||
import { LogSources } from '@latitude-data/core/browser' | ||
|
||
export const internalInfoSchema = z.object({ | ||
__internal: z | ||
.object({ | ||
source: z.nativeEnum(LogSources).optional(), | ||
}) | ||
.optional(), | ||
}) |
Oops, something went wrong.