@qvac/sdk 0.3.0 → 0.4.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 (207) hide show
  1. package/README.md +22 -0
  2. package/dist/client/api/cancel.d.ts +1 -1
  3. package/dist/client/api/cancel.js +4 -3
  4. package/dist/client/api/completion-stream.d.ts +24 -32
  5. package/dist/client/api/completion-stream.js +142 -50
  6. package/dist/client/api/delete-cache.js +3 -2
  7. package/dist/client/api/download-asset.d.ts +3 -3
  8. package/dist/client/api/download-asset.js +12 -17
  9. package/dist/client/api/embed.d.ts +1 -1
  10. package/dist/client/api/embed.js +4 -3
  11. package/dist/client/api/get-model-info.d.ts +29 -0
  12. package/dist/client/api/get-model-info.js +14 -0
  13. package/dist/client/api/index.d.ts +3 -0
  14. package/dist/client/api/index.js +3 -0
  15. package/dist/client/api/load-model.d.ts +3 -3
  16. package/dist/client/api/load-model.js +13 -22
  17. package/dist/client/api/logging-stream.d.ts +1 -1
  18. package/dist/client/api/logging-stream.js +3 -2
  19. package/dist/client/api/ping.d.ts +1 -1
  20. package/dist/client/api/ping.js +3 -2
  21. package/dist/client/api/provide.d.ts +1 -1
  22. package/dist/client/api/provide.js +4 -3
  23. package/dist/client/api/rag.d.ts +3 -3
  24. package/dist/client/api/rag.js +13 -12
  25. package/dist/client/api/set-config.d.ts +7 -0
  26. package/dist/client/api/set-config.js +20 -0
  27. package/dist/client/api/stop-provider.d.ts +1 -1
  28. package/dist/client/api/stop-provider.js +4 -3
  29. package/dist/client/api/text-to-speech.d.ts +7 -0
  30. package/dist/client/api/text-to-speech.js +58 -0
  31. package/dist/client/api/transcribe.d.ts +4 -2
  32. package/dist/client/api/transcribe.js +9 -5
  33. package/dist/client/api/translate.d.ts +1 -1
  34. package/dist/client/api/translate.js +3 -2
  35. package/dist/client/api/unload-model.d.ts +5 -1
  36. package/dist/client/api/unload-model.js +14 -4
  37. package/dist/client/config-manager.d.ts +4 -0
  38. package/dist/client/config-manager.js +8 -0
  39. package/dist/client/index.d.ts +1 -1
  40. package/dist/client/index.js +1 -1
  41. package/dist/client/init-hooks.d.ts +6 -0
  42. package/dist/client/init-hooks.js +37 -0
  43. package/dist/client/rpc/bare-client.d.ts +69 -0
  44. package/dist/client/rpc/bare-client.js +5 -4
  45. package/dist/client/rpc/bare-rpc-client.js +7 -3
  46. package/dist/client/rpc/expo-rpc-client.d.ts +1 -0
  47. package/dist/client/rpc/expo-rpc-client.js +9 -1
  48. package/dist/client/rpc/node-rpc-client.d.ts +1 -1
  49. package/dist/client/rpc/node-rpc-client.js +22 -9
  50. package/dist/client/rpc/rpc-client.d.ts +1 -0
  51. package/dist/client/rpc/rpc-client.js +20 -1
  52. package/dist/examples/cache-management.d.ts +2 -0
  53. package/dist/examples/cache-management.js +97 -0
  54. package/dist/examples/delegated-inference/consumer.js +2 -2
  55. package/dist/examples/download-with-blind-relays.d.ts +2 -0
  56. package/dist/examples/download-with-blind-relays.js +48 -0
  57. package/dist/examples/download-with-cancel.js +3 -4
  58. package/dist/examples/embed-hyperdrive.js +0 -1
  59. package/dist/examples/kv-cache-cleanup.js +0 -1
  60. package/dist/examples/kv-cache-custom-key.js +0 -1
  61. package/dist/examples/kv-cache-example.js +0 -1
  62. package/dist/examples/llamacpp-file-logging.js +0 -1
  63. package/dist/examples/llamacpp-filesystem.js +2 -2
  64. package/dist/examples/llamacpp-http.js +0 -1
  65. package/dist/examples/llamacpp-hyperdrive.js +0 -1
  66. package/dist/examples/llamacpp-multimodal.js +0 -1
  67. package/dist/examples/llamacpp-native-tools.js +118 -36
  68. package/dist/examples/llamacpp-sharded.d.ts +2 -0
  69. package/dist/examples/llamacpp-sharded.js +44 -0
  70. package/dist/examples/multi-model-demo.js +0 -1
  71. package/dist/examples/parallel-download.js +2 -2
  72. package/dist/examples/rag/rag-chromadb.js +0 -1
  73. package/dist/examples/rag/rag-hyperdb-workspaces.js +0 -1
  74. package/dist/examples/rag/rag-hyperdb.js +0 -1
  75. package/dist/examples/rag/rag-lancedb.js +0 -1
  76. package/dist/examples/rag/rag-sqlite.js +0 -1
  77. package/dist/examples/text-to-speech.d.ts +3 -0
  78. package/dist/examples/text-to-speech.js +137 -0
  79. package/dist/examples/translation/translation-indic.js +3 -1
  80. package/dist/examples/translation/translation-llm-context.js +0 -1
  81. package/dist/examples/translation/translation-llm.js +2 -2
  82. package/dist/examples/translation/translation-opus.js +3 -1
  83. package/dist/examples/translation/translation-stream.js +0 -1
  84. package/dist/examples/whispercpp-filesystem.js +32 -5
  85. package/dist/examples/whispercpp-microphone-record.js +9 -9
  86. package/dist/examples/whispercpp-prompt.d.ts +2 -0
  87. package/dist/examples/whispercpp-prompt.js +68 -0
  88. package/dist/expo/plugins/withMobileBundle.js +9 -3
  89. package/dist/index.d.ts +4 -2
  90. package/dist/index.js +4 -2
  91. package/dist/models/hyperdrive/models.d.ts +1916 -100
  92. package/dist/models/hyperdrive/models.js +2042 -102
  93. package/dist/schemas/common.d.ts +413 -58
  94. package/dist/schemas/common.js +9 -0
  95. package/dist/schemas/completion-stream.d.ts +26 -22
  96. package/dist/schemas/completion-stream.js +9 -23
  97. package/dist/schemas/download-asset.d.ts +77 -2
  98. package/dist/schemas/download-asset.js +15 -4
  99. package/dist/schemas/get-model-info.d.ts +100 -0
  100. package/dist/schemas/get-model-info.js +40 -0
  101. package/dist/schemas/index.d.ts +7 -0
  102. package/dist/schemas/index.js +7 -0
  103. package/dist/schemas/llamacpp-config.d.ts +2 -1
  104. package/dist/schemas/llamacpp-config.js +3 -4
  105. package/dist/schemas/load-model.d.ts +2316 -266
  106. package/dist/schemas/load-model.js +157 -10
  107. package/dist/schemas/model-src-utils.d.ts +98 -0
  108. package/dist/schemas/model-src-utils.js +31 -0
  109. package/dist/schemas/sdk-errors-client.d.ts +25 -0
  110. package/dist/schemas/sdk-errors-client.js +111 -0
  111. package/dist/schemas/sdk-errors-server.d.ts +55 -0
  112. package/dist/schemas/sdk-errors-server.js +271 -0
  113. package/dist/schemas/set-config.d.ts +21 -0
  114. package/dist/schemas/set-config.js +14 -0
  115. package/dist/schemas/text-to-speech.d.ts +34 -0
  116. package/dist/schemas/text-to-speech.js +25 -0
  117. package/dist/schemas/tools.d.ts +62 -0
  118. package/dist/schemas/tools.js +43 -0
  119. package/dist/schemas/transcription.d.ts +25 -2
  120. package/dist/schemas/transcription.js +12 -1
  121. package/dist/schemas/translation-config.d.ts +33 -5
  122. package/dist/schemas/translation-config.js +18 -1
  123. package/dist/schemas/unload-model.d.ts +2 -0
  124. package/dist/schemas/unload-model.js +2 -0
  125. package/dist/schemas/whispercpp-config.d.ts +55 -18
  126. package/dist/schemas/whispercpp-config.js +63 -15
  127. package/dist/server/bare/addons/cancel.js +2 -1
  128. package/dist/server/bare/addons/llamacpp/create-model.d.ts +9 -2
  129. package/dist/server/bare/addons/llamacpp/create-model.js +23 -23
  130. package/dist/server/bare/addons/llamacpp/handlers/completion-stream.d.ts +10 -2
  131. package/dist/server/bare/addons/llamacpp/handlers/completion-stream.js +62 -43
  132. package/dist/server/bare/addons/llamacpp/handlers/embed.js +3 -2
  133. package/dist/server/bare/addons/llamacpp/kv-cache-utils.d.ts +1 -1
  134. package/dist/server/bare/addons/llamacpp/kv-cache-utils.js +6 -2
  135. package/dist/server/bare/addons/load-model.js +59 -28
  136. package/dist/server/bare/addons/translation/create-model.d.ts +5 -1
  137. package/dist/server/bare/addons/translation/create-model.js +17 -25
  138. package/dist/server/bare/addons/translation/handlers/translate.js +10 -1
  139. package/dist/server/bare/addons/tts/create-model.d.ts +10 -0
  140. package/dist/server/bare/addons/tts/create-model.js +25 -0
  141. package/dist/server/bare/addons/tts/handlers/text-to-speech.d.ts +3 -0
  142. package/dist/server/bare/addons/tts/handlers/text-to-speech.js +87 -0
  143. package/dist/server/bare/addons/tts/index.d.ts +3 -0
  144. package/dist/server/bare/addons/tts/index.js +2 -0
  145. package/dist/server/bare/addons/unload-model.js +21 -3
  146. package/dist/server/bare/addons/whispercpp/create-model.d.ts +5 -1
  147. package/dist/server/bare/addons/whispercpp/create-model.js +13 -32
  148. package/dist/server/bare/addons/whispercpp/handlers/transcribe-stream.js +41 -20
  149. package/dist/server/bare/delegate-rpc-client.js +3 -2
  150. package/dist/server/bare/hyperswarm.d.ts +3 -0
  151. package/dist/server/bare/hyperswarm.js +22 -1
  152. package/dist/server/bare/rag-hyperdb/rag-manager.js +2 -3
  153. package/dist/server/bare/registry/config-registry.d.ts +5 -0
  154. package/dist/server/bare/registry/config-registry.js +39 -0
  155. package/dist/server/bare/registry/model-registry.d.ts +13 -0
  156. package/dist/server/bare/registry/model-registry.js +24 -7
  157. package/dist/server/rpc/handle-request.js +42 -3
  158. package/dist/server/rpc/handlers/completion-stream-delegated.js +2 -1
  159. package/dist/server/rpc/handlers/completion-stream.js +11 -4
  160. package/dist/server/rpc/handlers/download-asset.js +2 -3
  161. package/dist/server/rpc/handlers/get-model-info.d.ts +3 -0
  162. package/dist/server/rpc/handlers/get-model-info.js +148 -0
  163. package/dist/server/rpc/handlers/index.d.ts +4 -0
  164. package/dist/server/rpc/handlers/index.js +6 -0
  165. package/dist/server/rpc/handlers/load-model/http.js +51 -10
  166. package/dist/server/rpc/handlers/load-model/hyperdrive.js +336 -156
  167. package/dist/server/rpc/handlers/load-model/index.js +1 -1
  168. package/dist/server/rpc/handlers/load-model/resolve.d.ts +1 -1
  169. package/dist/server/rpc/handlers/load-model/resolve.js +10 -6
  170. package/dist/server/rpc/handlers/load-model-delegated.js +7 -4
  171. package/dist/server/rpc/handlers/load-model.js +19 -1
  172. package/dist/server/rpc/handlers/provideHandler/handler.js +2 -1
  173. package/dist/server/rpc/handlers/rag.js +0 -3
  174. package/dist/server/rpc/handlers/set-config.d.ts +3 -0
  175. package/dist/server/rpc/handlers/set-config.js +18 -0
  176. package/dist/server/rpc/handlers/stopProvideHandler.js +3 -1
  177. package/dist/server/rpc/handlers/text-to-speech.d.ts +3 -0
  178. package/dist/server/rpc/handlers/text-to-speech.js +7 -0
  179. package/dist/server/rpc/handlers/transcribe-stream.js +6 -2
  180. package/dist/server/rpc/handlers/unload-model.js +7 -0
  181. package/dist/server/utils/cache.d.ts +13 -8
  182. package/dist/server/utils/cache.js +69 -14
  183. package/dist/server/utils/checksum.d.ts +2 -0
  184. package/dist/server/utils/checksum.js +24 -0
  185. package/dist/server/utils/index.d.ts +2 -0
  186. package/dist/server/utils/index.js +2 -0
  187. package/dist/server/utils/shard-utils.d.ts +22 -0
  188. package/dist/server/utils/shard-utils.js +37 -0
  189. package/dist/server/utils/tool-integration.d.ts +13 -0
  190. package/dist/server/utils/tool-integration.js +43 -0
  191. package/dist/server/utils/tool-parser.d.ts +7 -0
  192. package/dist/server/utils/tool-parser.js +244 -0
  193. package/dist/utils/errors-base.d.ts +3 -0
  194. package/dist/utils/errors-base.js +13 -0
  195. package/dist/utils/errors-client.d.ts +107 -0
  196. package/dist/utils/errors-client.js +186 -0
  197. package/dist/utils/errors-server.d.ts +146 -0
  198. package/dist/utils/errors-server.js +253 -0
  199. package/dist/utils/tool-helpers.d.ts +17 -0
  200. package/dist/utils/tool-helpers.js +99 -0
  201. package/package.json +12 -8
  202. package/dist/examples/llamacpp-tools-routing.d.ts +0 -2
  203. package/dist/examples/llamacpp-tools-routing.js +0 -364
  204. package/dist/examples/llamacpp-tools.d.ts +0 -2
  205. package/dist/examples/llamacpp-tools.js +0 -81
  206. package/dist/server/http-server.d.ts +0 -2
  207. package/dist/server/http-server.js +0 -189
