assistant-cloud 0.1.40 → 0.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. package/dist/AssistantCloudAuthStrategy.d.ts.map +1 -1
  2. package/dist/AssistantCloudAuthStrategy.js +35 -12
  3. package/dist/AssistantCloudAuthStrategy.js.map +1 -1
  4. package/dist/AssistantCloudAuthTokens.d.ts.map +1 -1
  5. package/dist/AssistantCloudAuthTokens.js +3 -1
  6. package/dist/AssistantCloudAuthTokens.js.map +1 -1
  7. package/dist/AssistantCloudFiles.d.ts.map +1 -1
  8. package/dist/AssistantCloudFiles.js +16 -4
  9. package/dist/AssistantCloudFiles.js.map +1 -1
  10. package/dist/AssistantCloudRuns.d.ts.map +1 -1
  11. package/dist/AssistantCloudRuns.js +10 -2
  12. package/dist/AssistantCloudRuns.js.map +1 -1
  13. package/dist/AssistantCloudThreadMessages.d.ts.map +1 -1
  14. package/dist/AssistantCloudThreadMessages.js +3 -2
  15. package/dist/AssistantCloudThreadMessages.js.map +1 -1
  16. package/dist/AssistantCloudThreads.d.ts.map +1 -1
  17. package/dist/AssistantCloudThreads.js +3 -2
  18. package/dist/AssistantCloudThreads.js.map +1 -1
  19. package/package.json +4 -4
  20. package/src/AssistantCloudAuthStrategy.ts +49 -12
  21. package/src/AssistantCloudAuthTokens.test.ts +30 -0
  22. package/src/AssistantCloudAuthTokens.ts +7 -1
  23. package/src/AssistantCloudFiles.test.ts +71 -0
  24. package/src/AssistantCloudFiles.ts +56 -10
  25. package/src/AssistantCloudRuns.ts +34 -1
  26. package/src/AssistantCloudThreadMessages.test.ts +20 -0
  27. package/src/AssistantCloudThreadMessages.ts +10 -3
  28. package/src/AssistantCloudThreads.test.ts +17 -0
  29. package/src/AssistantCloudThreads.ts +6 -1
  30. package/src/tests/AssistantCloudAuthStrategy.test.ts +278 -5
  31. package/src/tests/AssistantCloudRuns.test.ts +115 -0
@@ -0,0 +1,115 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import type { AssistantCloudAPI } from "../AssistantCloudAPI";
3
+ import { AssistantCloud } from "../AssistantCloud";
4
+ import { AssistantCloudRuns } from "../AssistantCloudRuns";
5
+ import { CloudResponseError } from "../cloudResponse";
6
+
7
+ const createCloud = () =>
8
+ new AssistantCloud({
9
+ apiKey: "test-key",
10
+ userId: "user-id",
11
+ workspaceId: "workspace-id",
12
+ });
13
+
14
+ const streamBody = {
15
+ thread_id: "thread-id",
16
+ assistant_id: "system/thread_title" as const,
17
+ messages: [],
18
+ };
19
+
20
+ describe("AssistantCloudRuns", () => {
21
+ afterEach(() => {
22
+ vi.unstubAllGlobals();
23
+ });
24
+
25
+ it("decodes text/plain run streams", async () => {
26
+ vi.stubGlobal(
27
+ "fetch",
28
+ vi.fn().mockResolvedValue(
29
+ new Response("Generated title", {
30
+ headers: { "Content-Type": "text/plain; charset=utf-8" },
31
+ }),
32
+ ),
33
+ );
34
+ const stream = await createCloud().runs.stream(streamBody);
35
+ let text = "";
36
+
37
+ await stream.pipeTo(
38
+ new WritableStream({
39
+ write(chunk) {
40
+ if (chunk.type === "text-delta") text += chunk.textDelta;
41
+ },
42
+ }),
43
+ );
44
+
45
+ expect(text).toBe("Generated title");
46
+ });
47
+
48
+ it("rejects successful run responses without a body", async () => {
49
+ vi.stubGlobal(
50
+ "fetch",
51
+ vi.fn().mockResolvedValue(new Response(null, { status: 204 })),
52
+ );
53
+
54
+ await expect(createCloud().runs.stream(streamBody)).rejects.toThrow(
55
+ new CloudResponseError(
56
+ 'Invalid Assistant Cloud response for "run stream": expected a response body',
57
+ ),
58
+ );
59
+ });
60
+
61
+ it("rejects and cancels run responses with the wrong content type", async () => {
62
+ const cancel = vi.fn();
63
+ const body = new ReadableStream({ cancel });
64
+ vi.stubGlobal(
65
+ "fetch",
66
+ vi.fn().mockResolvedValue(
67
+ new Response(body, {
68
+ headers: { "Content-Type": "text/html; charset=utf-8" },
69
+ }),
70
+ ),
71
+ );
72
+
73
+ await expect(createCloud().runs.stream(streamBody)).rejects.toThrow(
74
+ new CloudResponseError(
75
+ 'Invalid Assistant Cloud response for "run stream": expected a "text/plain" content type, received "text/html; charset=utf-8"',
76
+ ),
77
+ );
78
+ expect(cancel).toHaveBeenCalledOnce();
79
+ });
80
+
81
+ it("rejects and cancels run responses without a content type", async () => {
82
+ const cancel = vi.fn();
83
+ const body = new ReadableStream({ cancel });
84
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(body)));
85
+
86
+ await expect(createCloud().runs.stream(streamBody)).rejects.toThrow(
87
+ new CloudResponseError(
88
+ 'Invalid Assistant Cloud response for "run stream": expected a "text/plain" content type, received no Content-Type header',
89
+ ),
90
+ );
91
+ expect(cancel).toHaveBeenCalledOnce();
92
+ });
93
+ });
94
+
95
+ const createCloudRuns = () => {
96
+ const makeRequest = vi.fn();
97
+ const api = { makeRequest } as unknown as AssistantCloudAPI;
98
+ return { runs: new AssistantCloudRuns(api), makeRequest };
99
+ };
100
+
101
+ describe("AssistantCloudRuns responses", () => {
102
+ it("validates reported run IDs", async () => {
103
+ const { runs, makeRequest } = createCloudRuns();
104
+ const body = { thread_id: "thread-1", status: "completed" } as const;
105
+ makeRequest.mockResolvedValueOnce({ run_id: "run-1" });
106
+
107
+ await expect(runs.report(body)).resolves.toEqual({ run_id: "run-1" });
108
+
109
+ makeRequest.mockResolvedValueOnce({});
110
+
111
+ await expect(runs.report(body)).rejects.toThrow(
112
+ 'Invalid Assistant Cloud response for "run_id": expected a string',
113
+ );
114
+ });
115
+ });