diff --git a/core/src/common.ts b/core/src/common.ts index 5a86d491a7..7ac4cd4629 100644 --- a/core/src/common.ts +++ b/core/src/common.ts @@ -361,6 +361,7 @@ export { isIntentMismatchError, } from './tools/tool_confirmation.js'; export type {IntentMismatchReason} from './tools/tool_confirmation.js'; +export {CallbackContext, ToolContext} from './tools/tool_context.js'; export {URL_CONTEXT, UrlContextTool} from './tools/url_context_tool.js'; export {VertexAiSearchTool} from './tools/vertex_ai_search_tool.js'; export type { diff --git a/core/src/tools/tool_context.ts b/core/src/tools/tool_context.ts new file mode 100644 index 0000000000..4e14da8af4 --- /dev/null +++ b/core/src/tools/tool_context.ts @@ -0,0 +1,24 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * The tool-facing names for the unified agent context. + * + * `ToolContext` and `CallbackContext` are aliases of {@link Context}, not + * subclasses, so a context the framework builds satisfies either name. This + * mirrors `adk-python`'s `google.adk.tools.tool_context`, which binds + * `ToolContext = Context`. + * + * Python also re-exports `AuthHandler`, `AuthConfig` and `AuthCredential` from + * this module. adk-js does not: the package `exports` map has no entry for this + * path, so no consumer can import them from here, and all three already ship + * from the package root. + */ + +export { + Context as CallbackContext, + Context as ToolContext, +} from '../agents/context.js'; diff --git a/core/test/tools/tool_context_test.ts b/core/test/tools/tool_context_test.ts new file mode 100644 index 0000000000..e11f9f6e59 --- /dev/null +++ b/core/test/tools/tool_context_test.ts @@ -0,0 +1,109 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { + CallbackContext, + Context, + createEventActions, + createSession, + FunctionTool, + InvocationContext, + PluginManager, + State, + ToolContext, +} from '@google/adk'; +import {describe, expect, it} from 'vitest'; +import {z} from 'zod/v4'; +import * as toolContextModule from '../../src/tools/tool_context.js'; + +function newInvocationContext() { + return new InvocationContext({ + invocationId: 'test-invocation', + session: createSession({ + id: 'test-session', + appName: 'tool-context-test', + userId: 'test-user', + }), + pluginManager: new PluginManager([]), + }); +} + +function takesToolContext(context: ToolContext): string | undefined { + return context.functionCallId; +} + +describe('tool_context', () => { + it('binds ToolContext to the Context class itself', () => { + expect(ToolContext).toBe(Context); + }); + + it('binds CallbackContext to the same class as ToolContext', () => { + expect(CallbackContext).toBe(Context); + expect(ToolContext).toBe(CallbackContext); + }); + + it('builds a working context through the ToolContext name', () => { + const eventActions = createEventActions(); + const context = new ToolContext({ + invocationContext: newInvocationContext(), + eventActions, + functionCallId: 'fc-1', + }); + + expect(context).toBeInstanceOf(Context); + expect(context.actions).toBe(eventActions); + expect(context.state).toBeInstanceOf(State); + + context.state.set('greeting', 'hello'); + + expect(context.state.get('greeting')).toBe('hello'); + expect(eventActions.stateDelta).toEqual({greeting: 'hello'}); + }); + + it('accepts a framework-built Context where a ToolContext is required', () => { + const context = new Context({ + invocationContext: newInvocationContext(), + functionCallId: 'fc-2', + }); + + expect(takesToolContext(context)).toBe('fc-2'); + }); + + it('exports exactly the ported names', () => { + expect(Object.keys(toolContextModule).sort()).toEqual([ + 'CallbackContext', + 'ToolContext', + ]); + }); + + it('runs a tool whose execute declares a ToolContext parameter', async () => { + const rememberTool = new FunctionTool({ + name: 'remember', + description: 'Stores a value in session state.', + parameters: z.object({key: z.string(), value: z.string()}), + execute: async (input, toolContext?: ToolContext) => { + toolContext?.state.set(input.key, input.value); + return `stored ${input.key}`; + }, + }); + + const eventActions = createEventActions(); + const context = new ToolContext({ + invocationContext: newInvocationContext(), + eventActions, + functionCallId: 'fc-3', + }); + + const result = await rememberTool.runAsync({ + args: {key: 'city', value: 'Paris'}, + toolContext: context, + }); + + expect(result).toBe('stored city'); + expect(context.state.get('city')).toBe('Paris'); + expect(eventActions.stateDelta).toEqual({city: 'Paris'}); + }); +});