@mycadet/assistant 0.12.10 → 0.13.0

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 (180) hide show
  1. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +10 -10
  2. package/node_modules/@mycadet/ces-client/node_modules/@mycadet/service-contracts/src/ingress.ts +3 -3
  3. package/node_modules/@mycadet/ces-client/src/http-credentials.ts +18 -1
  4. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +10 -10
  5. package/node_modules/@mycadet/gateway-client/node_modules/@mycadet/service-contracts/src/ingress.ts +3 -3
  6. package/node_modules/@mycadet/gateway-client/src/gateway-ipc-contracts.ts +0 -20
  7. package/node_modules/@mycadet/service-contracts/src/__tests__/ingress.test.ts +10 -10
  8. package/node_modules/@mycadet/service-contracts/src/ingress.ts +3 -3
  9. package/openapi.yaml +2 -708
  10. package/package.json +2 -3
  11. package/scripts/sync-web-search-catalog.ts +1 -2
  12. package/src/__tests__/cloud-profile-guard.test.ts +41 -5
  13. package/src/__tests__/config-schema.test.ts +6 -38
  14. package/src/__tests__/conversation-queue.test.ts +45 -6
  15. package/src/__tests__/db-readiness-http-gate.test.ts +0 -6
  16. package/src/__tests__/persist-onboarding-artifacts.test.ts +0 -1
  17. package/src/__tests__/persona-resolver.test.ts +0 -51
  18. package/src/__tests__/provider-cloud-proxy-integration.test.ts +2 -110
  19. package/src/__tests__/run-due-schedules.test.ts +0 -4
  20. package/src/__tests__/schedule-store.test.ts +0 -12
  21. package/src/__tests__/stt-stream-session.test.ts +4 -4
  22. package/src/__tests__/telephony-tts-guard.test.ts +2 -50
  23. package/src/__tests__/voice-config-update.test.ts +12 -167
  24. package/src/__tests__/web-search-catalog-parity.test.ts +0 -2
  25. package/src/__tests__/workspace-migration-130-speech-mode-to-provider.test.ts +9 -7
  26. package/src/__tests__/workspace-migration-132-web-search-mode-to-provider.test.ts +6 -6
  27. package/src/calls/__tests__/voice-session-bridge.test.ts +1 -4
  28. package/src/calls/resolve-call-tts-provider.ts +6 -25
  29. package/src/calls/telephony-credential-preflight.ts +0 -10
  30. package/src/calls/telephony-tts-capability.ts +1 -17
  31. package/src/cli/commands/tts.ts +1 -2
  32. package/src/cli/index.help.ts +0 -2
  33. package/src/cli/program.ts +0 -2
  34. package/src/config/bundled-skills/settings/tools/voice-config-update.ts +4 -42
  35. package/src/config/schemas/__tests__/stt.test.ts +10 -14
  36. package/src/config/schemas/llm.ts +3 -5
  37. package/src/config/schemas/services.ts +5 -6
  38. package/src/config/schemas/stt.ts +1 -3
  39. package/src/config/schemas/tts.ts +1 -30
  40. package/src/daemon/daemon-readiness.ts +4 -29
  41. package/src/daemon/lifecycle.ts +0 -31
  42. package/src/daemon/providers-setup.ts +0 -8
  43. package/src/heartbeat/__tests__/heartbeat-service.test.ts +0 -1
  44. package/src/heartbeat/heartbeat-service.ts +1 -66
  45. package/src/ipc/__tests__/db-readiness-ipc-gate.test.ts +1 -9
  46. package/src/live-voice/__tests__/live-voice-credential-preflight.test.ts +0 -44
  47. package/src/live-voice/__tests__/live-voice-flux-turn-end.test.ts +4 -6
  48. package/src/live-voice/__tests__/live-voice-stt.test.ts +1 -1
  49. package/src/live-voice/live-voice-credential-preflight.ts +9 -33
  50. package/src/live-voice/live-voice-session.ts +10 -6
  51. package/src/live-voice/live-voice-tts.ts +1 -8
  52. package/src/plugins/defaults/memory/worker.ts +3 -3
  53. package/src/prompts/persona-resolver.ts +0 -35
  54. package/src/providers/__tests__/search-provider-catalog.test.ts +5 -8
  55. package/src/providers/__tests__/voice-error-copy.test.ts +9 -10
  56. package/src/providers/search-provider-catalog.ts +7 -39
  57. package/src/providers/speech-to-text/__tests__/provider-catalog.test.ts +3 -47
  58. package/src/providers/speech-to-text/__tests__/resolve.test.ts +6 -372
  59. package/src/providers/speech-to-text/provider-catalog.ts +2 -73
  60. package/src/providers/speech-to-text/resolve.ts +3 -120
  61. package/src/runtime/http-router-types.ts +1 -3
  62. package/src/runtime/http-server.ts +1 -1
  63. package/src/runtime/routes/__tests__/conversation-query-routes.test.ts +13 -11
  64. package/src/runtime/routes/__tests__/tts-routes.test.ts +2 -6
  65. package/src/runtime/routes/conversation-query-routes.ts +0 -96
  66. package/src/runtime/routes/inbound-stages/transcribe-audio.test.ts +7 -7
  67. package/src/runtime/routes/index.ts +0 -4
  68. package/src/runtime/routes/live-voice-routes.test.ts +4 -11
  69. package/src/runtime/routes/live-voice-routes.ts +8 -11
  70. package/src/runtime/routes/secret-routes.ts +0 -6
  71. package/src/runtime/routes/tts-routes.ts +0 -38
  72. package/src/schedule/__tests__/plugin-schedule-reconciler.test.ts +1 -7
  73. package/src/schedule/__tests__/worker-mcp-readiness.test.ts +0 -4
  74. package/src/schedule/schedule-store.ts +0 -7
  75. package/src/schedule/scheduler.ts +0 -5
  76. package/src/schedule/worker.ts +1 -1
  77. package/src/stt/__tests__/daemon-batch-transcriber.test.ts +2 -10
  78. package/src/stt/daemon-batch-transcriber.ts +4 -38
  79. package/src/stt/types.ts +1 -9
  80. package/src/tools/network/__tests__/web-search.test.ts +3 -250
  81. package/src/tools/network/web-search.ts +4 -165
  82. package/src/tts/__tests__/provider-resolution.test.ts +0 -5
  83. package/src/tts/provider-catalog.ts +0 -4
  84. package/src/tts/types.ts +0 -1
  85. package/src/__tests__/cloud-speech-defaults.test.ts +0 -330
  86. package/src/__tests__/migration-cross-version-compatibility.test.ts +0 -1708
  87. package/src/__tests__/migration-export-http.test.ts +0 -557
  88. package/src/__tests__/migration-export-streaming.test.ts +0 -350
  89. package/src/__tests__/migration-export-to-gcs.test.ts +0 -706
  90. package/src/__tests__/migration-import-commit-http.test.ts +0 -1176
  91. package/src/__tests__/migration-import-from-gcs.test.ts +0 -534
  92. package/src/__tests__/migration-import-from-path.test.ts +0 -324
  93. package/src/__tests__/migration-import-from-url.test.ts +0 -588
  94. package/src/__tests__/migration-import-preflight-http.test.ts +0 -739
  95. package/src/__tests__/migration-jobs-status.test.ts +0 -139
  96. package/src/__tests__/migration-parity-persistence.test.ts +0 -1891
  97. package/src/__tests__/migration-transport.test.ts +0 -1089
  98. package/src/__tests__/migration-validate-http.test.ts +0 -634
  99. package/src/__tests__/migration-wizard.test.ts +0 -1395
  100. package/src/__tests__/rebind-secrets-screen.test.ts +0 -876
  101. package/src/__tests__/sanitize-config-for-transfer.test.ts +0 -259
  102. package/src/__tests__/transfer-progress-screen.test.ts +0 -1217
  103. package/src/__tests__/validation-results-screen.test.ts +0 -1130
  104. package/src/__tests__/vbundle-pax-and-symlink.test.ts +0 -147
  105. package/src/background-wake/background-wake-routes.test.ts +0 -906
  106. package/src/background-wake/cloud-client.test.ts +0 -328
  107. package/src/background-wake/cloud-client.ts +0 -196
  108. package/src/background-wake/next-wake.test.ts +0 -335
  109. package/src/background-wake/next-wake.ts +0 -188
  110. package/src/background-wake/publisher.ts +0 -93
  111. package/src/background-wake/wake-intent-hooks.test.ts +0 -234
  112. package/src/cli/commands/__tests__/backup.test.ts +0 -363
  113. package/src/cli/commands/backup.help.ts +0 -180
  114. package/src/cli/commands/backup.ts +0 -398
  115. package/src/cloud/cloud-speech.test.ts +0 -335
  116. package/src/cloud/cloud-speech.ts +0 -300
  117. package/src/config/cloud-speech-defaults.ts +0 -300
  118. package/src/config/sanitize-for-transfer.ts +0 -111
  119. package/src/live-voice/__tests__/live-voice-cloud-speech-fallback.test.ts +0 -250
  120. package/src/providers/speech-to-text/__tests__/cadet-cloud-realtime.test.ts +0 -457
  121. package/src/providers/speech-to-text/__tests__/cadet-cloud.test.ts +0 -271
  122. package/src/providers/speech-to-text/__tests__/cadet-speech-relay-connection.test.ts +0 -126
  123. package/src/providers/speech-to-text/cadet-cloud-flux-realtime.ts +0 -309
  124. package/src/providers/speech-to-text/cadet-cloud-realtime.ts +0 -315
  125. package/src/providers/speech-to-text/cadet-cloud.ts +0 -108
  126. package/src/providers/speech-to-text/cadet-speech-relay-connection.ts +0 -176
  127. package/src/runtime/migrations/__tests__/gcs-signed-url.test.ts +0 -254
  128. package/src/runtime/migrations/__tests__/job-registry.test.ts +0 -346
  129. package/src/runtime/migrations/__tests__/origin-mode.test.ts +0 -62
  130. package/src/runtime/migrations/__tests__/rebind-secrets-credentials.test.ts +0 -184
  131. package/src/runtime/migrations/__tests__/staged-import-path.test.ts +0 -110
  132. package/src/runtime/migrations/__tests__/v1-test-helpers.ts +0 -112
  133. package/src/runtime/migrations/__tests__/vbundle-builder-credentials.test.ts +0 -285
  134. package/src/runtime/migrations/__tests__/vbundle-builder-fd-leak.test.ts +0 -126
  135. package/src/runtime/migrations/__tests__/vbundle-builder-v1-shape.test.ts +0 -253
  136. package/src/runtime/migrations/__tests__/vbundle-import-credentials.test.ts +0 -208
  137. package/src/runtime/migrations/__tests__/vbundle-import-parity.test.ts +0 -485
  138. package/src/runtime/migrations/__tests__/vbundle-import-policy.test.ts +0 -349
  139. package/src/runtime/migrations/__tests__/vbundle-import-version-compat.test.ts +0 -196
  140. package/src/runtime/migrations/__tests__/vbundle-legacy-user-md.test.ts +0 -551
  141. package/src/runtime/migrations/__tests__/vbundle-metadata-merge-integration.test.ts +0 -428
  142. package/src/runtime/migrations/__tests__/vbundle-metadata-merge.test.ts +0 -220
  143. package/src/runtime/migrations/__tests__/vbundle-streaming-importer.test.ts +0 -1823
  144. package/src/runtime/migrations/__tests__/vbundle-streaming-validator.test.ts +0 -598
  145. package/src/runtime/migrations/__tests__/vbundle-symlink-importer.test.ts +0 -463
  146. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming-importer.test.ts +0 -538
  147. package/src/runtime/migrations/__tests__/vbundle-symlink-streaming.test.ts +0 -515
  148. package/src/runtime/migrations/__tests__/vbundle-symlink-tar.test.ts +0 -453
  149. package/src/runtime/migrations/__tests__/vbundle-symlink-walker.test.ts +0 -319
  150. package/src/runtime/migrations/__tests__/vbundle-tar-stream.test.ts +0 -240
  151. package/src/runtime/migrations/__tests__/vbundle-validator-v1-schema.test.ts +0 -473
  152. package/src/runtime/migrations/gcs-signed-url.ts +0 -190
  153. package/src/runtime/migrations/job-registry.ts +0 -297
  154. package/src/runtime/migrations/migration-transport.ts +0 -612
  155. package/src/runtime/migrations/migration-wizard.ts +0 -747
  156. package/src/runtime/migrations/origin-mode.ts +0 -42
  157. package/src/runtime/migrations/rebind-secrets-screen.ts +0 -412
  158. package/src/runtime/migrations/staged-import-path.ts +0 -116
  159. package/src/runtime/migrations/transfer-progress-screen.ts +0 -323
  160. package/src/runtime/migrations/validation-results-screen.ts +0 -469
  161. package/src/runtime/migrations/vbundle-builder.ts +0 -1382
  162. package/src/runtime/migrations/vbundle-import-analyzer.ts +0 -405
  163. package/src/runtime/migrations/vbundle-import-policy.ts +0 -241
  164. package/src/runtime/migrations/vbundle-importer.ts +0 -826
  165. package/src/runtime/migrations/vbundle-metadata-merge.ts +0 -113
  166. package/src/runtime/migrations/vbundle-streaming-importer.ts +0 -2844
  167. package/src/runtime/migrations/vbundle-streaming-validator.ts +0 -426
  168. package/src/runtime/migrations/vbundle-tar-stream.ts +0 -244
  169. package/src/runtime/migrations/vbundle-validator.ts +0 -724
  170. package/src/runtime/routes/__tests__/migration-cadet-metadata-reconcile.test.ts +0 -227
  171. package/src/runtime/routes/__tests__/migration-export-secrets-redacted.test.ts +0 -54
  172. package/src/runtime/routes/__tests__/migration-import-allowed-hosts-env.test.ts +0 -70
  173. package/src/runtime/routes/__tests__/migration-import-credential-filter.test.ts +0 -257
  174. package/src/runtime/routes/background-wake-routes.ts +0 -387
  175. package/src/runtime/routes/migration-routes.ts +0 -2584
  176. package/src/tools/network/__tests__/cloud-search-proxy.test.ts +0 -282
  177. package/src/tools/network/cloud-search-proxy.ts +0 -211
  178. package/src/tts/__tests__/cadet-provider.test.ts +0 -253
  179. package/src/tts/providers/cadet-provider.ts +0 -244
  180. package/src/types/tar-stream.d.ts +0 -66
