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
3 changes: 2 additions & 1 deletion js/ai/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function defineResource(
): ResourceAction {
const action = dynamicResource(opts, fn);
action.matches = createMatcher(opts.uri, opts.template);
action.__action.metadata.dynamic = false;
registry.registerAction('resource', action);
return action;
}
Expand Down Expand Up @@ -193,7 +194,7 @@ export async function findMatchingResource(

/** Checks whether provided object is a dynamic resource. */
export function isDynamicResourceAction(t: unknown): t is ResourceAction {
return isAction(t) && !t.__registry;
return isAction(t) && t.__action?.metadata?.dynamic === true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/ai/src/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export function isToolResponse(part: Part): part is ToolResponsePart {
}

export function isDynamicTool(t: unknown): t is ToolAction {
return isAction(t) && !t.__registry;
return isAction(t) && t.__action?.metadata?.dynamic === true;
}

export function interrupt<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(
Expand Down
16 changes: 14 additions & 2 deletions js/ai/tests/resource/resource_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
dynamicResource,
findMatchingResource,
isDynamicResourceAction,
resource,
} from '../../src/resource.js';
import { defineEchoModel } from '../helpers.js';

Expand Down Expand Up @@ -58,7 +59,7 @@ describe('resource', () => {
uri: 'foo://bar',
},
type: 'resource',
dynamic: true,
dynamic: false,
});

assert.strictEqual(testResource.matches({ uri: 'foo://bar' }), true);
Expand Down Expand Up @@ -229,7 +230,7 @@ describe('resource', () => {
uri: undefined,
},
type: 'resource',
dynamic: true,
dynamic: false,
});

const gotBaz = await findMatchingResource(registry, resList, {
Expand Down Expand Up @@ -286,4 +287,15 @@ describe('isDynamicResourceAction', () => {
true
);
});

it('should remain dynamic after registration', () => {
const dynamic = resource({ uri: 'bar://baz' }, () => ({
content: [{ text: `bar` }],
}));
assert.strictEqual(isDynamicResourceAction(dynamic), true);

registry.registerAction('resource', dynamic);

assert.strictEqual(isDynamicResourceAction(dynamic), true);
});
});
55 changes: 53 additions & 2 deletions js/ai/tests/tool_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
* limitations under the License.
*/

import { z } from '@genkit-ai/core';
import { action, z } from '@genkit-ai/core';
import { initNodeFeatures } from '@genkit-ai/core/node';
import { Registry } from '@genkit-ai/core/registry';
import * as assert from 'assert';
import { afterEach, describe, it } from 'node:test';
import { defineInterrupt, defineTool } from '../src/tool.js';
import {
defineInterrupt,
defineTool,
isDynamicTool,
tool,
} from '../src/tool.js';

initNodeFeatures();

Expand Down Expand Up @@ -109,6 +114,52 @@ describe('defineInterrupt', () => {
});
});

describe('isDynamicTool', () => {
let registry = new Registry();
registry.apiStability = 'beta';
afterEach(() => {
registry = new Registry();
registry.apiStability = 'beta';
});

it('should return true for a dynamic tool', () => {
const dynamic = tool({ name: 'dynamic', description: 'test' });
assert.strictEqual(isDynamicTool(dynamic), true);
});

it('should remain dynamic after registration', () => {
const dynamic = tool({ name: 'dynamic', description: 'test' });
assert.strictEqual(isDynamicTool(dynamic), true);

registry.registerAction('tool', dynamic);

assert.strictEqual(isDynamicTool(dynamic), true);
});

it('should return false for a registered tool', () => {
const regular = defineTool(
registry,
{ name: 'regular', description: 'test' },
async () => {}
);
assert.strictEqual(isDynamicTool(regular), false);
});

it('should return false for a non-tool action', () => {
const regularAction = action(
{ actionType: 'util', name: 'regularAction', description: 'test' },
async () => {}
);
assert.strictEqual(isDynamicTool(regularAction), false);
});

it('should return false for a non-action', () => {
assert.strictEqual(isDynamicTool({}), false);
assert.strictEqual(isDynamicTool('tool'), false);
assert.strictEqual(isDynamicTool(123), false);
});
});

describe('defineTool', () => {
let registry = new Registry();
registry.apiStability = 'beta';
Expand Down