feat(ai): search + read grounding tools
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
23
lib/ai/agent/tools/read.js
Normal file
23
lib/ai/agent/tools/read.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import { pool } from '../../../db/pool.js';
|
||||
|
||||
const TABLE = { page: 'pages', ref: 'refs', task: 'tasks', conversation: 'conversations' };
|
||||
|
||||
export const readTool = {
|
||||
name: 'read',
|
||||
description: 'Read a single entity (page, ref, task, conversation) by id for grounding.',
|
||||
input_schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
kind: { type: 'string', enum: ['page', 'ref', 'task', 'conversation'] },
|
||||
id: { type: 'string', description: 'uuid of the entity' }
|
||||
},
|
||||
required: ['kind', 'id']
|
||||
},
|
||||
async handler({ kind, id }, _ctx) {
|
||||
const table = TABLE[kind];
|
||||
if (!table) return { error: `unknown kind "${kind}"` };
|
||||
const { rows: [row] } = await pool.query(`SELECT * FROM ${table} WHERE id=$1`, [id]);
|
||||
if (!row) return { error: `${kind} ${id} not found` };
|
||||
return row;
|
||||
}
|
||||
};
|
||||
28
lib/ai/agent/tools/search.js
Normal file
28
lib/ai/agent/tools/search.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import * as searchRepo from '../../../db/repos/search.js';
|
||||
|
||||
export const searchTool = {
|
||||
name: 'search',
|
||||
description: 'Full-text search across pages, refs, source docs and messages in the current Space. Use to find information before answering.',
|
||||
input_schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
q: { type: 'string', description: 'search query' },
|
||||
kinds: {
|
||||
type: 'array',
|
||||
items: { type: 'string', enum: ['page', 'ref', 'source_doc', 'message'] },
|
||||
description: 'optional filter of result kinds'
|
||||
}
|
||||
},
|
||||
required: ['q']
|
||||
},
|
||||
async handler({ q, kinds }, ctx) {
|
||||
const results = await searchRepo.fts({
|
||||
q,
|
||||
space_id: ctx.space_id ?? null,
|
||||
kinds: kinds?.length ? kinds : null,
|
||||
limit: 8,
|
||||
offset: 0
|
||||
});
|
||||
return { results };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user