From 2e121ce6d4e8cc7bd2b9881871915e9774a495e9 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 1 Jun 2026 18:14:42 +1000 Subject: [PATCH] =?UTF-8?q?feat(ai):=20context=20tool=20=E2=80=94=20resolv?= =?UTF-8?q?e=20the=20active=20view=20entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 --- lib/ai/agent/tools/context.js | 20 ++++++++++++++++++++ tests/ai/agent/tools/context.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 lib/ai/agent/tools/context.js create mode 100644 tests/ai/agent/tools/context.test.js diff --git a/lib/ai/agent/tools/context.js b/lib/ai/agent/tools/context.js new file mode 100644 index 0000000..4a5f9f0 --- /dev/null +++ b/lib/ai/agent/tools/context.js @@ -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 }; + } +}; diff --git a/tests/ai/agent/tools/context.test.js b/tests/ai/agent/tools/context.test.js new file mode 100644 index 0000000..3d78827 --- /dev/null +++ b/tests/ai/agent/tools/context.test.js @@ -0,0 +1,26 @@ +import { describe, it, expect, beforeAll } from 'vitest'; +import { pool } from '../../../../lib/db/pool.js'; +import { resetDb } from '../../../helpers/db.js'; +import { migrateUp } from '../../../../lib/db/migrate.js'; +import { contextTool } from '../../../../lib/ai/agent/tools/context.js'; + +let spaceId, taskId; +beforeAll(async () => { + await resetDb(); await migrateUp(); + ({ rows: [{ id: spaceId }] } = await pool.query( + `INSERT INTO spaces(slug,name) VALUES('s','S') RETURNING id`)); + ({ rows: [{ id: taskId }] } = await pool.query( + `INSERT INTO tasks(space_id,title) VALUES($1,'Wire telemetry') RETURNING id`, [spaceId])); +}); + +describe('context tool', () => { + it('summarises the current view entity', async () => { + const out = await contextTool.handler({}, { space_id: spaceId, view: { entityType: 'task', entityId: taskId } }); + expect(out.entityType).toBe('task'); + expect(out.title).toBe('Wire telemetry'); + }); + it('handles no active view', async () => { + const out = await contextTool.handler({}, { space_id: spaceId, view: null }); + expect(out.note).toMatch(/no specific entity/i); + }); +});