-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/publish typescript sdk #250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Changesets | ||
|
||
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works | ||
with multi-package repos, or single-package repos to help you version and publish your code. You can | ||
find the full documentation for it [in our repository](https://github.com/changesets/changesets) | ||
|
||
We have a quick list of common questions to get you started engaging with this project in | ||
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json", | ||
"changelog": "@changesets/cli/changelog", | ||
"commit": false, | ||
"fixed": [], | ||
"linked": [], | ||
"access": "restricted", | ||
"baseBranch": "main", | ||
"updateInternalDependencies": "patch", | ||
"ignore": [] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@latitude-data/sdk-js': patch | ||
--- | ||
|
||
First release! Introduces `run` and `chat` commands in order interact with Latitude's Gateway |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { zValidator } from '@hono/zod-validator' | ||
import { LogSources } from '@latitude-data/core/browser' | ||
import { streamToGenerator } from '@latitude-data/core/lib/streamToGenerator' | ||
import { addMessages } from '@latitude-data/core/services/documentLogs/index' | ||
import { captureException } from '@sentry/node' | ||
import { messageSchema } from '$/common/messageSchema' | ||
import { pipeToStream } from '$/common/pipeToStream' | ||
import { Factory } from 'hono/factory' | ||
import { streamSSE } from 'hono/streaming' | ||
import { z } from 'zod' | ||
|
||
import { chainEventPresenter } from '../../projects/:projectId/commits/:commitUuid/documents/handlers/_shared' | ||
|
||
const factory = new Factory() | ||
|
||
const schema = z.object({ | ||
messages: z.array(messageSchema), | ||
documentLogUuid: z.string(), | ||
source: z.nativeEnum(LogSources).optional().default(LogSources.API), | ||
uuid: z.string(), | ||
}) | ||
|
||
export const addMessageHandler = factory.createHandlers( | ||
|
@@ -22,17 +23,26 @@ export const addMessageHandler = factory.createHandlers( | |
return streamSSE( | ||
c, | ||
async (stream) => { | ||
const { documentLogUuid, messages, source } = c.req.valid('json') | ||
const { uuid, messages } = c.req.valid('json') | ||
const workspace = c.get('workspace') | ||
|
||
const result = await addMessages({ | ||
workspace, | ||
documentLogUuid, | ||
documentLogUuid: uuid, | ||
messages, | ||
source, | ||
source: LogSources.API, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is not technically correct because we use ts sdk in playground but honestly i don't care |
||
}).then((r) => r.unwrap()) | ||
|
||
await pipeToStream(stream, result.stream) | ||
let id = 0 | ||
for await (const event of streamToGenerator(result.stream)) { | ||
const data = chainEventPresenter(event) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. internally nothing has really changed but from the public API perspective I don't want to expose users to concepts such as providerlogs or document logs, and thus I'm introducing event presenters to filter this information out |
||
|
||
stream.writeSSE({ | ||
id: String(id++), | ||
event: event.event, | ||
data: typeof data === 'string' ? data : JSON.stringify(data), | ||
}) | ||
} | ||
}, | ||
(error: Error) => { | ||
captureException(error) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
import type { Workspace } from '@latitude-data/core/browser' | ||
import { omit } from 'lodash-es' | ||
|
||
import { Message } from '@latitude-data/compiler' | ||
import { | ||
ChainEventDto, | ||
ChainEventTypes, | ||
LatitudeEventData, | ||
StreamEventTypes, | ||
type ChainEvent, | ||
type Workspace, | ||
} from '@latitude-data/core/browser' | ||
import { BadRequestError } from '@latitude-data/core/lib/errors' | ||
import { Result } from '@latitude-data/core/lib/Result' | ||
import { | ||
CommitsRepository, | ||
DocumentVersionsRepository, | ||
ProjectsRepository, | ||
} from '@latitude-data/core/repositories' | ||
import { Config } from '@latitude-data/core/services/ai/helpers' | ||
|
||
export const getData = async ({ | ||
workspace, | ||
|
@@ -19,16 +32,65 @@ export const getData = async ({ | |
const projectsScope = new ProjectsRepository(workspace.id) | ||
const commitsScope = new CommitsRepository(workspace.id) | ||
const docsScope = new DocumentVersionsRepository(workspace.id) | ||
const project = await projectsScope | ||
.getProjectById(projectId) | ||
.then((r) => r.unwrap()) | ||
const commit = await commitsScope | ||
.getCommitByUuid({ projectId: project.id, uuid: commitUuid }) | ||
.then((r) => r.unwrap()) | ||
|
||
const document = await docsScope | ||
.getDocumentByPath({ commit, path: documentPath }) | ||
.then((r) => r.unwrap()) | ||
|
||
return { project, commit, document } | ||
|
||
const projectResult = await projectsScope.getProjectById(projectId) | ||
if (projectResult.error) return projectResult | ||
const project = projectResult.value | ||
|
||
const commitResult = await commitsScope.getCommitByUuid({ | ||
projectId: project.id, | ||
uuid: commitUuid, | ||
}) | ||
if (commitResult.error) return commitResult | ||
const commit = commitResult.value | ||
|
||
const documentResult = await docsScope.getDocumentByPath({ | ||
commit, | ||
path: documentPath, | ||
}) | ||
if (documentResult.error) return documentResult | ||
const document = documentResult.value | ||
|
||
return Result.ok({ project, commit, document }) | ||
} | ||
|
||
export function chainEventPresenter(event: ChainEvent) { | ||
switch (event.event) { | ||
case StreamEventTypes.Provider: | ||
return event.data | ||
case StreamEventTypes.Latitude: | ||
return latitudeEventPresenter(event) | ||
} | ||
} | ||
|
||
function latitudeEventPresenter(event: { | ||
data: LatitudeEventData | ||
event: StreamEventTypes.Latitude | ||
}): ChainEventDto | string { | ||
switch (event.data.type) { | ||
case ChainEventTypes.Step: | ||
case ChainEventTypes.StepComplete: | ||
return { | ||
...omit(event.data, 'documentLogUuid'), | ||
uuid: event.data.documentLogUuid!, | ||
} as { | ||
type: ChainEventTypes.Step | ||
config: Config | ||
isLastStep: boolean | ||
messages: Message[] | ||
uuid?: string | ||
} | ||
case ChainEventTypes.Complete: | ||
return { | ||
...omit(event.data, 'documentLogUuid'), | ||
uuid: event.data.response.documentLogUuid!, | ||
response: omit(event.data.response, 'providerLog', 'documentLogUuid'), | ||
} | ||
case ChainEventTypes.Error: | ||
return event.data | ||
default: | ||
throw new BadRequestError( | ||
`Unknown event type in chainEventPresenter ${JSON.stringify(event)}`, | ||
) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this probably can be further simplified in the future |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ import { serve } from '@hono/node-server' | |
import env from '$/common/env' | ||
import app from '$/routes/app' | ||
|
||
import { captureException, captureMessage } from './common/sentry' | ||
|
||
serve( | ||
{ | ||
fetch: app.fetch, | ||
|
@@ -21,3 +23,11 @@ function gracefulShutdown() { | |
|
||
process.on('SIGTERM', gracefulShutdown) | ||
process.on('SIGINT', gracefulShutdown) | ||
|
||
process.on('uncaughtException', function (err) { | ||
captureException(err) | ||
}) | ||
|
||
process.on('unhandledRejection', (reason: string) => { | ||
captureMessage(reason) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. important otherwise gateway doesn't recover from any unexpected error (hono is a bit crap honestly) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public API has to be the simplest possible