Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 176 additions & 2 deletions packages/api/src/agent-side/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,19 @@ const draft = {
};

const archive = mock();
const markDone = mock();
const unarchive = mock();
const send = mock();
const sendNew = mock();
const read = mock();
const sendReply = mock();
const connector = {list: mock(), get: mock(), create: mock(), update: mock(), remove: mock(), commands: {archive, send}, newCommands: {sendNew, read, send: sendReply}};
const open = mock();
const connector = {list: mock(), get: mock(), create: mock(), update: mock(), remove: mock(), commands: {archive, send, markDone, unarchive}, newCommands: {sendNew, read, send: sendReply, open}};

function useConnection(connection: typeof vaultConnection | typeof emailConnection | typeof notionConnection | typeof googleConnection) {
// A member's Chat, which no agent may do anything with by default.
const chatConnection = {...vaultConnection, id: 'chat-1', integrationId: 'chat', account: 'Acme', defaults: []};

function useConnection(connection: typeof vaultConnection | typeof emailConnection | typeof notionConnection | typeof googleConnection | typeof chatConnection) {
loadConnection.mockResolvedValue(Ok({connection, integration: integration(connection.integrationId)}));
}

Expand Down Expand Up @@ -586,6 +592,174 @@ describe('runAgentTool', () => {
});
});

describe('where a thread stands, through the Chat connection', () => {
const thread = (standing: string, place = 'Slack') => ({id: 'thread-2', values: {title: 'Pricing review with Dana', standing, place}, updatedAt: '2026-10-01T00:00:00.000Z'});
const builtIn = (actions: string[]) => ({...agent, providerId: 'proxy', grants: actions.map((actionId) => ({connectionId: 'chat-1', actionId, allowed: true}))});

beforeEach(() => {
useConnection(chatConnection);
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads', 'markThreads'])));
connector.get.mockResolvedValue(Ok(thread('Waiting for you')));
});

test('is marked done, archived and brought back by the connector, each logged with the thread’s title, a thread in Slack as one in Chat', async () => {
const {runAgentTool} = await import('./tools');
markDone.mockResolvedValue(Ok(thread('Done')));
archive.mockResolvedValue(Ok(thread('Archived')));
unarchive.mockResolvedValue(Ok(thread('Done', 'Chat')));

expect((await runAgentTool(caller, 'chat-1', 'threads_mark_done', {id: 'thread-2'})).unwrap()).toEqual({record: thread('Done')});
expect((await runAgentTool(caller, 'chat-1', 'threads_archive', {id: 'thread-2'})).unwrap()).toEqual({record: thread('Archived')});
connector.get.mockResolvedValue(Ok(thread('Archived', 'Chat')));
expect((await runAgentTool(caller, 'chat-1', 'threads_unarchive', {id: 'thread-2'})).unwrap()).toEqual({record: thread('Done', 'Chat')});

expect(markDone.mock.calls[0]?.[1]).toBe('thread-2');
expect(logAgentRequest.mock.calls.map(([logged]) => [logged.connectionId, logged.collectionId, logged.action, logged.recordTitle, logged.outcome])).toEqual([
['chat-1', 'threads', 'markDone', 'Pricing review with Dana', 'allowed'],
['chat-1', 'threads', 'archive', 'Pricing review with Dana', 'allowed'],
['chat-1', 'threads', 'unarchive', 'Pricing review with Dana', 'allowed'],
]);
});

test('is refused without Mark done and archive, and logged as denied', async () => {
const {runAgentTool} = await import('./tools');
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads'])));

expect((await runAgentTool(caller, 'chat-1', 'threads_archive', {id: 'thread-2'})).unwrapErr().kind).toBe('forbidden');
expect(archive).not.toHaveBeenCalled();
expect(logAgentRequest.mock.calls[0]?.[0]).toMatchObject({collectionId: 'threads', action: 'archive', outcome: 'denied'});
});

test('is not changed for a thread in progress, nor archived while watching, nor brought back unless archived, nor for one that isn’t the member’s', async () => {
const {runAgentTool} = await import('./tools');
connector.get.mockResolvedValue(Ok(thread('In progress')));

expect((await runAgentTool(caller, 'chat-1', 'threads_archive', {id: 'thread-2'})).unwrapErr()).toMatchObject({kind: 'validation_error', message: expect.stringContaining('Waiting for you')});
expect((await runAgentTool(caller, 'chat-1', 'threads_mark_done', {id: 'thread-2'})).unwrapErr().kind).toBe('validation_error');
connector.get.mockResolvedValue(Ok(thread('Watching')));
expect((await runAgentTool(caller, 'chat-1', 'threads_archive', {id: 'thread-2'})).unwrapErr().kind).toBe('validation_error');
connector.get.mockResolvedValue(Ok(thread('Done')));
expect((await runAgentTool(caller, 'chat-1', 'threads_unarchive', {id: 'thread-2'})).unwrapErr().kind).toBe('validation_error');
expect(archive).not.toHaveBeenCalled();
expect(markDone).not.toHaveBeenCalled();
expect(unarchive).not.toHaveBeenCalled();

connector.get.mockResolvedValue(Err(ApiErr.notFound('record', 'thread-9')));
expect((await runAgentTool(caller, 'chat-1', 'threads_mark_done', {id: 'thread-9'})).unwrapErr().kind).toBe('not_found');
expect(markDone).not.toHaveBeenCalled();
});
});

