From a7e588f19c63c4833f5629226313727b43843e36 Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 25 Sep 2024 09:20:15 +0200 Subject: [PATCH] feature: more sdk/gateway improvements (#259) - Do not talk about commits in sdk or public API, instead use versions. - Replaced add-message endpoint with conversations/:uuid/chat endpoint. --- apps/gateway/src/common/routes.ts | 8 +++--- apps/gateway/src/routes/api/v1/chats/index.ts | 10 ------- .../:conversationUuid/handlers/chat.test.ts} | 18 +++++++------ .../:conversationUuid/handlers/chat.ts} | 10 +++---- .../conversations/:conversationUuid/index.ts | 8 ++++++ .../documents/handlers/_shared.ts | 0 .../documents/handlers/get.test.ts | 4 +-- .../:versionUuid}/documents/handlers/get.ts | 4 +-- .../documents/handlers/run.test.ts | 4 +-- .../:versionUuid}/documents/handlers/run.ts | 4 +-- .../:versionUuid}/documents/index.ts | 0 apps/gateway/src/routes/app.ts | 6 ++--- apps/web/src/actions/sdk/runDocumentAction.ts | 7 +++-- apps/web/src/app/(private)/_lib/createSdk.ts | 4 +-- packages/sdks/typescript/package.json | 3 ++- .../src/{addMessage.test.ts => chat.test.ts} | 22 ++++++++------- packages/sdks/typescript/src/index.ts | 13 ++++----- .../sdks/typescript/src/runDocument.test.ts | 26 +++++++++--------- packages/sdks/typescript/src/utils/index.ts | 27 ++++++++++--------- packages/sdks/typescript/src/utils/types.ts | 24 ++++++++++------- 20 files changed, 106 insertions(+), 96 deletions(-) delete mode 100644 apps/gateway/src/routes/api/v1/chats/index.ts rename apps/gateway/src/routes/api/v1/{chats/handlers/addMessage.test.ts => conversations/:conversationUuid/handlers/chat.test.ts} (91%) rename apps/gateway/src/routes/api/v1/{chats/handlers/addMessage.ts => conversations/:conversationUuid/handlers/chat.ts} (80%) create mode 100644 apps/gateway/src/routes/api/v1/conversations/:conversationUuid/index.ts rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/handlers/_shared.ts (100%) rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/handlers/get.test.ts (90%) rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/handlers/get.ts (80%) rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/handlers/run.test.ts (96%) rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/handlers/run.ts (94%) rename apps/gateway/src/routes/api/v1/projects/:projectId/{commits/:commitUuid => versions/:versionUuid}/documents/index.ts (100%) rename packages/sdks/typescript/src/{addMessage.test.ts => chat.test.ts} (86%) diff --git a/apps/gateway/src/common/routes.ts b/apps/gateway/src/common/routes.ts index bb435719c..12b7cd67a 100644 --- a/apps/gateway/src/common/routes.ts +++ b/apps/gateway/src/common/routes.ts @@ -4,12 +4,12 @@ const ROUTES = { Base: '/api', V1: { Base: '/v1', - Chats: { - Base: '/chats', - AddMessage: '/add-message', + Conversations: { + Base: '/conversations', + Chat: '/:conversationUuid/chat', }, Documents: { - Base: '/projects/:projectId/commits/:commitUuid/documents', + Base: '/projects/:projectId/versions/:versionUuid/documents', Get: '/:documentPath{.+}', Run: '/run', }, diff --git a/apps/gateway/src/routes/api/v1/chats/index.ts b/apps/gateway/src/routes/api/v1/chats/index.ts deleted file mode 100644 index 57b090fae..000000000 --- a/apps/gateway/src/routes/api/v1/chats/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import ROUTES from '$/common/routes' -import { Hono } from 'hono' - -import { addMessageHandler } from './handlers/addMessage' - -const router = new Hono() - -router.post(ROUTES.Api.V1.Chats.AddMessage, ...addMessageHandler) - -export { router as chatsRouter } diff --git a/apps/gateway/src/routes/api/v1/chats/handlers/addMessage.test.ts b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.test.ts similarity index 91% rename from apps/gateway/src/routes/api/v1/chats/handlers/addMessage.test.ts rename to apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.test.ts index 91b5b5b23..3abdcb8ba 100644 --- a/apps/gateway/src/routes/api/v1/chats/handlers/addMessage.test.ts +++ b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.test.ts @@ -74,12 +74,15 @@ let apiKey: ApiKey describe('POST /add-message', () => { describe('unauthorized', () => { it('fails', async () => { - const res = await app.request('/api/v1/chats/add-message', { - method: 'POST', - body: JSON.stringify({ - path: '/path/to/document', - }), - }) + const res = await app.request( + '/api/v1/conversations/fake-document-log-uuid/chat', + { + method: 'POST', + body: JSON.stringify({ + path: '/path/to/document', + }), + }, + ) expect(res.status).toBe(401) }) @@ -98,10 +101,9 @@ describe('POST /add-message', () => { apiKey = key! token = apiKey.token - route = '/api/v1/chats/add-message' + route = '/api/v1/conversations/fake-document-log-uuid/chat' body = JSON.stringify({ messages: [], - uuid: 'fake-document-log-uuid', }) headers = { Authorization: `Bearer ${token}`, diff --git a/apps/gateway/src/routes/api/v1/chats/handlers/addMessage.ts b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.ts similarity index 80% rename from apps/gateway/src/routes/api/v1/chats/handlers/addMessage.ts rename to apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.ts index a9f703103..4ccb0fca0 100644 --- a/apps/gateway/src/routes/api/v1/chats/handlers/addMessage.ts +++ b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/handlers/chat.ts @@ -8,27 +8,27 @@ import { Factory } from 'hono/factory' import { streamSSE } from 'hono/streaming' import { z } from 'zod' -import { chainEventPresenter } from '../../projects/:projectId/commits/:commitUuid/documents/handlers/_shared' +import { chainEventPresenter } from '../../../projects/:projectId/versions/:versionUuid/documents/handlers/_shared' const factory = new Factory() const schema = z.object({ messages: z.array(messageSchema), - uuid: z.string(), }) -export const addMessageHandler = factory.createHandlers( +export const chatHandler = factory.createHandlers( zValidator('json', schema), async (c) => { return streamSSE( c, async (stream) => { - const { uuid, messages } = c.req.valid('json') + const { conversationUuid } = c.req.param() + const { messages } = c.req.valid('json') const workspace = c.get('workspace') const result = await addMessages({ workspace, - documentLogUuid: uuid, + documentLogUuid: conversationUuid, messages, source: LogSources.API, }).then((r) => r.unwrap()) diff --git a/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/index.ts b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/index.ts new file mode 100644 index 000000000..0097fb7db --- /dev/null +++ b/apps/gateway/src/routes/api/v1/conversations/:conversationUuid/index.ts @@ -0,0 +1,8 @@ +import ROUTES from '$/common/routes' +import { Hono } from 'hono' + +import { chatHandler } from './handlers/chat' + +export const chatsRouter = new Hono() + +chatsRouter.post(ROUTES.Api.V1.Conversations.Chat, ...chatHandler) diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/_shared.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/_shared.ts similarity index 100% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/_shared.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/_shared.ts diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.test.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.test.ts similarity index 90% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.test.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.test.ts index e75389d6a..b8bd89e2c 100644 --- a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.test.ts +++ b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.test.ts @@ -20,7 +20,7 @@ describe('GET documents', () => { describe('unauthorized', () => { it('fails', async () => { const res = await app.request( - '/api/v1/projects/1/commits/asldkfjhsadl/documents/path/to/document', + '/api/v1/projects/1/versions/asldkfjhsadl/documents/path/to/document', ) expect(res.status).toBe(401) @@ -54,7 +54,7 @@ describe('GET documents', () => { .getDocumentByPath({ commit, path }) .then((r) => r.unwrap()) - const route = `/api/v1/projects/${project!.id}/commits/${commit!.uuid}/documents/${document.documentVersion.path}` + const route = `/api/v1/projects/${project!.id}/versions/${commit!.uuid}/documents/${document.documentVersion.path}` const res = await app.request(route, { headers: { Authorization: `Bearer ${apikey!.token}`, diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.ts similarity index 80% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.ts index 7d16a555b..12ce772e1 100644 --- a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/get.ts +++ b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/get.ts @@ -6,12 +6,12 @@ const factory = createFactory() export const getHandler = factory.createHandlers(async (c) => { const workspace = c.get('workspace') - const { projectId, commitUuid, documentPath } = c.req.param() + const { projectId, versionUuid, documentPath } = c.req.param() const { document } = await getData({ workspace, projectId: Number(projectId!), - commitUuid: commitUuid!, + commitUuid: versionUuid!, documentPath: documentPath!, }).then((r) => r.unwrap()) diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.test.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.test.ts similarity index 96% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.test.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.test.ts index 4446265aa..543231626 100644 --- a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.test.ts +++ b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.test.ts @@ -61,7 +61,7 @@ describe('POST /run', () => { describe('unauthorized', () => { it('fails', async () => { const res = await app.request( - '/api/v1/projects/1/commits/asldkfjhsadl/documents/run', + '/api/v1/projects/1/versions/asldkfjhsadl/documents/run', { method: 'POST', body: JSON.stringify({ @@ -102,7 +102,7 @@ describe('POST /run', () => { commit = await mergeCommit(cmt).then((r) => r.unwrap()) - route = `/api/v1/projects/${project!.id}/commits/${commit!.uuid}/documents/run` + route = `/api/v1/projects/${project!.id}/versions/${commit!.uuid}/documents/run` body = JSON.stringify({ path: document.documentVersion.path, parameters: {}, diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.ts similarity index 94% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.ts index c504a99cc..6d2be2371 100644 --- a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/handlers/run.ts +++ b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/handlers/run.ts @@ -22,13 +22,13 @@ export const runHandler = factory.createHandlers( return streamSSE( c, async (stream) => { - const { projectId, commitUuid } = c.req.param() + const { projectId, versionUuid } = c.req.param() const { path, parameters } = c.req.valid('json') const workspace = c.get('workspace') const { document, commit } = await getData({ workspace, projectId: Number(projectId!), - commitUuid: commitUuid!, + commitUuid: versionUuid!, documentPath: path!, }).then((r) => r.unwrap()) const result = await runDocumentAtCommit({ diff --git a/apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/index.ts b/apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/index.ts similarity index 100% rename from apps/gateway/src/routes/api/v1/projects/:projectId/commits/:commitUuid/documents/index.ts rename to apps/gateway/src/routes/api/v1/projects/:projectId/versions/:versionUuid/documents/index.ts diff --git a/apps/gateway/src/routes/app.ts b/apps/gateway/src/routes/app.ts index f4894ad18..ca40fdcc6 100644 --- a/apps/gateway/src/routes/app.ts +++ b/apps/gateway/src/routes/app.ts @@ -5,8 +5,8 @@ import { Hono } from 'hono' import { logger } from 'hono/logger' import jetPaths from 'jet-paths' -import { chatsRouter } from './api/v1/chats' -import { documentsRouter } from './api/v1/projects/:projectId/commits/:commitUuid/documents' +import { chatsRouter } from './api/v1/conversations/:conversationUuid' +import { documentsRouter } from './api/v1/projects/:projectId/versions/:versionUuid/documents' const app = new Hono() @@ -23,7 +23,7 @@ app.use(authMiddleware()) // Routers app.route(jetPaths(ROUTES).Api.V1.Documents.Base, documentsRouter) -app.route(jetPaths(ROUTES).Api.V1.Chats.Base, chatsRouter) +app.route(jetPaths(ROUTES).Api.V1.Conversations.Base, chatsRouter) // Must be the last one! app.use(errorHandlerMiddleware()) diff --git a/apps/web/src/actions/sdk/runDocumentAction.ts b/apps/web/src/actions/sdk/runDocumentAction.ts index 7fb3c8ef2..c0ba9a2a2 100644 --- a/apps/web/src/actions/sdk/runDocumentAction.ts +++ b/apps/web/src/actions/sdk/runDocumentAction.ts @@ -1,7 +1,7 @@ 'use server' import { StreamEventTypes } from '@latitude-data/core/browser' -import { LatitudeSdk, type ChainEventDto } from '@latitude-data/sdk' +import { Latitude, type ChainEventDto } from '@latitude-data/sdk' import { createSdk } from '$/app/(private)/_lib/createSdk' import { createStreamableValue, StreamableValue } from 'ai/rsc' @@ -13,7 +13,7 @@ type RunDocumentActionProps = { } type RunDocumentResponse = Promise<{ output: StreamableValue<{ event: StreamEventTypes; data: ChainEventDto }> - response: ReturnType + response: ReturnType }> export type RunDocumentActionFn = ( _: RunDocumentActionProps, @@ -30,9 +30,8 @@ export async function runDocumentAction({ { event: StreamEventTypes; data: ChainEventDto }, Error >() - const response = sdk.run(documentPath, { - commitUuid, + versionUuid: commitUuid, parameters, onEvent: (event) => { stream.update(event) diff --git a/apps/web/src/app/(private)/_lib/createSdk.ts b/apps/web/src/app/(private)/_lib/createSdk.ts index ce5366956..9b22ea99f 100644 --- a/apps/web/src/app/(private)/_lib/createSdk.ts +++ b/apps/web/src/app/(private)/_lib/createSdk.ts @@ -1,7 +1,7 @@ import { compactObject } from '@latitude-data/core/lib/compactObject' import { Result } from '@latitude-data/core/lib/Result' import { LatitudeApiKeysRepository } from '@latitude-data/core/repositories' -import { LatitudeSdk } from '@latitude-data/sdk' +import { Latitude } from '@latitude-data/sdk' import env from '$/env' import { getCurrentUser } from '$/services/auth/getCurrentUser' @@ -28,6 +28,6 @@ export async function createSdk(projectId?: number) { ssl: env.GATEWAY_SSL, } return Result.ok( - new LatitudeSdk(latitudeApiKey, compactObject({ gateway, projectId })), + new Latitude(latitudeApiKey, compactObject({ gateway, projectId })), ) } diff --git a/packages/sdks/typescript/package.json b/packages/sdks/typescript/package.json index de9e07ca7..63c11b277 100644 --- a/packages/sdks/typescript/package.json +++ b/packages/sdks/typescript/package.json @@ -6,8 +6,9 @@ "license": "LGPL-3.0", "repository": { "type": "git", - "url": "https://github.com/latitude-dev/latitude-llm" + "url": "https://github.com/latitude-dev/latitude-llm/tree/main/packages/sdks/typescript" }, + "homepage": "https://github.com/latitude-dev/latitude-llm/tree/main/packages/sdks/typescript#readme", "scripts": { "dev": "rollup -w -c ./rollup.config.mjs", "build": "NODE_ENV=production rollup -c ./rollup.config.mjs", diff --git a/packages/sdks/typescript/src/addMessage.test.ts b/packages/sdks/typescript/src/chat.test.ts similarity index 86% rename from packages/sdks/typescript/src/addMessage.test.ts rename to packages/sdks/typescript/src/chat.test.ts index 44cb7378e..037e92ffa 100644 --- a/packages/sdks/typescript/src/addMessage.test.ts +++ b/packages/sdks/typescript/src/chat.test.ts @@ -11,13 +11,13 @@ import { vi, } from 'vitest' -import { LatitudeSdk } from './index' +import { Latitude } from './index' import { parseSSE } from './utils/parseSSE' const encoder = new TextEncoder() let latitudeApiKey = 'fake-api-key' let projectId = 123 -const SDK = new LatitudeSdk(latitudeApiKey) +const SDK = new Latitude(latitudeApiKey) const server = setupServer() @@ -31,10 +31,13 @@ describe('message', () => { server.boundary(async () => { const mockFn = vi.fn() server.use( - http.post('http://localhost:8787/api/v1/chats/add-message', (info) => { - mockFn(info.request.headers.get('Authorization')) - return HttpResponse.json({}) - }), + http.post( + 'http://localhost:8787/api/v1/conversations/fake-document-log-uuid/chat', + (info) => { + mockFn(info.request.headers.get('Authorization')) + return HttpResponse.json({}) + }, + ), ) await SDK.chat('fake-document-log-uuid', []) expect(mockFn).toHaveBeenCalledWith('Bearer fake-api-key') @@ -47,7 +50,7 @@ describe('message', () => { const onMessageMock = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/chats/add-message', + 'http://localhost:8787/api/v1/conversations/fake-document-log-uuid/chat', async () => { const stream = new ReadableStream({ start(controller) { @@ -89,7 +92,7 @@ describe('message', () => { const onFinishMock = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', async () => { const stream = new ReadableStream({ start(controller) { @@ -127,7 +130,7 @@ describe('message', () => { let body = {} server.use( http.post( - 'http://localhost:8787/api/v1/chats/add-message', + 'http://localhost:8787/api/v1/conversations/fake-document-log-uuid/chat', async (info) => { const reader = info.request.body!.getReader() while (true) { @@ -147,7 +150,6 @@ describe('message', () => { expect(mockFn).toHaveBeenCalledWith({ body: { messages: [], - uuid: 'fake-document-log-uuid', }, }) }), diff --git a/packages/sdks/typescript/src/index.ts b/packages/sdks/typescript/src/index.ts index 876667e85..9562e40ec 100644 --- a/packages/sdks/typescript/src/index.ts +++ b/packages/sdks/typescript/src/index.ts @@ -24,7 +24,7 @@ type StreamResponseCallbacks = { onError?: (error: Error) => void } -export class LatitudeSdk { +export class Latitude { private projectId?: number private apiKey: string private routeResolver: RouteResolver @@ -61,14 +61,14 @@ export class LatitudeSdk { path: string, { projectId, - commitUuid, + versionUuid, parameters, onEvent, onFinished, onError, }: { projectId?: number - commitUuid?: string + versionUuid?: string parameters?: Record } & StreamResponseCallbacks, ) { @@ -82,7 +82,7 @@ export class LatitudeSdk { const response = await this.request({ method: 'POST', handler: HandlerType.RunDocument, - params: { projectId, commitUuid }, + params: { projectId, versionUuid }, body: { path, parameters }, }) @@ -105,8 +105,9 @@ export class LatitudeSdk { ) { const response = await this.request({ method: 'POST', - handler: HandlerType.AddMessageToDocumentLog, - body: { uuid, messages }, + handler: HandlerType.Chat, + params: { conversationUuid: uuid }, + body: { messages }, }) return this.handleStream({ stream: response.body!, diff --git a/packages/sdks/typescript/src/runDocument.test.ts b/packages/sdks/typescript/src/runDocument.test.ts index cd47ebbd7..cdc3123a5 100644 --- a/packages/sdks/typescript/src/runDocument.test.ts +++ b/packages/sdks/typescript/src/runDocument.test.ts @@ -11,13 +11,13 @@ import { vi, } from 'vitest' -import { LatitudeSdk } from './index' +import { Latitude } from './index' import { parseSSE } from './utils/parseSSE' const encoder = new TextEncoder() let latitudeApiKey = 'fake-api-key' let projectId = 123 -const SDK = new LatitudeSdk(latitudeApiKey) +const SDK = new Latitude(latitudeApiKey) const server = setupServer() @@ -32,7 +32,7 @@ describe('run', () => { const mockFn = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', (info) => { mockFn(info.request.headers.get('Authorization')) return HttpResponse.json({}) @@ -52,7 +52,7 @@ describe('run', () => { const mockFn = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', (info) => { mockFn(info.request.url) return HttpResponse.json({}) @@ -63,18 +63,18 @@ describe('run', () => { projectId, }) expect(mockFn).toHaveBeenCalledWith( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', ) }), ) it( - 'sends request with specific commitUuid', + 'sends request with specific versionUuid', server.boundary(async () => { const mockFn = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/SOME_UUID/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/SOME_UUID/documents/run', (info) => { mockFn(info.request.url) return HttpResponse.json({}) @@ -83,10 +83,10 @@ describe('run', () => { ) await SDK.run('path/to/document', { projectId, - commitUuid: 'SOME_UUID', + versionUuid: 'SOME_UUID', }) expect(mockFn).toHaveBeenCalledWith( - 'http://localhost:8787/api/v1/projects/123/commits/SOME_UUID/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/SOME_UUID/documents/run', ) }), ) @@ -97,7 +97,7 @@ describe('run', () => { const mockFn = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/SOME_UUID/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/SOME_UUID/documents/run', async (info) => { const body = await info.request.json() mockFn(body) @@ -107,7 +107,7 @@ describe('run', () => { ) await SDK.run('path/to/document', { projectId, - commitUuid: 'SOME_UUID', + versionUuid: 'SOME_UUID', parameters: { foo: 'bar', lol: 'foo' }, }) expect(mockFn).toHaveBeenCalledWith({ @@ -123,7 +123,7 @@ describe('run', () => { const onMessageMock = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', async () => { const stream = new ReadableStream({ start(controller) { @@ -166,7 +166,7 @@ describe('run', () => { const onFinishMock = vi.fn() server.use( http.post( - 'http://localhost:8787/api/v1/projects/123/commits/live/documents/run', + 'http://localhost:8787/api/v1/projects/123/versions/live/documents/run', async () => { const stream = new ReadableStream({ start(controller) { diff --git a/packages/sdks/typescript/src/utils/index.ts b/packages/sdks/typescript/src/utils/index.ts index ed2ec9403..4e67203ff 100644 --- a/packages/sdks/typescript/src/utils/index.ts +++ b/packages/sdks/typescript/src/utils/index.ts @@ -1,4 +1,5 @@ import { + ChatUrlParams, GetDocumentUrlParams, HandlerType, RunUrlParams, @@ -39,41 +40,43 @@ export class RouteResolver { return this.documents(params as RunUrlParams).run case HandlerType.GetDocument: { const getParams = params as GetDocumentUrlParams - return this.documents(getParams).document(getParams.documentPath) + return this.documents(getParams).document(getParams.path) } - case HandlerType.AddMessageToDocumentLog: - return this.chats().addMessage + case HandlerType.Chat: + return this.conversations().chat( + (params as ChatUrlParams).conversationUuid, + ) default: throw new Error(`Unknown handler: ${handler satisfies never}`) } } - private chats() { - const base = `${this.baseUrl}/chats` + private conversations() { + const base = `${this.baseUrl}/conversations` return { - addMessage: `${base}/add-message`, + chat: (uuid: string) => `${base}/${uuid}/chat`, } } - private documents(params: { projectId: number; commitUuid?: string }) { + private documents(params: { projectId: number; versionUuid?: string }) { const base = `${this.commitsUrl(params)}/documents` return { run: `${base}/run`, - document: (documentPath: string) => `${base}/${documentPath}`, + document: (path: string) => `${base}/${path}`, } } private commitsUrl({ projectId, - commitUuid, + versionUuid, }: { projectId: number - commitUuid?: string + versionUuid?: string }) { // TODO: Think how to share HEAD_COMMIT constant from core // I don't want to require all core just for this - const commit = commitUuid ?? 'live' - return `${this.projectsUrl({ projectId })}/commits/${commit}` + const commit = versionUuid ?? 'live' + return `${this.projectsUrl({ projectId })}/versions/${commit}` } private projectsUrl({ projectId }: { projectId: number }) { diff --git a/packages/sdks/typescript/src/utils/types.ts b/packages/sdks/typescript/src/utils/types.ts index 0708fa962..d13c219a4 100644 --- a/packages/sdks/typescript/src/utils/types.ts +++ b/packages/sdks/typescript/src/utils/types.ts @@ -7,39 +7,43 @@ export type { } from '@latitude-data/core/browser' export type RunUrlParams = { projectId: number - commitUuid?: string + versionUuid?: string +} +export type ChatUrlParams = { + conversationUuid: string } type RunDocumentBodyParam = { path: string parameters?: Record } -type AddMessageBodyParam = { - uuid: string +type ChatBodyParams = { messages: Message[] } export type GetDocumentUrlParams = { projectId: number - commitUuid?: string - documentPath: string + versionUuid?: string + path: string } export enum HandlerType { - RunDocument = 'run-document', - AddMessageToDocumentLog = 'add-message-to-document-log', + Chat = 'chat', GetDocument = 'get-document', + RunDocument = 'run-document', } export type UrlParams = T extends HandlerType.RunDocument ? RunUrlParams : T extends HandlerType.GetDocument ? GetDocumentUrlParams - : never + : T extends HandlerType.Chat + ? ChatUrlParams + : never export type BodyParams = T extends HandlerType.RunDocument ? RunDocumentBodyParam - : T extends HandlerType.AddMessageToDocumentLog - ? AddMessageBodyParam + : T extends HandlerType.Chat + ? ChatBodyParams : never