Skip to content

Commit cc741e8

Browse files
committed
Refactor the API
1 parent fd91528 commit cc741e8

File tree

4 files changed

+83
-54
lines changed

4 files changed

+83
-54
lines changed

src/chat-workspace.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { HttpRequests } from "./http-requests.js";
2+
import type {
3+
ChatWorkspaceSettings,
4+
ChatCompletionRequest,
5+
} from "./types/types.js";
6+
7+
/**
8+
* Class for handling chat workspaces.
9+
*
10+
* @see {@link https://www.meilisearch.com/docs/reference/api/chats}
11+
*/
12+
export class ChatWorkspace {
13+
readonly #httpRequest: HttpRequests;
14+
readonly #workspace: string;
15+
16+
constructor(httpRequests: HttpRequests, workspace: string) {
17+
this.#httpRequest = httpRequests;
18+
this.#workspace = workspace;
19+
}
20+
21+
async get(): Promise<ChatWorkspaceSettings> {
22+
return await this.#httpRequest.get({
23+
path: `chats/${this.#workspace}/settings`,
24+
});
25+
}
26+
27+
async update(
28+
settings: Partial<ChatWorkspaceSettings>,
29+
): Promise<ChatWorkspaceSettings> {
30+
return await this.#httpRequest.patch({
31+
path: `chats/${this.#workspace}/settings`,
32+
body: settings,
33+
});
34+
}
35+
36+
async streamCompletion(
37+
completion: ChatCompletionRequest,
38+
): Promise<ReadableStream<Uint8Array>> {
39+
if (!completion.stream) {
40+
throw new Error("The SDK only support streaming");
41+
}
42+
return await this.#httpRequest.postStream({
43+
path: `chats/${this.#workspace}/chat/completions`,
44+
body: completion,
45+
});
46+
}
47+
}

src/meilisearch.ts

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import type {
2929
Network,
3030
RecordAny,
3131
RuntimeTogglableFeatures,
32-
WorkspaceSettings,
33-
ChatCompletion,
3432
ResourceResults,
3533
} from "./types/index.js";
3634
import { ErrorStatusCode } from "./types/index.js";
@@ -41,6 +39,7 @@ import {
4139
type HttpRequestsWithEnqueuedTaskPromise,
4240
} from "./task.js";
4341
import { BatchClient } from "./batch.js";
42+
import { ChatWorkspace } from "./chat-workspace.js";
4443
import type { MeiliSearchApiError } from "./errors/index.js";
4544

4645
export class MeiliSearch {
@@ -277,6 +276,31 @@ export class MeiliSearch {
277276
});
278277
}
279278

279+
///
280+
/// CHATS
281+
///
282+
283+
/**
284+
* Get a chat workspace instance
285+
*
286+
* @param workspace - The chat workspace UID
287+
* @returns Instance of ChatWorkspace
288+
*/
289+
chat(workspace: string): ChatWorkspace {
290+
return new ChatWorkspace(this.httpRequest, workspace);
291+
}
292+
293+
/**
294+
* Get all chat workspaces
295+
*
296+
* @returns Promise returning an array of chat workspaces UIDs
297+
*/
298+
async getChatWorkspaces(): Promise<ResourceResults<{ uid: string }[]>> {
299+
return await this.httpRequest.get({
300+
path: "chats",
301+
});
302+
}
303+
280304
///
281305
/// Network
282306
///
@@ -302,45 +326,6 @@ export class MeiliSearch {
302326
});
303327
}
304328

