-
-
Notifications
You must be signed in to change notification settings - Fork 160
feat(ai): add clientOutput to tool definitions for server-side result filtering
#398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -162,6 +162,7 @@ export class ToolCallManager { | |||||||||||||||||||||||||||||||||||
| const tool = this.tools.find((t) => t.name === toolCall.function.name) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| let toolResultContent: string | ||||||||||||||||||||||||||||||||||||
| let clientResultContent: string | undefined | ||||||||||||||||||||||||||||||||||||
| if (tool?.execute) { | ||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| // Parse arguments (normalize "null" to "{}" for empty tool_use blocks) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -215,6 +216,13 @@ export class ToolCallManager { | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| toolResultContent = | ||||||||||||||||||||||||||||||||||||
| typeof result === 'string' ? result : JSON.stringify(result) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Apply clientOutput filter if the tool defines one | ||||||||||||||||||||||||||||||||||||
| if (tool.clientOutput) { | ||||||||||||||||||||||||||||||||||||
| const parsed = | ||||||||||||||||||||||||||||||||||||
| typeof result === 'string' ? JSON.parse(result) : result | ||||||||||||||||||||||||||||||||||||
| clientResultContent = JSON.stringify(tool.clientOutput(parsed)) | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+219
to
+225
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential runtime error when If Since 🛡️ Proposed fix to add defensive error handling // Apply clientOutput filter if the tool defines one
if (tool.clientOutput) {
- const parsed =
- typeof result === 'string' ? JSON.parse(result) : result
- clientResultContent = JSON.stringify(tool.clientOutput(parsed))
+ try {
+ const parsed =
+ typeof result === 'string' ? JSON.parse(result) : result
+ clientResultContent = JSON.stringify(tool.clientOutput(parsed))
+ } catch {
+ // Fall back to unfiltered content if clientOutput fails
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
| } catch (error: unknown) { | ||||||||||||||||||||||||||||||||||||
| // If tool execution fails, add error message | ||||||||||||||||||||||||||||||||||||
| const message = | ||||||||||||||||||||||||||||||||||||
|
|
@@ -233,7 +241,7 @@ export class ToolCallManager { | |||||||||||||||||||||||||||||||||||
| toolName: toolCall.function.name, | ||||||||||||||||||||||||||||||||||||
| model: finishEvent.model, | ||||||||||||||||||||||||||||||||||||
| timestamp: Date.now(), | ||||||||||||||||||||||||||||||||||||
| result: toolResultContent, | ||||||||||||||||||||||||||||||||||||
| result: clientResultContent ?? toolResultContent, | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // Add tool result message | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| /** | ||
| * Type-level tests for clientOutput on tool definitions. | ||
| * | ||
| * These tests verify that TypeScript correctly infers the clientOutput | ||
| * parameter type from the outputSchema, and that the property propagates | ||
| * through .server() and .client() builders. | ||
| */ | ||
|
|
||
| import { describe, expectTypeOf, it } from 'vitest' | ||
| import { z } from 'zod' | ||
| import { toolDefinition } from '../src/activities/chat/tools/tool-definition' | ||
|
|
||
| describe('clientOutput type inference', () => { | ||
| const outputSchema = z.object({ | ||
| id: z.string(), | ||
| name: z.string(), | ||
| ssn: z.string(), | ||
| internalScore: z.number(), | ||
| }) | ||
|
|
||
| type ExpectedOutput = z.infer<typeof outputSchema> | ||
|
|
||
| it('should type the clientOutput parameter from outputSchema', () => { | ||
| toolDefinition({ | ||
| name: 'lookup_user', | ||
| description: 'Look up a user', | ||
| outputSchema, | ||
| clientOutput: (result) => { | ||
| // result should be typed as the inferred output schema type | ||
| expectTypeOf(result).toEqualTypeOf<ExpectedOutput>() | ||
| return { id: result.id } | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('should allow clientOutput to return any shape', () => { | ||
| // clientOutput return type is unknown — no constraints on what shape the | ||
| // filtered result has. This is intentional: the client result doesn't need | ||
| // to conform to the outputSchema. | ||
| toolDefinition({ | ||
| name: 'flexible_return', | ||
| description: 'Return anything', | ||
| outputSchema, | ||
| clientOutput: (result) => { | ||
| expectTypeOf(result).toEqualTypeOf<ExpectedOutput>() | ||
| return { justId: result.id, extra: 42 } | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('should reject accessing nonexistent properties in clientOutput', () => { | ||
| toolDefinition({ | ||
| name: 'bad_access', | ||
| description: 'Bad property access', | ||
| outputSchema, | ||
| clientOutput: (result) => ({ | ||
| id: result.id, | ||
| // @ts-expect-error - nonExistent does not exist on output schema | ||
| bad: result.nonExistent, | ||
| }), | ||
| }) | ||
| }) | ||
|
|
||
| it('should propagate clientOutput through .server()', () => { | ||
| const tool = toolDefinition({ | ||
| name: 'server_propagation', | ||
| description: 'Check server propagation', | ||
| outputSchema, | ||
| clientOutput: (result) => ({ id: result.id }), | ||
| }) | ||
|
|
||
| const serverTool = tool.server(async () => ({ | ||
| id: '1', | ||
| name: 'Alice', | ||
| ssn: '000', | ||
| internalScore: 99, | ||
| })) | ||
|
|
||
| // ServerTool extends Tool which uses (result: any) => any for clientOutput. | ||
| // Verify it exists and is callable. | ||
| expectTypeOf(serverTool).toHaveProperty('clientOutput') | ||
| if (serverTool.clientOutput) { | ||
| expectTypeOf(serverTool.clientOutput).toBeFunction() | ||
| } | ||
| }) | ||
|
|
||
| it('should propagate clientOutput through .client()', () => { | ||
| const tool = toolDefinition({ | ||
| name: 'client_propagation', | ||
| description: 'Check client propagation', | ||
| outputSchema, | ||
| clientOutput: (result) => ({ id: result.id }), | ||
| }) | ||
|
|
||
| const clientTool = tool.client() | ||
|
|
||
| // ClientTool preserves the strongly-typed clientOutput | ||
| expectTypeOf(clientTool).toHaveProperty('clientOutput') | ||
| if (clientTool.clientOutput) { | ||
| // The parameter is typed from outputSchema | ||
| type Param = Parameters<typeof clientTool.clientOutput>[0] | ||
| expectTypeOf<Param>().toEqualTypeOf<ExpectedOutput>() | ||
| } | ||
| }) | ||
|
|
||
| it('should type clientOutput parameter as any when no outputSchema', () => { | ||
| // Without outputSchema, SchemaInput defaults to StandardJSONSchemaV1<any, any> | JSONSchema, | ||
| // so InferSchemaType resolves to `any` — matching the convention of execute's args type. | ||
| toolDefinition({ | ||
| name: 'no_schema', | ||
| description: 'No output schema', | ||
| clientOutput: (result) => { | ||
| expectTypeOf(result).toBeAny() | ||
| return result | ||
| }, | ||
| }) | ||
| }) | ||
|
|
||
| it('should allow omitting clientOutput entirely', () => { | ||
| const tool = toolDefinition({ | ||
| name: 'no_filter', | ||
| description: 'No client filtering', | ||
| outputSchema, | ||
| }) | ||
|
|
||
| // clientOutput should be optional (undefined at runtime) | ||
| expectTypeOf(tool).toHaveProperty('clientOutput') | ||
| expectTypeOf(tool.clientOutput).toBeNullable() | ||
| }) | ||
| }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap
clientOutputinvocation in try-catch to prevent stream breakage.If
clientOutputthrows (e.g., due to unexpected result structure orJSON.stringifyfailing on circular references), the exception will propagate and break the entire stream. Consider catching exceptions and falling back tofullContent.🛡️ Proposed fix to add error handling
const fullContent = JSON.stringify(result.result) // Apply clientOutput filter if the tool defines one const tool = this.tools.find((t) => t.name === result.toolName) - const clientContent = - tool?.clientOutput && result.state !== 'output-error' - ? JSON.stringify(tool.clientOutput(result.result)) - : fullContent + let clientContent = fullContent + if (tool?.clientOutput && result.state !== 'output-error') { + try { + clientContent = JSON.stringify(tool.clientOutput(result.result)) + } catch { + // Fall back to full content if clientOutput fails + } + }📝 Committable suggestion
🤖 Prompt for AI Agents