describe('a message sent to a thread, through the Chat connection', () => {
const thread = (standing: string) => ({id: 'thread-2', values: {title: 'Upgrade vite', standing, place: 'Chat'}, updatedAt: '2026-10-01T00:00:00.000Z'});
const builtIn = (actions: string[]) => ({...agent, providerId: 'proxy', grants: actions.map((actionId) => ({connectionId: 'chat-1', actionId, allowed: true}))});

beforeEach(() => {
useConnection(chatConnection);
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads', 'writeThreads'])));
connector.get.mockResolvedValue(Ok(thread('In progress')));
});

test('is sent by the connector with its text, for the caller’s task, to a thread whatever it stands, and logged with the thread’s title', async () => {
const {runAgentTool} = await import('./tools');
send.mockResolvedValue(Ok(thread('In progress')));

expect((await runAgentTool(caller, 'chat-1', 'threads_send', {id: 'thread-2', text: 'Yes, include the docs site too'})).unwrap()).toEqual({record: thread('In progress')});

const [target, recordId, values] = send.mock.calls[0] ?? [];
expect(target).toMatchObject({by: {taskId: 'task-1'}, collection: {id: 'threads'}});
expect(recordId).toBe('thread-2');
expect(values).toEqual({text: 'Yes, include the docs site too'});
expect(logAgentRequest.mock.calls.map(([logged]) => [logged.collectionId, logged.action, logged.recordTitle, logged.outcome, logged.taskId])).toEqual([
['threads', 'send', 'Upgrade vite', 'allowed', 'task-1'],
]);
});

test('needs its text', async () => {
const {runAgentTool} = await import('./tools');

expect((await runAgentTool(caller, 'chat-1', 'threads_send', {id: 'thread-2'})).unwrapErr()).toMatchObject({kind: 'validation_error'});
expect(send).not.toHaveBeenCalled();
});

test('is refused without Write in threads, and logged as denied', async () => {
const {runAgentTool} = await import('./tools');
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads', 'markThreads'])));

expect((await runAgentTool(caller, 'chat-1', 'threads_send', {id: 'thread-2', text: 'Hello'})).unwrapErr().kind).toBe('forbidden');
expect(send).not.toHaveBeenCalled();
expect(logAgentRequest.mock.calls[0]?.[0]).toMatchObject({collectionId: 'threads', action: 'send', outcome: 'denied'});
});

test('the connector refuses is logged as failed, with its kind', async () => {
const {runAgentTool} = await import('./tools');
send.mockResolvedValue(Err(ApiErr.validationError('Only the agent of one of your threads can send a message to another')));

expect((await runAgentTool(caller, 'chat-1', 'threads_send', {id: 'thread-2', text: 'Hello'})).unwrapErr()).toMatchObject({kind: 'validation_error'});
expect(logAgentRequest.mock.calls[0]?.[0]).toMatchObject({collectionId: 'threads', action: 'send', recordTitle: 'Upgrade vite', outcome: 'failed', error: 'validation_error'});
});
});