@@ -1,282 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
2
-
3
- interface MockCloudClient {
4
- cloudAssistantId: string;
5
- fetch: ReturnType<typeof mock>;
6
- }
7
-
8
- let mockClient: MockCloudClient | null = null;
9
-
10
- mock.module("../../../cloud/client.js", () => ({
11
- CadetCloudClient: {
12
- create: async () => mockClient,
13
- },
14
- }));
15
-
16
- import { callManagedSearchProxy } from "../cloud-search-proxy.js";
17
-
18
- describe("callManagedSearchProxy", () => {
19
- beforeEach(() => {
20
- mockClient = {
21
- cloudAssistantId: "asst-123",
22
- fetch: mock(async () => {
23
- return new Response(
24
- JSON.stringify({
25
- status: 200,
26
- headers: { "content-type": "application/json" },
27
- body: { web: { results: [] } },
28
- }),
29
- { status: 200, headers: { "content-type": "application/json" } },
30
- );
31
- }),
32
- };
33
- });
34
-
35
- afterEach(() => {
36
- mockClient = null;
37
- });
38
-
39
- test("posts to the cloud search proxy endpoint for the assistant and provider", async () => {
40
- await callManagedSearchProxy("brave", {
41
- method: "GET",
42
- path: "/res/v1/web/search",
43
- });
44
-
45
- expect(mockClient!.fetch).toHaveBeenCalledTimes(1);
46
- expect(mockClient!.fetch.mock.calls[0][0]).toBe(
47
- "/v1/assistants/asst-123/cloud-search-proxy/brave/",
48
- );
49
- });
50
-
51
- test("encodes URL path segments", async () => {
52
- mockClient!.cloudAssistantId = "asst/123";
53
-
54
- await callManagedSearchProxy("brave", {
55
- method: "GET",
56
- path: "/res/v1/web/search",
57
- });
58
-
59
- expect(mockClient!.fetch.mock.calls[0][0]).toBe(
60
- "/v1/assistants/asst%2F123/cloud-search-proxy/brave/",
61
- );
62
- });
63
-
64
- test("sends the cloud contract request envelope", async () => {
65
- await callManagedSearchProxy("brave", {
66
- method: "GET",
67
- path: "/res/v1/web/search",
68
- query: {
69
- q: "kimi k2 fireworks web search",
70
- count: "10",
71
- offset: "0",
72
- freshness: "pw",
73
- },
74
- headers: {
75
- Accept: "application/json",
76
- },
77
- body: null,
78
- });
79
-
80
- const init = mockClient!.fetch.mock.calls[0][1] as RequestInit;
81
- expect(init.method).toBe("POST");
82
- expect(new Headers(init.headers).get("Content-Type")).toBe(
83
- "application/json",
84
- );
85
- expect(JSON.parse(init.body as string)).toEqual({
86
- request: {
87
- method: "GET",
88
- path: "/res/v1/web/search",
89
- query: {
90
- q: "kimi k2 fireworks web search",
91
- count: "10",
92
- offset: "0",
93
- freshness: "pw",
94
- },
95
- headers: {
96
- Accept: "application/json",
97
- },
98
- body: null,
99
- },
100
- });
101
- });
102
-
103
- test("fills absent query, headers, and body with contract defaults", async () => {
104
- await callManagedSearchProxy("brave", {
105
- method: "GET",
106
- path: "/res/v1/web/search",
107
- });
108
-
109
- const init = mockClient!.fetch.mock.calls[0][1] as RequestInit;
110
- expect(JSON.parse(init.body as string)).toEqual({
111
- request: {
112
- method: "GET",
113
- path: "/res/v1/web/search",
114
- query: {},
115
- headers: {},
116
- body: null,
117
- },
118
- });
119
- });
120
-
121
- test("passes the abort signal through to platform fetch", async () => {
122
- const controller = new AbortController();
123
-
124
- await callManagedSearchProxy(
125
- "brave",
126
- {
127
- method: "GET",
128
- path: "/res/v1/web/search",
129
- },
130
- controller.signal,
131
- );
132
-
133
- const init = mockClient!.fetch.mock.calls[0][1] as RequestInit;
134
- expect(init.signal).toBe(controller.signal);
135
- });
136
-
137
- test("parses the response envelope and preserves provider JSON body", async () => {
138
- const providerBody = {
139
- web: {
140
- results: [{ title: "Result", url: "https://example.com" }],
141
- },
142
- };
143
- mockClient!.fetch = mock(async () => {
144
- return new Response(
145
- JSON.stringify({
146
- status: 200,
147
- headers: {
148
- "content-type": "application/json",
149
- "x-request-id": "req-123",
150
- },
151
- body: providerBody,
152
- }),
153
- { status: 200, headers: { "content-type": "application/json" } },
154
- );
155
- });
156
-
157
- const result = await callManagedSearchProxy("brave", {
158
- method: "GET",
159
- path: "/res/v1/web/search",
160
- });
161
-
162
- expect(result).toEqual({
163
- ok: true,
164
- status: 200,
165
- headers: {
166
- "content-type": "application/json",
167
- "x-request-id": "req-123",
168
- },
169
- body: providerBody,
170
- });
171
- });
172
-
173
- test("returns a typed platform error for non-2xx platform responses", async () => {
174
- mockClient!.fetch = mock(async () => {
175
- return new Response(
176
- JSON.stringify({ detail: "unsupported cloud search provider" }),
177
- {
178
- status: 400,
179
- headers: {
180
- "content-type": "application/json",
181
- "x-platform-request-id": "platform-req-123",
182
- },
183
- },
184
- );
185
- });
186
-
187
- const result = await callManagedSearchProxy("brave", {
188
- method: "GET",
189
- path: "/res/v1/web/search",
190
- });
191
-
192
- expect(result).toEqual({
193
- ok: false,
194
- kind: "platform-error",
195
- status: 400,
196
- headers: {
197
- "content-type": "application/json",
198
- "x-platform-request-id": "platform-req-123",
199
- },
200
- body: { detail: "unsupported cloud search provider" },
201
- message:
202
- "Cloud search proxy returned status 400: unsupported cloud search provider",
203
- });
204
- });
205
-
206
- test("preserves insufficient-balance platform errors", async () => {
207
- mockClient!.fetch = mock(async () => {
208
- return new Response(JSON.stringify({ detail: "Insufficient balance" }), {
209
- status: 402,
210
- headers: { "content-type": "application/json" },
211
- });
212
- });
213
-
214
- const result = await callManagedSearchProxy("brave", {
215
- method: "GET",
216
- path: "/res/v1/web/search",
217
- });
218
-
219
- expect(result).toEqual({
220
- ok: false,
221
- kind: "platform-error",
222
- status: 402,
223
- headers: {
224
- "content-type": "application/json",
225
- },
226
- body: { detail: "Insufficient balance" },
227
- message: "Cloud search proxy returned status 402: Insufficient balance",
228
- });
229
- });
230
-
231
- test("returns unavailable when platform context is missing", async () => {
232
- mockClient = null;
233
-
234
- const result = await callManagedSearchProxy("brave", {
235
- method: "GET",
236
- path: "/res/v1/web/search",
237
- });
238
-
239
- expect(result).toEqual({
240
- ok: false,
241
- kind: "unavailable",
242
- message: "Cloud search proxy is unavailable in this environment.",
243
- });
244
- });
245
-
246
- test("returns unavailable when platform assistant ID is missing", async () => {
247
- mockClient!.cloudAssistantId = "";
248
-
249
- const result = await callManagedSearchProxy("brave", {
250
- method: "GET",
251
- path: "/res/v1/web/search",
252
- });
253
-
254
- expect(result).toEqual({
255
- ok: false,
256
- kind: "unavailable",
257
- message:
258
- "Cloud search proxy is unavailable: platform assistant ID is missing.",
259
- });
260
- });
261
-
262
- test("returns an invalid-response result for malformed platform envelopes", async () => {
263
- mockClient!.fetch = mock(async () => {
264
- return new Response(JSON.stringify({ ok: true }), {
265
- status: 200,
266
- headers: { "content-type": "application/json" },
267
- });
268
- });
269
-
270
- const result = await callManagedSearchProxy("brave", {
271
- method: "GET",
272
- path: "/res/v1/web/search",
273
- });
274
-
275
- expect(result).toEqual({
276
- ok: false,
277
- kind: "invalid-response",
278
- body: { ok: true },
279
- message: "Cloud search proxy returned an invalid response envelope.",
280
- });
281
- });
282
- });
@@ -1,211 +0,0 @@
1
- import { CadetCloudClient } from "../../cloud/client.js";
2
-
3
- export type ManagedSearchProxyProvider = "brave";
4
-
5
- export interface ManagedSearchProxyRequest {
6
- method: string;
7
- path: string;
8
- query?: Record<string, string>;
9
- headers?: Record<string, string>;
10
- body?: unknown;
11
- }
12
-
13
- export interface ManagedSearchProxyResponse {
14
- status: number;
15
- headers: Record<string, string>;
16
- body: unknown;
17
- }
18
-
19
- export type ManagedSearchProxyResult =
20
- | ({ ok: true } & ManagedSearchProxyResponse)
21
- | {
22
- ok: false;
23
- kind: "unavailable";
24
- message: string;
25
- }
26
- | {
27
- ok: false;
28
- kind: "platform-error";
29
- status: number;
30
- headers: Record<string, string>;
31
- body: unknown;
32
- message: string;
33
- }
34
- | {
35
- ok: false;
36
- kind: "invalid-response";
37
- body: unknown;
38
- message: string;
39
- };
40
-
41
- interface ManagedSearchProxyEnvelope {
42
- request: {
43
- method: string;
44
- path: string;
45
- query: Record<string, string>;
46
- headers: Record<string, string>;
47
- body: unknown;
48
- };
49
- }
50
-
51
- /**
52
- * Whether the cloud search proxy can be dialed at all: a cloud client
53
- * exists and carries an assistant id. Mirrors `managedSpeechAvailable()`.
54
- * Callers use this to decide between the proxy and the BYOK chain *before*
55
- * attempting a call — the same two conditions `callManagedSearchProxy`
56
- * reports as `kind: "unavailable"`.
57
- */
58
- export async function managedSearchAvailable(): Promise<boolean> {
59
- const client = await CadetCloudClient.create();
60
- return client !== null && Boolean(client.cloudAssistantId);
61
- }
62
-
63
- export async function callManagedSearchProxy(
64
- provider: ManagedSearchProxyProvider,
65
- request: ManagedSearchProxyRequest,
66
- signal?: AbortSignal,
67
- ): Promise<ManagedSearchProxyResult> {
68
- const client = await CadetCloudClient.create();
69
- if (!client) {
70
- return {
71
- ok: false,
72
- kind: "unavailable",
73
- message: "Cloud search proxy is unavailable in this environment.",
74
- };
75
- }
76
-
77
- if (!client.cloudAssistantId) {
78
- return {
79
- ok: false,
80
- kind: "unavailable",
81
- message:
82
- "Cloud search proxy is unavailable: platform assistant ID is missing.",
83
- };
84
- }
85
-
86
- const path = `/v1/assistants/${encodeURIComponent(
87
- client.cloudAssistantId,
88
- )}/cloud-search-proxy/${encodeURIComponent(provider)}/`;
89
-
90
- const envelope: ManagedSearchProxyEnvelope = {
91
- request: {
92
- method: request.method,
93
- path: request.path,
94
- query: request.query ?? {},
95
- headers: request.headers ?? {},
96
- body: request.body ?? null,
97
- },
98
- };
99
-
100
- const response = await client.fetch(path, {
101
- method: "POST",
102
- headers: {
103
- "Content-Type": "application/json",
104
- },
105
- body: JSON.stringify(envelope),
106
- signal,
107
- });
108
-
109
- if (!response.ok) {
110
- const body = await readResponseBody(response);
111
- return {
112
- ok: false,
113
- kind: "platform-error",
114
- status: response.status,
115
- headers: responseHeadersToRecord(response.headers),
116
- body,
117
- message: platformErrorMessage(response.status, body),
118
- };
119
- }
120
-
121
- const body = await readResponseBody(response);
122
- if (!isManagedSearchProxyResponse(body)) {
123
- return {
124
- ok: false,
125
- kind: "invalid-response",
126
- body,
127
- message: "Cloud search proxy returned an invalid response envelope.",
128
- };
129
- }
130
-
131
- return {
132
- ok: true,
133
- status: body.status,
134
- headers: body.headers,
135
- body: body.body,
136
- };
137
- }
138
-
139
- function isManagedSearchProxyResponse(
140
- value: unknown,
141
- ): value is ManagedSearchProxyResponse {
142
- if (!isRecord(value)) {
143
- return false;
144
- }
145
- if (typeof value.status !== "number") {
146
- return false;
147
- }
148
- if (!isStringRecord(value.headers)) {
149
- return false;
150
- }
151
- return "body" in value;
152
- }
153
-
154
- async function readResponseBody(response: Response): Promise<unknown> {
155
- const text = await response.text().catch(() => "");
156
- if (!text) {
157
- return null;
158
- }
159
-
160
- const contentType = response.headers.get("content-type") ?? "";
161
- if (contentType.includes("application/json")) {
162
- try {
163
- return JSON.parse(text);
164
- } catch {
165
- return text;
166
- }
167
- }
168
-
169
- return text;
170
- }
171
-
172
- function responseHeadersToRecord(headers: Headers): Record<string, string> {
173
- return Object.fromEntries(headers.entries());
174
- }
175
-
176
- function platformErrorMessage(status: number, body: unknown): string {
177
- const detail = extractErrorDetail(body);
178
- if (detail) {
179
- return `Cloud search proxy returned status ${status}: ${detail}`;
180
- }
181
- return `Cloud search proxy returned status ${status}.`;
182
- }
183
-
184
- function extractErrorDetail(body: unknown): string | undefined {
185
- if (typeof body === "string") {
186
- return body || undefined;
187
- }
188
- if (!isRecord(body)) {
189
- return undefined;
190
- }
191
-
192
- for (const key of ["detail", "error", "message"]) {
193
- const value = body[key];
194
- if (typeof value === "string" && value) {
195
- return value;
196
- }
197
- }
198
-
199
- return undefined;
200
- }
201
-
202
- function isRecord(value: unknown): value is Record<string, unknown> {
203
- return typeof value === "object" && value !== null && !Array.isArray(value);
204
- }
205
-
206
- function isStringRecord(value: unknown): value is Record<string, string> {
207
- if (!isRecord(value)) {
208
- return false;
209
- }
210
- return Object.values(value).every((entry) => typeof entry === "string");
211
- }