xiaozuoassistant 0.2.28 → 0.2.30

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.
@@ -29,10 +29,21 @@ export class VectorMemory {
29
29
  if (!tableNames.includes('memories')) {
30
30
  // Create table with dummy data to define schema, then delete it
31
31
  // LanceDB schema inference is based on data
32
+ let vectorDim = 1536; // Fallback default
33
+ // 动态探测向量维度:通过调用一次真实的 embedding API,获取准确的维度
34
+ console.log(`[VectorMemory] Detecting embedding dimension for model: ${config.llm.embeddingModel || 'default'}...`);
35
+ const testVector = await this.getEmbedding('init');
36
+ if (testVector && testVector.length > 0) {
37
+ vectorDim = testVector.length;
38
+ console.log(`[VectorMemory] Detected actual embedding dimension: ${vectorDim}`);
39
+ }
40
+ else {
41
+ console.warn(`[VectorMemory] Failed to detect dimension, falling back to ${vectorDim}`);
42
+ }
32
43
  const dummyData = [{
33
44
  id: 'init',
34
45
  text: 'init',
35
- vector: Array(1536).fill(0), // OpenAI embedding dimension
46
+ vector: Array(vectorDim).fill(0),
36
47
  metadata: { type: 'recent', timestamp: Date.now(), sessionId: 'init', tags: '' }
37
48
  }];
38
49
  this.table = await this.db.createTable('memories', dummyData);
@@ -50,12 +61,10 @@ export class VectorMemory {
50
61
  async getEmbedding(text) {
51
62
  if (!this.openai) {
52
63
  console.warn('OpenAI client not initialized (missing API Key), skipping embedding.');
53
- return Array(1536).fill(0);
64
+ return []; // Return empty instead of zero array
54
65
  }
55
66
  try {
56
67
  // Use OpenAI compatible embedding endpoint
57
- // Note: Some compatible providers might use different models/dimensions.
58
- // Default to text-embedding-ada-002 or similar standard.
59
68
  const model = config.llm.embeddingModel || 'text-embedding-ada-002';
60
69
  const response = await this.openai.embeddings.create({
61
70
  model: model,
@@ -65,9 +74,7 @@ export class VectorMemory {
65
74
  }
66
75
  catch (error) {
67
76
  console.error('Embedding failed:', error);
68
- // Fallback: return zero vector or throw?
69
- // For MVP robustness, return zeros if embedding fails (search won't work but app won't crash)
70
- return Array(1536).fill(0);
77
+ return []; // Return empty instead of zero array to trigger upstream handling
71
78
  }
72
79
  }
73
80
  async addMemory(text, type, metadata = {}) {
@@ -76,6 +83,10 @@ export class VectorMemory {
76
83
  if (!this.table)
77
84
  return; // DB failed to init
78
85
  const vector = await this.getEmbedding(text);
86
+ if (!vector || vector.length === 0) {
87
+ console.error('Failed to get valid embedding, skipping memory addition.');
88
+ return;
89
+ }
79
90
  const memory = {
80
91
  id: Math.random().toString(36).substring(7),
81
92
  text,
@@ -99,6 +110,10 @@ export class VectorMemory {
99
110
  if (!this.table)
100
111
  return [];
101
112
  const vector = await this.getEmbedding(query);
113
+ if (!vector || vector.length === 0) {
114
+ console.error('Failed to get valid embedding for query, skipping search.');
115
+ return [];
116
+ }
102
117
  // Check if table supports search
103
118
  if (this.table.search) {
104
119
  let search = this.table.search(vector).limit(limit);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xiaozuoassistant",
3
- "version": "0.2.28",
3
+ "version": "0.2.30",
4
4
  "description": "A local-first personal AI assistant with multi-channel support and enhanced memory.",
5
5
  "author": "mantle.lau",
6
6
  "license": "MIT",