pplx-zero 2.3.0 → 2.3.1

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 (3) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +21 -17
  3. package/src/rag.ts +62 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pplx-zero",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "Fast, minimal Perplexity AI CLI. Stream answers, analyze PDFs/images, continue conversations. Zero bloat.",
5
5
  "author": "kenzo",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ import { getEnv } from './env';
6
6
  import { fmt, write, writeLn } from './output';
7
7
  import { appendHistory, readHistory, getLastEntry } from './history';
8
8
  import { renderMarkdown, createMarkdownState } from './markdown';
9
- import { search as ragSearch, ingestDirectory, ingestFile, ingestPath, getDocCount, getKnowledgeDir } from './rag';
9
+ import { search as ragSearch, searchForRag, ingestDirectory, ingestFile, ingestPath, getDocCount, getKnowledgeDir } from './rag';
10
10
 
11
11
  getEnv();
12
12
 
@@ -42,7 +42,7 @@ Options:
42
42
  -i, --image <path> Attach an image (PNG, JPG, etc.)
43
43
  -o, --output <path> Save output to file (.md, .txt)
44
44
  -c, --continue Continue from last query (add context)
45
- -l, --local Search local knowledge base first
45
+ -l, --local RAG: search local docs, inject as context
46
46
  --ingest [path] Index file/dir/glob (default: ~/.pplx/knowledge/)
47
47
  --history Show query history
48
48
  --no-history Don't save this query to history
@@ -99,6 +99,8 @@ if (values.ingest) {
99
99
  process.exit(0);
100
100
  }
101
101
 
102
+ let ragContext = '';
103
+
102
104
  if (values.local) {
103
105
  const query = positionals.join(' ');
104
106
  if (!query) {
@@ -106,26 +108,24 @@ if (values.local) {
106
108
  process.exit(2);
107
109
  }
108
110
 
109
- const results = ragSearch(query);
111
+ const results = searchForRag(query);
110
112
 
111
113
  if (results.length === 0) {
112
- console.log('No local documents match. Try --ingest first or check ~/.pplx/knowledge/');
113
- process.exit(0);
114
- }
115
-
116
- console.log(`\n${fmt.model('local')} Found ${results.length} result(s):\n`);
117
-
118
- for (let i = 0; i < results.length; i++) {
119
- const r = results[i]!;
120
- console.log(`[${i + 1}] ${r.title}`);
121
- console.log(` ${r.path}`);
122
- console.log(` ${r.snippet}\n`);
114
+ console.log('No local documents match. Proceeding with Perplexity only...\n');
115
+ } else {
116
+ if (!values.json) {
117
+ console.log(`${fmt.model('local')} Found ${results.length} relevant doc(s), using as context...\n`);
118
+ }
119
+
120
+ ragContext = 'Context from user\'s knowledge base:\n---\n';
121
+ for (const r of results) {
122
+ ragContext += `[${r.title}]:\n${r.content}\n\n`;
123
+ }
124
+ ragContext += '---\n\nQuestion: ';
123
125
  }
124
-
125
- process.exit(0);
126
126
  }
127
127
 
128
- if (positionals.length === 0 && !values.continue) {
128
+ if (positionals.length === 0 && !values.continue && !values.local) {
129
129
  console.error(fmt.error('No query provided. Use -h for help.'));
130
130
  process.exit(2);
131
131
  }
@@ -139,6 +139,10 @@ const model = values.model as Model;
139
139
 
140
140
  let query = positionals.join(' ');
141
141
 
142
+ if (ragContext) {
143
+ query = ragContext + query;
144
+ }
145
+
142
146
  if (values.continue) {
143
147
  const last = await getLastEntry();
144
148
  if (last) {
package/src/rag.ts CHANGED
@@ -103,6 +103,12 @@ export async function ingestDirectory(dir?: string): Promise<IngestStats> {
103
103
  export function search(query: string, limit = 5): SearchResult[] {
104
104
  const db = getDb();
105
105
 
106
+ const ftsQuery = query
107
+ .trim()
108
+ .split(/\s+/)
109
+ .map(word => `${word}*`)
110
+ .join(' OR ');
111
+
106
112
  const stmt = db.prepare(`
107
113
  SELECT
108
114
  path,
@@ -116,7 +122,62 @@ export function search(query: string, limit = 5): SearchResult[] {
116
122
  `);
117
123
 
118
124
  try {
119
- return stmt.all(query, limit) as SearchResult[];
125
+ return stmt.all(ftsQuery, limit) as SearchResult[];
126
+ } catch {
127
+ return [];
128
+ }
129
+ }
130
+
131
+ export interface RagContext {
132
+ title: string;
133
+ content: string;
134
+ }
135
+
136
+ function truncateUtf8Safe(str: string, maxLen: number): string {
137
+ if (str.length <= maxLen) return str;
138
+ let truncated = str.slice(0, maxLen);
139
+ const lastChar = truncated.charCodeAt(truncated.length - 1);
140
+ if (lastChar >= 0xD800 && lastChar <= 0xDBFF) {
141
+ truncated = truncated.slice(0, -1);
142
+ }
143
+ return truncated;
144
+ }
145
+
146
+ export function searchForRag(query: string, limit = 3, maxChars = 4000): RagContext[] {
147
+ const db = getDb();
148
+
149
+ const ftsQuery = query
150
+ .trim()
151
+ .split(/\s+/)
152
+ .map(word => `${word}*`)
153
+ .join(' OR ');
154
+
155
+ const stmt = db.prepare(`
156
+ SELECT title, content
157
+ FROM docs_fts
158
+ WHERE docs_fts MATCH ?
159
+ ORDER BY bm25(docs_fts)
160
+ LIMIT ?
161
+ `);
162
+
163
+ try {
164
+ const results = stmt.all(ftsQuery, limit) as { title: string; content: string }[];
165
+ let totalChars = 0;
166
+ const truncated: RagContext[] = [];
167
+
168
+ for (const r of results) {
169
+ const remaining = maxChars - totalChars;
170
+ if (remaining <= 0) break;
171
+
172
+ const content = truncateUtf8Safe(r.content, remaining);
173
+ truncated.push({
174
+ title: r.title,
175
+ content,
176
+ });
177
+ totalChars += content.length;
178
+ }
179
+
180
+ return truncated;
120
181
  } catch {
121
182
  return [];
122
183
  }