|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +import { describe, it, expect } from 'vitest'; |
| 3 | +import { clientToolSpecs, clientToolNames } from './langgraph/middleware'; |
| 4 | + |
| 5 | +const WEATHER = { name: 'get_weather', description: 'Weather', parameters: { type: 'object' } }; |
| 6 | + |
| 7 | +describe('clientToolSpecs', () => { |
| 8 | + it('wraps each catalog entry as an OpenAI function tool', () => { |
| 9 | + expect(clientToolSpecs({ messages: [], tools: [WEATHER] })).toEqual([ |
| 10 | + { type: 'function', function: { name: 'get_weather', description: 'Weather', parameters: { type: 'object' } } }, |
| 11 | + ]); |
| 12 | + }); |
| 13 | + it('falls back to client_tools when tools is absent', () => { |
| 14 | + expect(clientToolSpecs({ messages: [], client_tools: [WEATHER] })).toHaveLength(1); |
| 15 | + }); |
| 16 | + it('defaults missing description/parameters and drops nameless entries', () => { |
| 17 | + const specs = clientToolSpecs({ messages: [], tools: [{ name: 'x' } as never, { description: 'no name' } as never] }); |
| 18 | + expect(specs).toEqual([{ type: 'function', function: { name: 'x', description: '', parameters: {} } }]); |
| 19 | + }); |
| 20 | + it('returns [] for empty state', () => { |
| 21 | + expect(clientToolSpecs({ messages: [] })).toEqual([]); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +describe('clientToolNames', () => { |
| 26 | + it('returns the set of catalog names', () => { |
| 27 | + expect(clientToolNames({ messages: [], tools: [WEATHER] })).toEqual(new Set(['get_weather'])); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +import { lastMessage, hasClientToolCall, hasServerToolCall } from './langgraph/middleware'; |
| 32 | +import { AIMessage, HumanMessage } from '@langchain/core/messages'; |
| 33 | + |
| 34 | +const stateWith = (toolCalls: { name: string }[]) => ({ |
| 35 | + messages: [new HumanMessage('hi'), new AIMessage({ content: '', tool_calls: toolCalls.map((c) => ({ name: c.name, args: {}, id: c.name })) })], |
| 36 | + tools: [{ name: 'get_weather', description: '', parameters: {} }], |
| 37 | +}); |
| 38 | + |
| 39 | +describe('lastMessage', () => { |
| 40 | + it('returns the last message or undefined', () => { |
| 41 | + expect(lastMessage({ messages: [] })).toBeUndefined(); |
| 42 | + expect(lastMessage({ messages: [new HumanMessage('a'), new HumanMessage('b')] })?.content).toBe('b'); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('hasClientToolCall', () => { |
| 47 | + it('true when the last AI message calls a client tool', () => { |
| 48 | + expect(hasClientToolCall(stateWith([{ name: 'get_weather' }]))).toBe(true); |
| 49 | + }); |
| 50 | + it('false when the last AI message calls only non-client tools', () => { |
| 51 | + expect(hasClientToolCall(stateWith([{ name: 'search' }]))).toBe(false); |
| 52 | + }); |
| 53 | + it('false when there are no tool calls', () => { |
| 54 | + expect(hasClientToolCall(stateWith([]))).toBe(false); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('hasServerToolCall', () => { |
| 59 | + it('true when a call name is in serverToolNames', () => { |
| 60 | + expect(hasServerToolCall(stateWith([{ name: 'search' }]), ['search'])).toBe(true); |
| 61 | + }); |
| 62 | + it('true when a call name is unknown (not a client tool)', () => { |
| 63 | + expect(hasServerToolCall(stateWith([{ name: 'mystery' }]), [])).toBe(true); |
| 64 | + }); |
| 65 | + it('false when the only call is a known client tool', () => { |
| 66 | + expect(hasServerToolCall(stateWith([{ name: 'get_weather' }]), [])).toBe(false); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +import { bindClientTools, routeAfterAgent } from './langgraph/middleware'; |
| 71 | +import { clientToolsChannel } from './langgraph/channel'; |
| 72 | +import { Annotation, MessagesAnnotation } from '@langchain/langgraph'; |
| 73 | + |
| 74 | +describe('clientToolsChannel', () => { |
| 75 | + it('produces tools + client_tools channels usable in Annotation.Root', () => { |
| 76 | + const frag = clientToolsChannel(); |
| 77 | + expect(Object.keys(frag).sort()).toEqual(['client_tools', 'tools']); |
| 78 | + const State = Annotation.Root({ ...MessagesAnnotation.spec, ...frag }); |
| 79 | + expect(State.spec).toHaveProperty('tools'); |
| 80 | + expect(State.spec).toHaveProperty('client_tools'); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('bindClientTools', () => { |
| 85 | + it('binds server tools then client stubs (server first), calling bindTools once', () => { |
| 86 | + const calls: unknown[][] = []; |
| 87 | + const fake = { bindTools: (tools: unknown[]) => { calls.push(tools); return 'BOUND'; } }; |
| 88 | + const SERVER = { name: 'search' }; |
| 89 | + const result = bindClientTools(fake as never, [SERVER as never], { messages: [], tools: [{ name: 'get_weather', description: '', parameters: {} }] }); |
| 90 | + expect(result).toBe('BOUND'); |
| 91 | + expect(calls).toHaveLength(1); |
| 92 | + expect(calls[0][0]).toBe(SERVER); |
| 93 | + expect((calls[0][1] as { function: { name: string } }).function.name).toBe('get_weather'); |
| 94 | + }); |
| 95 | + it('binds only server tools when there is no client catalog', () => { |
| 96 | + let bound: unknown[] = []; |
| 97 | + const fake = { bindTools: (tools: unknown[]) => { bound = tools; return fake; } }; |
| 98 | + bindClientTools(fake as never, [{ name: 'search' } as never], { messages: [] }); |
| 99 | + expect(bound).toHaveLength(1); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('routeAfterAgent', () => { |
| 104 | + const st = (names: string[]) => ({ |
| 105 | + messages: [new AIMessage({ content: '', tool_calls: names.map((n) => ({ name: n, args: {}, id: n })) })], |
| 106 | + tools: [{ name: 'get_weather', description: '', parameters: {} }], |
| 107 | + }); |
| 108 | + it('routes a server tool call to the tools node', () => { |
| 109 | + expect(routeAfterAgent(st(['search']), ['search'])).toBe('tools'); |
| 110 | + }); |
| 111 | + it('routes a client-only tool call to END', () => { |
| 112 | + expect(routeAfterAgent(st(['get_weather']), [])).toBe('__end__'); |
| 113 | + }); |
| 114 | + it('routes no tool calls to END', () => { |
| 115 | + expect(routeAfterAgent(st([]), [])).toBe('__end__'); |
| 116 | + }); |
| 117 | + it('routes a mixed call to the server (precedence)', () => { |
| 118 | + expect(routeAfterAgent(st(['get_weather', 'search']), ['search'])).toBe('tools'); |
| 119 | + }); |
| 120 | + it('honors custom node names', () => { |
| 121 | + expect(routeAfterAgent(st(['search']), ['search'], { toolsNode: 'act' })).toBe('act'); |
| 122 | + expect(routeAfterAgent(st([]), [], { end: 'DONE' })).toBe('DONE'); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +import { clientToolsRouter } from './langgraph/router'; |
| 127 | + |
| 128 | +describe('clientToolsRouter', () => { |
| 129 | + const st = (names: string[]) => ({ |
| 130 | + messages: [new AIMessage({ content: '', tool_calls: names.map((n) => ({ name: n, args: {}, id: n })) })], |
| 131 | + tools: [{ name: 'get_weather', description: '', parameters: {} }], |
| 132 | + }); |
| 133 | + it('returns a callback that routes via routeAfterAgent with bound serverToolNames', () => { |
| 134 | + const route = clientToolsRouter(['search']); |
| 135 | + expect(route(st(['search']))).toBe('tools'); |
| 136 | + expect(route(st(['get_weather']))).toBe('__end__'); |
| 137 | + }); |
| 138 | + it('honors custom node names', () => { |
| 139 | + const route = clientToolsRouter([], { end: 'DONE' }); |
| 140 | + expect(route(st([]))).toBe('DONE'); |
| 141 | + }); |
| 142 | +}); |
0 commit comments