@qvac/sdk 0.2.4-dev.1760969057.aafc839 → 0.2.4-dev.1760984293.e5addea

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.
@@ -54,8 +54,34 @@ function prepareMessagesForCache(cachePathToUse, cacheExists, history) {
54
54
  }
55
55
  async function* processModelResponse(model, messagesToSend, shouldSaveCache) {
56
56
  const response = await model.run(messagesToSend);
57
- for await (const token of response.iterate()) {
58
- yield token;
57
+ // Create a promise that rejects when onError is triggered
58
+ let rejectOnError = null;
59
+ const errorPromise = new Promise((_, reject) => {
60
+ rejectOnError = reject;
61
+ });
62
+ response.onError((error) => {
63
+ if (rejectOnError) {
64
+ rejectOnError(error);
65
+ }
66
+ });
67
+ const iterator = response.iterate();
68
+ try {
69
+ while (true) {
70
+ // Race between getting the next token and an error occurring
71
+ const result = await Promise.race([iterator.next(), errorPromise]);
72
+ if (result.done) {
73
+ break;
74
+ }
75
+ yield result.value;
76
+ }
77
+ }
78
+ catch (error) {
79
+ if (error instanceof Error) {
80
+ throw error;
81
+ }
82
+ else {
83
+ throw new Error(String(error));
84
+ }
59
85
  }
60
86
  if (shouldSaveCache) {
61
87
  await model.run([{ role: "session", content: "save" }]);
@@ -8,6 +8,7 @@ export interface AnyModel {
8
8
  onUpdate(callback: (data: string) => void): {
9
9
  await: () => Promise<void>;
10
10
  };
11
+ onError(callback: (error: Error) => void): void;
11
12
  onUpdate(callback: (data: Array<{
12
13
  text: string;
13
14
  }>) => void): {
@@ -1,52 +1,38 @@
1
1
  import { completion } from "../../bare/addons/llamacpp/index.js";
2
2
  export async function* handleCompletionStream(request) {
3
- try {
4
- // Log tools if provided
5
- if (request.tools) {
6
- console.log("Tools passed to completionStream:", request.tools);
3
+ const filteredHistory = request.history.map(({ role, content, attachments }) => ({
4
+ role,
5
+ content,
6
+ attachments: attachments ?? [],
7
+ }));
8
+ const stream = completion({
9
+ history: filteredHistory,
10
+ modelId: request.modelId,
11
+ kvCache: request.kvCache,
12
+ });
13
+ let stats;
14
+ let done = false;
15
+ let buffer = "";
16
+ while (!done) {
17
+ const result = await stream.next();
18
+ if (result.done) {
19
+ stats = result.value;
20
+ done = true;
7
21
  }
8
- const filteredHistory = request.history.map(({ role, content, attachments }) => ({
9
- role,
10
- content,
11
- attachments: attachments ?? [],
12
- }));
13
- const stream = completion({
14
- history: filteredHistory,
15
- modelId: request.modelId,
16
- kvCache: request.kvCache,
17
- });
18
- let stats;
19
- let done = false;
20
- let buffer = "";
21
- while (!done) {
22
- const result = await stream.next();
23
- if (result.done) {
24
- stats = result.value;
25
- done = true;
26
- }
27
- else {
28
- buffer += result.value;
29
- if (request.stream) {
30
- yield {
31
- type: "completionStream",
32
- token: result.value,
33
- };
34
- }
22
+ else {
23
+ buffer += result.value;
24
+ if (request.stream) {
25
+ yield {
26
+ type: "completionStream",
27
+ token: result.value,
28
+ };
35
29
  }
36
30
  }
37
- yield {
38
- type: "completionStream",
39
- token: buffer,
40
- done: true,
41
- stats,
42
- };
43
- }
44
- catch (error) {
45
- console.error("Error during completion stream:", error);
46
- yield {
47
- type: "completionStream",
48
- token: "",
49
- error: error instanceof Error ? error.message : "Unknown error",
50
- };
51
31
  }
32
+ yield {
33
+ type: "completionStream",
34
+ token: buffer,
35
+ done: true,
36
+ stats,
37
+ };
52
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qvac/sdk",
3
- "version": "0.2.4-dev.1760969057.aafc839",
3
+ "version": "0.2.4-dev.1760984293.e5addea",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -54,7 +54,7 @@
54
54
  },
55
55
  "dependencies": {
56
56
  "@qvac/embed-llamacpp": "^0.3.0",
57
- "@qvac/llm-llamacpp": "^0.3.0",
57
+ "@qvac/llm-llamacpp": "^0.3.1",
58
58
  "@qvac/decoder-audio": "^0.1.0",
59
59
  "@qvac/error": "^0.1.0",
60
60
  "@qvac/langdetect-text": "^0.1.0",