describe('a thread opened from a thread, through the Chat connection', () => {
const thread = {
id: 'thread-3',
values: {title: 'Draft the renewal email', agent: 'Legal', standing: 'In progress', place: 'Slack'},
updatedAt: '2026-10-01T00:00:00.000Z',
url: 'https://app.slack.com/client/T01/D01',
};
const builtIn = (actions: string[]) => ({...agent, providerId: 'proxy', grants: actions.map((actionId) => ({connectionId: 'chat-1', actionId, allowed: true}))});

beforeEach(() => {
useConnection(chatConnection);
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads', 'writeThreads'])));
});

test('is opened by the connector with its message, agent and place, for the caller’s task, and logged with the new thread’s title', async () => {
const {runAgentTool} = await import('./tools');
open.mockResolvedValue(Ok(thread));

expect((await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Draft the renewal email', agent: 'Legal', place: 'Slack'})).unwrap()).toEqual({record: thread});

const [target, values] = open.mock.calls[0] ?? [];
expect(target).toMatchObject({by: {taskId: 'task-1'}, collection: {id: 'threads'}});
expect(values).toEqual({text: 'Draft the renewal email', agent: 'Legal', place: 'Slack'});
expect(logAgentRequest.mock.calls.map(([logged]) => [logged.collectionId, logged.action, logged.recordTitle, logged.outcome, logged.taskId])).toEqual([
['threads', 'open', 'Draft the renewal email', 'allowed', 'task-1'],
]);
});

test('takes its agent and place as left out where not given, and a place only by its word', async () => {
const {runAgentTool} = await import('./tools');
open.mockResolvedValue(Ok(thread));

(await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Draft it'})).unwrap();
expect(open.mock.calls[0]?.[1]).toEqual({text: 'Draft it', agent: '', place: ''});
expect((await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Draft it', place: 'Email'})).unwrapErr()).toMatchObject({
kind: 'validation_error',
message: 'place must be one of Chat, Slack',
});
expect((await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Draft it', title: 'Renewal'})).unwrapErr()).toMatchObject({kind: 'validation_error'});
expect(open).toHaveBeenCalledTimes(1);
});

test('is refused without Write in threads, and logged as denied', async () => {
const {runAgentTool} = await import('./tools');
getAgentById.mockResolvedValue(Ok(builtIn(['readThreads', 'markThreads'])));

expect((await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Hello'})).unwrapErr().kind).toBe('forbidden');
expect(open).not.toHaveBeenCalled();
expect(logAgentRequest.mock.calls[0]?.[0]).toMatchObject({collectionId: 'threads', action: 'open', outcome: 'denied'});
});

test('the connector refuses is logged as failed, with its kind', async () => {
const {runAgentTool} = await import('./tools');
open.mockResolvedValue(Err(ApiErr.validationError('Finance isn’t in Slack')));

expect((await runAgentTool(caller, 'chat-1', 'threads_open', {text: 'Hello', place: 'Slack'})).unwrapErr()).toMatchObject({message: 'Finance isn’t in Slack'});
expect(logAgentRequest.mock.calls[0]?.[0]).toMatchObject({collectionId: 'threads', action: 'open', outcome: 'failed', error: 'validation_error'});
});
});

describe('nested collections', () => {
test('a list opened inside a record is logged with where it was opened', async () => {
useConnection(notionConnection);
Expand Down
15 changes: 10 additions & 5 deletions packages/api/src/agent-side/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {noteRejectedSignIn} from '../connections/rejected-sign-in';
import {needsSignIn, signInNeededError, unavailableActions, unavailableError} from '../connections/unavailable';
import {isRecordStore, type Connector, type DataRecord, type RecordStore, type RecordTarget, type RecordValues} from '../records/connector';
import {connectorFor} from '../records/connectors';
import {parseRecordValues} from '../records/record-values';
import {parseFieldValues, parseRecordValues} from '../records/record-values';
import {loadConnection} from '../records/target';
import {log as serverLog} from '../observability/log';
import {integrationOf} from '../utils/connection-actions';
Expand Down Expand Up @@ -528,7 +528,7 @@ function executeKind(run: ToolRun, params: Record<string, string>): Promise<Resu
if (kind === 'delete') {
return remove(stored, params.id ?? '');
}
return command(stored, params.id ?? '');
return command(stored, params);
}

// The params a write sets, without the record's id or the one a new record goes in.
Expand Down Expand Up @@ -619,12 +619,15 @@ function remove(run: StoreRun, recordId: string): Promise<Result<ToolResult, Api
});
}

function command(run: StoreRun, recordId: string): Promise<Result<ToolResult, ApiError>> {
// A command on a record, with what it takes beside the record's id, where it takes anything, as
// sending a message to a thread takes its text.
function command(run: StoreRun, params: Record<string, string>): Promise<Result<ToolResult, ApiError>> {
return Do(async ($) => {
const runner = $(requirePresent(run.connector.commands?.[run.tool.operation], ApiErr.notFound('tool', run.tool.name)));
const recordId = params.id ?? '';
const before = $(await logFailure(run, await run.connector.get(run.target, recordId), null));
$(requireApplies(run.tool.where, before));
const record = $(await logFailure(run, await runner(run.target, recordId), recordTitle(run.target.collection, before.values)));
const record = $(await logFailure(run, await runner(run.target, recordId, fieldsOf(params)), recordTitle(run.target.collection, before.values)));
$(await log(run, 'allowed', recordTitle(run.target.collection, before.values)));
return {record};
});
Expand All @@ -633,7 +636,9 @@ function command(run: StoreRun, recordId: string): Promise<Result<ToolResult, Ap
function newCommand(run: ToolRun, params: Record<string, string>): Promise<Result<ToolResult, ApiError>> {
return Do(async ($) => {
const runner = $(requirePresent(run.connector.newCommands?.[run.tool.operation], ApiErr.notFound('tool', run.tool.name)));
const values = $(parseRecordValues(run.target.collection, params));
// Its own values where it takes them, as opening a thread does, else the collection's fields.
const own = run.target.collection.commands?.find((candidate) => candidate.id === run.tool.operation)?.params;
const values = $(own ? parseFieldValues(own, params) : parseRecordValues(run.target.collection, params));
const record = $(await logFailure(run, await runner(run.target, values), recordTitle(run.target.collection, values)));
// Titled as it came back where it did, as an attachment read by its number is by its name.
$(await log(run, 'allowed', (record && recordTitle(run.target.collection, record.values)) ?? recordTitle(run.target.collection, values)));
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/harness/alert-sweep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const watch: AlertRow = {
terms: ['loan'],
split: null,
lastMatchedAt: null,
slackAppId: null,
slackChannel: null,
createdAt: now,
};
const items = [
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/harness/alerts-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const alert: MemberAlertRow = {
terms: [],
split: null,
lastMatchedAt: null,
slackAppId: null,
slackChannel: null,
createdAt: now,
persona: {id: 'persona-1', name: 'Travel', look, color: 'teal', harness: 'proxy', chatModel: 'claude', ownerUserId: null, createdById: null, createdAt: now},
thread: {id: 'thread-1', title: 'Plan the offsite in Lisbon', slack},
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/harness/alerts-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function toAlert(alert: MemberAlertRow, connections: Map<string, ConnectionRow>)
brief: alert.brief,
persona: {id: alert.persona.id, name: alert.persona.name, look: alert.persona.look, color: toAvatarColor(alert.persona.color)},
opens: alert.threadId === null ? 'new_thread' : 'this_thread',
opensInSlack: alert.threadId === null && alert.slackAppId !== null,
thread: toThread(alert.thread),
setIn: toThread(alert.setIn),
at: nextAt(alert)?.toISOString() ?? null,
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/harness/alerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const watch: AlertRow = {
terms: ['@bank.com', 'loan'],
split: null,
lastMatchedAt: null,
slackAppId: null,
slackChannel: null,
createdAt: now,
};

Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/harness/fake-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function fakeStore(): FakeStore {
getMemberChat: mock<HarnessStore['getMemberChat']>(),
ensureMemberChat: mock<HarnessStore['ensureMemberChat']>(),
listAttachments: mock<HarnessStore['listAttachments']>(),
listPassedOnThreads: mock<HarnessStore['listPassedOnThreads']>(),
openThread: mock<HarnessStore['openThread']>(),
replyInThread: mock<HarnessStore['replyInThread']>(),
listChatThreads: mock<HarnessStore['listChatThreads']>(),
Expand Down Expand Up @@ -72,6 +73,9 @@ export function fakeStore(): FakeStore {
messageSubAgentInline: mock<HarnessStore['messageSubAgentInline']>(),
tellParentInline: mock<HarnessStore['tellParentInline']>(),
completeDismiss: mock<HarnessStore['completeDismiss']>(),
// A thread in the app, and no other threads of the member's, unless a test gives them.
isSlackThread: mock<HarnessStore['isSlackThread']>(async () => Ok(false)),
listRecentThreads: mock<HarnessStore['listRecentThreads']>(async () => Ok([])),
// No time zone set, unless a test gives one.
getUserTimeZone: mock<HarnessStore['getUserTimeZone']>(async () => Ok(null)),
deleteAlert: mock<HarnessStore['deleteAlert']>(),
Expand Down
Loading
Loading