feat(ai): context tool — resolve the active view entity
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
20
lib/ai/agent/tools/context.js
Normal file
20
lib/ai/agent/tools/context.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { pool } from '../../../db/pool.js';
|
||||
|
||||
const TABLE = { page: 'pages', ref: 'refs', task: 'tasks', project: 'projects', space: 'spaces' };
|
||||
|
||||
export const contextTool = {
|
||||
name: 'context',
|
||||
description: "Resolve what the owner is currently looking at (the active view). Call this first to ground your answer in the right entity.",
|
||||
input_schema: { type: 'object', properties: {} },
|
||||
async handler(_args, ctx) {
|
||||
const view = ctx.view;
|
||||
if (!view?.entityType || !view?.entityId) {
|
||||
return { note: 'No specific entity is active; only the Space context is available.', space_id: ctx.space_id };
|
||||
}
|
||||
const table = TABLE[view.entityType];
|
||||
if (!table) return { entityType: view.entityType, entityId: view.entityId, note: 'unrecognised entity type' };
|
||||
const { rows: [row] } = await pool.query(`SELECT * FROM ${table} WHERE id=$1`, [view.entityId]);
|
||||
if (!row) return { entityType: view.entityType, entityId: view.entityId, error: 'not found' };
|
||||
return { entityType: view.entityType, ...row };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user