305-
///
306-
/// CHATS
307-
///
308-
309-
async getWorkspaceSettings(workspace: string): Promise<WorkspaceSettings> {
310-
return await this.httpRequest.get({
311-
path: `chats/${workspace}/settings`,
312-
});
313-
}
314-
315-
async updateWorkspaceSettings(
316-
workspace: string,
317-
settings: Partial<WorkspaceSettings>,
318-
): Promise<WorkspaceSettings> {
319-
return await this.httpRequest.patch({
320-
path: `chats/${workspace}/settings`,
321-
body: settings,
322-
});
323-
}
324-
325-
async listWorkspaces(): Promise<ResourceResults<{ uid: string }[]>> {
326-
return await this.httpRequest.get({
327-
path: "chats",
328-
});
329-
}
330-
331-
async createChatCompletion(
332-
workspace: string,
333-
completion: ChatCompletion,
334-
): Promise<ReadableStream<Uint8Array>> {
335-
if (!completion.stream) {
336-
throw new Error("The SDK only support streaming");
337-
}
338-
return await this.httpRequest.postStream({
339-
path: `chats/${workspace}/chat/completions`,
340-
body: completion,
341-
});
342-
}
343-
344329
///
345330
/// KEYS
346331
///

src/types/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ export type Stats = {
704704
*/
705705

706706
/** @see https://www.meilisearch.com/docs/reference/api/chats#settings-parameters */
707-
export type WorkspaceSettings = {
707+
export type ChatWorkspaceSettings = {
708708
source: "openAi" | "azureOpenAi" | "mistral" | "gemini" | "vLlm";
709709
orgId?: string;
710710
projectId?: string;
@@ -717,7 +717,7 @@ export type WorkspaceSettings = {
717717
};
718718
};
719719

720-
export type ChatCompletion = {
720+
export type ChatCompletionRequest = {
721721
model: string;
722722
messages: {
723723
role: "user" | "assistant" | "system";

tests/chat.test.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeAll, expect, test } from "vitest";
22
import { getClient, dataset } from "./utils/meilisearch-test-utils.js";
3-
import type { WorkspaceSettings } from "../src/types/types.js";
3+
import type { ChatWorkspaceSettings } from "../src/types/types.js";
44

55
beforeAll(async () => {
66
const client = await getClient("Admin");
@@ -27,7 +27,7 @@ const WORKSPACE_SETTINGS = {
2727
system:
2828
"You are a helpful assistant that answers questions based on the provided context.",
2929
},
30-
} satisfies WorkspaceSettings;
30+
} satisfies ChatWorkspaceSettings;
3131

3232
const WORKSPACE_SETTINGS_WITHOUT_API_KEY = {
3333
...WORKSPACE_SETTINGS,
@@ -38,30 +38,27 @@ const WORKSPACE_SETTINGS_WITHOUT_API_KEY = {
3838
test("it can update workspace settings", async () => {
3939
const client = await getClient("Admin");
4040

41-
const response = await client.updateWorkspaceSettings(
42-
"myWorkspace",
43-
WORKSPACE_SETTINGS,
44-
);
41+
const response = await client.chat("myWorkspace").update(WORKSPACE_SETTINGS);
4542
expect(response).toMatchObject(WORKSPACE_SETTINGS_WITHOUT_API_KEY);
4643
});
4744

4845
test("it can get workspace settings", async () => {
4946
const client = await getClient("Admin");
50-
await client.updateWorkspaceSettings("myWorkspace", WORKSPACE_SETTINGS);
51-
const response = await client.getWorkspaceSettings("myWorkspace");
47+
await client.chat("myWorkspace").update(WORKSPACE_SETTINGS);
48+
const response = await client.chat("myWorkspace").get();
5249
expect(response).toMatchObject(WORKSPACE_SETTINGS_WITHOUT_API_KEY);
5350
});
5451

5552
test("it can list workspaces", async () => {
5653
const client = await getClient("Admin");
57-
await client.updateWorkspaceSettings("myWorkspace", WORKSPACE_SETTINGS);
58-
const response = await client.listWorkspaces();
54+
await client.chat("myWorkspace").update(WORKSPACE_SETTINGS);
55+
const response = await client.getChatWorkspaces();
5956
expect(response.results).toEqual([{ uid: "myWorkspace" }]);
6057
});
6158

6259
test("it can create a chat completion (streaming)", async () => {
6360
const client = await getClient("Chat");
64-
const stream = await client.createChatCompletion("myWorkspace", {
61+
const stream = await client.chat("myWorkspace").streamCompletion({
6562
model: "gpt-4o-mini",
6663
messages: [
6764
{

0 commit comments

Comments
 (0)