package/README.md CHANGED
@@ -272,6 +272,12 @@ This works for all model types (LLM, Whisper, NMT, Embeddings) and provides valu
272
272
  - Indic language translation: [`examples/translation-indic.ts`](examples/translation-indic.ts)
273
273
  - LLM-based translation: [`examples/translation-llm.ts`](examples/translation-llm.ts)
274
274
 
275
+ ### Text to Speech
276
+
277
+ - Text to Speech: [`examples/text-to-speech.ts`](examples/text-to-speech.ts)
278
+
279
+ **Note**: To run the text-to-speech example, espeak data is required which can be obtained from [here](https://github.com/espeak-ng/espeak-ng).
280
+
275
281
  ### Multimodel
276
282
 
277
283
  - Load multiple models simultaneously: [`examples/multi-model-demo.ts`](examples/multi-model-demo.ts)
@@ -288,6 +294,22 @@ This works for all model types (LLM, Whisper, NMT, Embeddings) and provides valu
288
294
 
289
295
  - Pause and resume download: [`examples/download-with-cancel.ts`](examples/download-with-cancel.ts)
290
296
 
297
+ ### Sharded Models
298
+
299
+ - Load sharded models from Hyperdrive: [`examples/llamacpp-sharded.ts`](examples/llamacpp-sharded.ts)
300
+ - Load models from local filesystem: [`examples/llamacpp-filesystem.ts`](examples/llamacpp-filesystem.ts)
301
+
302
+ **Note**: Sharded models automatically download all parts sequentially with detailed progress tracking per shard. For local sharded models, pass the path to the first shard file (e.g., `model-00001-of-00005.gguf`), and remaining shards will be loaded automatically.
303
+
304
+ ### Blind Relays
305
+
306
+ Blind relays help establish peer connections through NAT/firewalls by routing traffic through relay nodes.
307
+
308
+ - Model downloads via Hyperdrive: [`examples/download-with-blind-relays.ts`](./examples/download-with-blind-relays.ts)
309
+ - Delegated inference: You can reuse the same pattern for delegated inference by calling `setConfig({ swarmRelays: [...relayPublicKeys] })` before starting your provider/consumer.
310
+
311
+ **Note**: The example uses mock relay keys. In real deployments, you **must** use your own relay servers or trusted public relays.
312
+
291
313
  ## Known issues
292
314
 
293
315
  - **One off scripts don't terminate / hang**
@@ -7,7 +7,7 @@ import { type CancelParams } from "../../schemas/index";
7
7
  * @param params.modelId - The model ID (required for inference cancellation)
8
8
  * @param params.downloadKey - The download key (required for download cancellation)
9
9
  * @param params.clearCache - If true, deletes the partial download file (default: false)
10
- * @throws {Error} When the response type is invalid or when the cancellation fails
10
+ * @throws {QvacErrorBase} When the response type is invalid or when the cancellation fails
11
11
  *
12
12
  * @example
13
13
  * // Cancel inference
@@ -1,5 +1,6 @@
1
1
  import { send } from "../rpc/rpc-client.js";
2
2
  import {} from "../../schemas/index.js";
3
+ import { InvalidResponseError, CancelFailedError } from "../../utils/errors-client.js";
3
4
  /**
4
5
  * Cancels an ongoing operation.
5
6
  *
@@ -8,7 +9,7 @@ import {} from "../../schemas/index.js";
8
9
  * @param params.modelId - The model ID (required for inference cancellation)
9
10
  * @param params.downloadKey - The download key (required for download cancellation)
10
11
  * @param params.clearCache - If true, deletes the partial download file (default: false)
11
- * @throws {Error} When the response type is invalid or when the cancellation fails
12
+ * @throws {QvacErrorBase} When the response type is invalid or when the cancellation fails
12
13
  *
13
14
  * @example
14
15
  * // Cancel inference
@@ -29,9 +30,9 @@ export async function cancel(params) {
29
30
  };
30
31
  const response = await send(request);
31
32
  if (response.type !== "cancel") {
32
- throw new Error("Invalid response type");
33
+ throw new InvalidResponseError("cancel");
33
34
  }
34
35
  if (!response.success) {
35
- throw new Error(response.error ?? "Failed to cancel");
36
+ throw new CancelFailedError(response.error);
36
37
  }
37
38
  }
@@ -1,4 +1,8 @@
1
- import { type CompletionStats, type CompletionClientParams } from "../../schemas/index";
1
+ import { type CompletionStats, type CompletionClientParams, type ToolCall, type ToolCallEvent, type Tool } from "../../schemas/index";
2
+ import { type ToolInput } from "../../utils/tool-helpers";
3
+ type CompletionParams = Omit<CompletionClientParams, "tools"> & {
4
+ tools?: Tool[] | ToolInput[];
5
+ };
2
6
  /**
3
7
  * Generates completion from a language model based on conversation history.
4
8
  *
@@ -6,7 +10,7 @@ import { type CompletionStats, type CompletionClientParams } from "../../schemas
6
10
  * @param params.modelId - The identifier of the model to use for completion
7
11
  * @param params.history - Array of conversation messages with role, content, and optional attachments
8
12
  * @param params.stream - Whether to stream tokens (true) or return complete response (false). Defaults to true
9
- * @param params.tools - Optional array of tools that can be used by the model
13
+ * @param params.tools - Optional array of tools (can be simple ToolInput with Zod schemas or full Tool objects)
10
14
  * @param params.kvCache - Optional KV cache configuration. Cache files are organized hierarchically:
11
15
  * - Structure: `{kvCacheKey}/{modelId}/{configHash}.bin`
12
16
  * - The configHash includes model config + system prompt to ensure cache isolation
@@ -15,51 +19,39 @@ import { type CompletionStats, type CompletionClientParams } from "../../schemas
15
19
  * - `false` or `undefined`: No caching
16
20
  * - ⚡ Performance: When cache exists, only the last message is sent to the model (includes multimodal attachments)
17
21
  * - 🗑️ Cleanup: Use `deleteCache({ kvCacheKey })` to remove cached sessions
18
- * @returns Object with tokenStream generator, text promise, and stats promise
22
+ * @returns Object with tokenStream generator, toolCallStream generator, text promise, toolCalls promise, and stats promise
19
23
  * @example
20
24
  * ```typescript
21
- * // Streaming mode with auto-generated cache
25
+ * import { z } from "zod";
26
+ *
27
+ * // Simple tool definition with Zod
22
28
  * const result = completion({
23
29
  * modelId: "llama-2",
24
30
  * history: [
25
- * { role: "user", content: "Hello, how are you?" }
31
+ * { role: "user", content: "What's the weather in Tokyo?" }
26
32
  * ],
27
33
  * stream: true,
28
- * kvCache: true, // Auto-generate cache key
34
+ * tools: [{
35
+ * name: "get_weather",
36
+ * description: "Get current weather",
37
+ * parameters: z.object({
38
+ * city: z.string().describe("City name"),
39
+ * country: z.string().describe("Country code").optional(),
40
+ * }),
41
+ * }]
29
42
  * });
30
43
  *
31
44
  * for await (const token of result.tokenStream) {
32
- * console.log(token);
45
+ * process.stdout.write(token);
33
46
  * }
34
- *
35
- * // Custom session management with cache key
36
- * const sessionResult = completion({
37
- * modelId: "llama-2",
38
- * history: [
39
- * { role: "user", content: "Continue our conversation" }
40
- * ],
41
- * stream: true,
42
- * kvCache: "user-session-123", // Custom cache key
43
- * });
44
- *
45
- * // Non-streaming mode
46
- * const response = completion({
47
- * modelId: "llama-2",
48
- * history: [
49
- * { role: "user", content: "What is the capital of France?" }
50
- * ],
51
- * stream: false,
52
- * });
53
- *
54
- * const text = await response.text;
55
- * const stats = await response.stats;
56
- * console.log(text);
57
- * console.log(stats);
58
47
  * ```
59
48
  */
60
- export declare function completion(params: CompletionClientParams): {
49
+ export declare function completion(params: CompletionParams): {
61
50
  tokenStream: AsyncGenerator<string>;
51
+ toolCallStream: AsyncGenerator<ToolCallEvent>;
62
52
  stats: Promise<CompletionStats | undefined>;
63
53
  text: Promise<string>;
54
+ toolCalls: Promise<ToolCall[]>;
64
55
  };
56
+ export {};
65
57
  //# sourceMappingURL=completion-stream.d.ts.map
@@ -1,5 +1,6 @@
1
1
  import { completionStreamResponseSchema, } from "../../schemas/index.js";
2
2
  import { stream as streamRpc } from "../rpc/rpc-client.js";
3
+ import { validateTools } from "../../utils/tool-helpers.js";
3
4
  /**
4
5
  * Generates completion from a language model based on conversation history.
5
6
  *
@@ -7,7 +8,7 @@ import { stream as streamRpc } from "../rpc/rpc-client.js";
7
8
  * @param params.modelId - The identifier of the model to use for completion
8
9
  * @param params.history - Array of conversation messages with role, content, and optional attachments
9
10
  * @param params.stream - Whether to stream tokens (true) or return complete response (false). Defaults to true
10
- * @param params.tools - Optional array of tools that can be used by the model
11
+ * @param params.tools - Optional array of tools (can be simple ToolInput with Zod schemas or full Tool objects)
11
12
  * @param params.kvCache - Optional KV cache configuration. Cache files are organized hierarchically:
12
13
  * - Structure: `{kvCacheKey}/{modelId}/{configHash}.bin`
13
14
  * - The configHash includes model config + system prompt to ensure cache isolation
@@ -16,103 +17,194 @@ import { stream as streamRpc } from "../rpc/rpc-client.js";
16
17
  * - `false` or `undefined`: No caching
17
18
  * - ⚡ Performance: When cache exists, only the last message is sent to the model (includes multimodal attachments)
18
19
  * - 🗑️ Cleanup: Use `deleteCache({ kvCacheKey })` to remove cached sessions
19
- * @returns Object with tokenStream generator, text promise, and stats promise
20
+ * @returns Object with tokenStream generator, toolCallStream generator, text promise, toolCalls promise, and stats promise
20
21
  * @example
21
22
  * ```typescript
22
- * // Streaming mode with auto-generated cache
23
+ * import { z } from "zod";
24
+ *
25
+ * // Simple tool definition with Zod
23
26
  * const result = completion({
24
27
  * modelId: "llama-2",
25
28
  * history: [
26
- * { role: "user", content: "Hello, how are you?" }
29
+ * { role: "user", content: "What's the weather in Tokyo?" }
27
30
  * ],
28
31
  * stream: true,
29
- * kvCache: true, // Auto-generate cache key
32
+ * tools: [{
33
+ * name: "get_weather",
34
+ * description: "Get current weather",
35
+ * parameters: z.object({
36
+ * city: z.string().describe("City name"),
37
+ * country: z.string().describe("Country code").optional(),
38
+ * }),
39
+ * }]
30
40
  * });
31
41
  *
32
42
  * for await (const token of result.tokenStream) {
33
- * console.log(token);
43
+ * process.stdout.write(token);
34
44
  * }
35
- *
36
- * // Custom session management with cache key
37
- * const sessionResult = completion({
38
- * modelId: "llama-2",
39
- * history: [
40
- * { role: "user", content: "Continue our conversation" }
41
- * ],
42
- * stream: true,
43
- * kvCache: "user-session-123", // Custom cache key
44
- * });
45
- *
46
- * // Non-streaming mode
47
- * const response = completion({
48
- * modelId: "llama-2",
49
- * history: [
50
- * { role: "user", content: "What is the capital of France?" }
51
- * ],
52
- * stream: false,
53
- * });
54
- *
55
- * const text = await response.text;
56
- * const stats = await response.stats;
57
- * console.log(text);
58
- * console.log(stats);
59
45
  * ```
60
46
  */
61
47
  export function completion(params) {
48
+ const tools = params.tools ? validateTools(params.tools) : undefined;
62
49
  const request = {
63
50
  type: "completionStream",
64
51
  ...params,
52
+ tools,
65
53
  stream: params.stream ?? true,
66
54
  };
67
55
  let stats;
68
56
  let statsResolver = () => { };
69
- const statsPromise = new Promise((resolve) => {
57
+ let statsRejecter = () => { };
58
+ const statsPromise = new Promise((resolve, reject) => {
70
59
  statsResolver = resolve;
60
+ statsRejecter = reject;
71
61
  });
72
- if (params.stream) {
73
- const tokenStream = (async function* () {
74
- for await (const response of streamRpc(request)) {
75
- if (response.type === "completionStream") {
62
+ statsPromise.catch(() => { });
63
+ let toolCallsArray = [];
64
+ let toolCallsResolver = () => { };
65
+ let toolCallsRejecter = () => { };
66
+ const toolCallsPromise = new Promise((resolve, reject) => {
67
+ toolCallsResolver = resolve;
68
+ toolCallsRejecter = reject;
69
+ });
70
+ toolCallsPromise.catch(() => { });
71
+ const responses = streamRpc(request);
72
+ const tokenQueue = [];
73
+ const toolEventQueue = [];
74
+ let tokenDone = false;
75
+ let toolDone = false;
76
+ let tokenResolve = null;
77
+ let toolResolve = null;
78
+ let textBuffer = "";
79
+ let streamError = null;
80
+ const processResponses = async () => {
81
+ try {
82
+ for await (const response of responses) {
83
+ if (response &&
84
+ typeof response === "object" &&
85
+ "type" in response &&
86
+ response.type === "completionStream") {
76
87
  const streamResponse = completionStreamResponseSchema.parse(response);
77
88
  if (!streamResponse.done) {
78
- yield streamResponse.token;
89
+ const token = streamResponse.token;
90
+ if (token) {
91
+ textBuffer += token;
92
+ tokenQueue.push(token);
93
+ if (tokenResolve) {
94
+ tokenResolve();
95
+ tokenResolve = null;
96
+ }
97
+ }
98
+ if (streamResponse.toolCallEvent) {
99
+ toolEventQueue.push(streamResponse.toolCallEvent);
100
+ if (toolResolve) {
101
+ toolResolve();
102
+ toolResolve = null;
103
+ }
104
+ }
79
105
  }
80
106
  else {
107
+ if (streamResponse.token) {
108
+ textBuffer += streamResponse.token;
109
+ }
81
110
  stats = streamResponse.stats;
82
111
  statsResolver(stats);
112
+ toolCallsArray = streamResponse.toolCalls || [];
113
+ toolCallsResolver(toolCallsArray);
114
+ tokenDone = true;
115
+ toolDone = true;
116
+ if (tokenResolve) {
117
+ tokenResolve();
118
+ tokenResolve = null;
119
+ }
120
+ if (toolResolve) {
121
+ toolResolve();
122
+ toolResolve = null;
123
+ }
83
124
  }
84
125
  }
85
126
  }
127
+ }
128
+ catch (error) {
129
+ streamError = error instanceof Error ? error : new Error(String(error));
130
+ statsRejecter(error);
131
+ toolCallsRejecter(error);
132
+ tokenDone = true;
133
+ toolDone = true;
134
+ if (tokenResolve) {
135
+ tokenResolve();
136
+ tokenResolve = null;
137
+ }
138
+ if (toolResolve) {
139
+ toolResolve();
140
+ toolResolve = null;
141
+ }
142
+ }
143
+ };
144
+ void processResponses();
145
+ const textPromise = (async () => {
146
+ await statsPromise;
147
+ return textBuffer;
148
+ })();
149
+ textPromise.catch(() => { });
150
+ if (params.stream) {
151
+ const tokenStream = (async function* () {
152
+ while (true) {
153
+ if (tokenQueue.length > 0) {
154
+ yield tokenQueue.shift();
155
+ }
156
+ else if (tokenDone) {
157
+ if (streamError !== null) {
158
+ throw streamError;
159
+ }
160
+ break;
161
+ }
162
+ else {
163
+ await new Promise((resolve) => {
164
+ tokenResolve = resolve;
165
+ });
166
+ }
167
+ }
168
+ })();
169
+ const toolCallStream = (async function* () {
170
+ while (true) {
171
+ if (toolEventQueue.length > 0) {
172
+ yield toolEventQueue.shift();
173
+ }
174
+ else if (toolDone) {
175
+ if (streamError !== null) {
176
+ throw streamError;
177
+ }
178
+ break;
179
+ }
180
+ else {
181
+ await new Promise((resolve) => {
182
+ toolResolve = resolve;
183
+ });
184
+ }
185
+ }
86
186
  })();
87
- const textPromise = Promise.resolve("");
88
187
  return {
89
188
  tokenStream,
189
+ toolCallStream,
90
190
  text: textPromise,
91
191
  stats: statsPromise,
192
+ toolCalls: toolCallsPromise,
92
193
  };
93
194
  }
94
195
  else {
95
196
  const tokenStream = (async function* () {
96
197
  //Empty generator for non-streaming mode
97
198
  })();
98
- const textPromise = (async () => {
99
- let buffer = "";
100
- for await (const response of streamRpc(request)) {
101
- if (response.type === "completionStream") {
102
- const streamResponse = completionStreamResponseSchema.parse(response);
103
- buffer += streamResponse.token;
104
- if (streamResponse.done) {
105
- stats = streamResponse.stats;
106
- statsResolver(stats);
107
- }
108
- }
109
- }
110
- return buffer;
199
+ const toolCallStream = (async function* () {
200
+ //Empty generator for non-streaming mode
111
201
  })();
112
202
  return {
113
203
  tokenStream,
204
+ toolCallStream,
114
205
  text: textPromise,
115
206
  stats: statsPromise,
207
+ toolCalls: toolCallsPromise,
116
208
  };
117
209
  }
118
210
  }
@@ -1,4 +1,5 @@
1
1
  import { send } from "../rpc/rpc-client.js";
2
+ import { InvalidDeleteCacheParamsError, DeleteCacheFailedError, } from "../../utils/errors-client.js";
2
3
  /**
3
4
  * Deletes KV cache files.
4
5
  *
@@ -35,11 +36,11 @@ export async function deleteCache(params) {
35
36
  };
36
37
  }
37
38
  else {
38
- throw new Error("Invalid deleteCache parameters");
39
+ throw new InvalidDeleteCacheParamsError();
39
40
  }
40
41
  const response = (await send(req));
41
42
  if (!response.success && response.error) {
42
- throw new Error(response.error);
43
+ throw new DeleteCacheFailedError(response.error);
43
44
  }
44
45
  return {
45
46
  success: response.success,
@@ -14,9 +14,9 @@ export type DownloadAssetOptions = BaseDownloadAssetOptions;
14
14
  *
15
15
  * @returns Promise that resolves to the asset ID (either the provided assetSrc or a generated ID)
16
16
  *
17
- * @throws {Error} When asset download fails, with details in the error message
18
- * @throws {Error} When streaming ends unexpectedly (only when using onProgress)
19
- * @throws {Error} When receiving an invalid response type from the server
17
+ * @throws {QvacErrorBase} When asset download fails, with details in the error message
18
+ * @throws {QvacErrorBase} When streaming ends unexpectedly (only when using onProgress)
19
+ * @throws {QvacErrorBase} When receiving an invalid response type from the server
20
20
  *
21
21
  * @example
22
22
  * ```typescript
@@ -1,5 +1,6 @@
1
1
  import { send, stream } from "../rpc/rpc-client.js";
2
- import { downloadAssetRequestSchema, } from "../../schemas/index.js";
2
+ import { downloadAssetOptionsToRequestSchema, } from "../../schemas/index.js";
3
+ import { DownloadAssetFailedError, StreamEndedError, InvalidResponseError, } from "../../utils/errors-client.js";
3
4
  /**
4
5
  * Downloads an asset (model file) without loading it into memory.
5
6
  *
@@ -14,9 +15,9 @@ import { downloadAssetRequestSchema, } from "../../schemas/index.js";
14
15
  *
15
16
  * @returns Promise that resolves to the asset ID (either the provided assetSrc or a generated ID)
16
17
  *
17
- * @throws {Error} When asset download fails, with details in the error message
18
- * @throws {Error} When streaming ends unexpectedly (only when using onProgress)
19
- * @throws {Error} When receiving an invalid response type from the server
18
+ * @throws {QvacErrorBase} When asset download fails, with details in the error message
19
+ * @throws {QvacErrorBase} When streaming ends unexpectedly (only when using onProgress)
20
+ * @throws {QvacErrorBase} When receiving an invalid response type from the server
20
21
  *
21
22
  * @example
22
23
  * ```typescript
@@ -36,13 +37,7 @@ import { downloadAssetRequestSchema, } from "../../schemas/index.js";
36
37
  * ```
37
38
  */
38
39
  export async function downloadAsset(options) {
39
- const { assetSrc } = options;
40
- const request = downloadAssetRequestSchema.parse({
41
- type: "downloadAsset",
42
- assetSrc: assetSrc,
43
- withProgress: !!options.onProgress,
44
- seed: options.seed,
45
- });
40
+ const request = downloadAssetOptionsToRequestSchema.parse(options);
46
41
  if (options.onProgress) {
47
42
  // Use streaming for progress updates
48
43
  for await (const response of stream(request)) {
@@ -51,22 +46,22 @@ export async function downloadAsset(options) {
51
46
  }
52
47
  else if (response.type === "downloadAsset") {
53
48
  if (!response.success) {
54
- throw new Error(response.error ?? "Failed to download asset");
49
+ throw new DownloadAssetFailedError(response.error);
55
50
  }
56
- return response.assetId ?? assetSrc;
51
+ return response.assetId;
57
52
  }
58
53
  }
59
- throw new Error("Stream ended without final response");
54
+ throw new StreamEndedError();
60
55
  }
61
56
  else {
62
57
  // Use regular send for simple downloading
63
58
  const response = await send(request);
64
59
  if (response.type !== "downloadAsset") {
65
- throw new Error("Invalid response type");
60
+ throw new InvalidResponseError("downloadAsset");
66
61
  }
67
62
  if (!response.success) {
68
- throw new Error(response.error ?? "Failed to download asset");
63
+ throw new DownloadAssetFailedError(response.error);
69
64
  }
70
- return response.assetId ?? assetSrc;
65
+ return response.assetId;
71
66
  }
72
67
  }
@@ -5,7 +5,7 @@ import { type EmbedParams } from "../../schemas/index";
5
5
  * @param params - The parameters for the embedding
6
6
  * @param params.modelId - The identifier of the embedding model to use
7
7
  * @param params.text - The input text to embed
8
- * @throws {Error} When the response type is invalid or when the embedding fails
8
+ * @throws {QvacErrorBase} When the response type is invalid or when the embedding fails
9
9
  */
10
10
  export declare function embed(params: EmbedParams): Promise<number[]>;
11
11
  //# sourceMappingURL=embed.d.ts.map
@@ -1,12 +1,13 @@
1
1
  import { send } from "../rpc/rpc-client.js";
2
2
  import {} from "../../schemas/index.js";
3
+ import { InvalidResponseError, EmbedFailedError } from "../../utils/errors-client.js";
3
4
  /**
4
5
  * Generates embeddings for a given text using a specified model.
5
6
  *
6
7
  * @param params - The parameters for the embedding
7
8
  * @param params.modelId - The identifier of the embedding model to use
8
9
  * @param params.text - The input text to embed
9
- * @throws {Error} When the response type is invalid or when the embedding fails
10
+ * @throws {QvacErrorBase} When the response type is invalid or when the embedding fails
10
11
  */
11
12
  export async function embed(params) {
12
13
  const request = {
@@ -15,10 +16,10 @@ export async function embed(params) {
15
16
  };
16
17
  const response = await send(request);
17
18
  if (response.type !== "embed") {
18
- throw new Error("Invalid response type");
19
+ throw new InvalidResponseError("embed");
19
20
  }
20
21
  if (!response.success) {
21
- throw new Error(response.error ?? "Failed to embed");
22
+ throw new EmbedFailedError(response.error);
22
23
  }
23
24
  return response.embedding;
24
25
  }
@@ -0,0 +1,29 @@
1
+ import { type GetModelInfoParams } from "../../schemas/index";
2
+ export declare const getModelInfo: (params: GetModelInfoParams) => Promise<{
3
+ name: string;
4
+ modelId: string;
5
+ hyperdriveKey: string;
6
+ hyperbeeKey: string;
7
+ expectedSize: number;
8
+ sha256Checksum: string;
9
+ addon: "llm" | "whisper" | "embeddings" | "nmt" | "vad" | "tts";
10
+ isCached: boolean;
11
+ isLoaded: boolean;
12
+ cacheFiles: {
13
+ filename: string;
14
+ path: string;
15
+ expectedSize: number;
16
+ sha256Checksum: string;
17
+ isCached: boolean;
18
+ actualSize?: number | undefined;
19
+ cachedAt?: Date | undefined;
20
+ }[];
21
+ actualSize?: number | undefined;
22
+ cachedAt?: Date | undefined;
23
+ loadedInstances?: {
24
+ registryId: string;
25
+ loadedAt: Date;
26
+ config?: unknown;
27
+ }[] | undefined;
28
+ }>;
29
+ //# sourceMappingURL=get-model-info.d.ts.map
@@ -0,0 +1,14 @@
1
+ import {} from "../../schemas/index.js";
2
+ import { send } from "../rpc/rpc-client.js";
3
+ import { InvalidResponseError } from "../../utils/errors-client.js";
4
+ export const getModelInfo = async (params) => {
5
+ const request = {
6
+ type: "getModelInfo",
7
+ name: params.name,
8
+ };
9
+ const response = await send(request);
10
+ if (response.type !== "getModelInfo") {
11
+ throw new InvalidResponseError("getModelInfo");
12
+ }
13
+ return response.modelInfo;
14
+ };
@@ -12,4 +12,7 @@ export { cancel } from "./cancel";
12
12
  export { startQVACProvider } from "./provide";
13
13
  export { stopQVACProvider } from "./stop-provider";
14
14
  export { ragSaveEmbeddings, ragSearch, ragDeleteEmbeddings } from "./rag";
15
+ export { textToSpeech } from "./text-to-speech";
16
+ export { setConfig } from "./set-config";
17
+ export { getModelInfo } from "./get-model-info";
15
18
  //# sourceMappingURL=index.d.ts.map
@@ -12,3 +12,6 @@ export { cancel } from "./cancel.js";
12
12
  export { startQVACProvider } from "./provide.js";
13
13
  export { stopQVACProvider } from "./stop-provider.js";
14
14
  export { ragSaveEmbeddings, ragSearch, ragDeleteEmbeddings } from "./rag.js";
15
+ export { textToSpeech } from "./text-to-speech.js";
16
+ export { setConfig } from "./set-config.js";
17
+ export { getModelInfo } from "./get-model-info.js";
@@ -21,9 +21,9 @@ export type LoadModelOptions = BaseLoadModelOptions & {
21
21
  *
22
22
  * @returns Promise that resolves to the model ID (either the provided modelSrc or a generated ID)
23
23
  *
24
- * @throws {Error} When model loading fails, with details in the error message
25
- * @throws {Error} When streaming ends unexpectedly (only when using onProgress)
26
- * @throws {Error} When receiving an invalid response type from the server
24
+ * @throws {QvacErrorBase} When model loading fails, with details in the error message
25
+ * @throws {QvacErrorBase} When streaming ends unexpectedly (only when using onProgress)
26
+ * @throws {QvacErrorBase} When receiving an invalid response type from the server
27
27
  *
28
28
  * @example
29
29
  * ```typescript