Skip to content

Commit f2b5cb9

Browse files
committed
Reuse content types
1 parent 8583cdd commit f2b5cb9

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/agents.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
OutputGuardrailFunctionArgs,
1515
} from '@openai/agents-core';
1616
import { GuardrailLLMContext, GuardrailResult, TextOnlyContent } from './types';
17+
import { TEXT_CONTENT_TYPES } from './utils/content';
1718
import {
1819
loadPipelineBundles,
1920
instantiateGuardrails,
@@ -249,7 +250,7 @@ function ensureGuardrailContext(
249250
} as GuardrailLLMContext;
250251
}
251252

252-
const TEXTUAL_CONTENT_TYPES = new Set(['input_text', 'text', 'output_text', 'summary_text']);
253+
const TEXTUAL_CONTENT_TYPES = new Set<string>(TEXT_CONTENT_TYPES);
253254
const MAX_CONTENT_EXTRACTION_DEPTH = 10;
254255

255256
/**
@@ -400,7 +401,7 @@ function extractTextFromAgentInput(input: unknown): string {
400401
}
401402
}
402403

403-
if (record.content !== undefined) {
404+
if (record.content != null) {
404405
const contentText = extractTextFromContentParts(record.content);
405406
if (contentText) {
406407
return contentText;
@@ -412,7 +413,15 @@ function extractTextFromAgentInput(input: unknown): string {
412413
}
413414
}
414415

415-
return String(input);
416+
if (
417+
typeof input === 'number' ||
418+
typeof input === 'boolean' ||
419+
typeof input === 'bigint'
420+
) {
421+
return String(input);
422+
}
423+
424+
return '';
416425
}
417426

418427
function extractLatestUserText(history: NormalizedConversationEntry[]): string {

src/utils/content.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
import { Message, ContentPart, TextContentPart, TextOnlyMessageArray } from '../types';
99

10+
export const TEXT_CONTENT_TYPES = ['input_text', 'text', 'output_text', 'summary_text'] as const;
11+
const TEXT_CONTENT_TYPES_SET = new Set<string>(TEXT_CONTENT_TYPES);
12+
1013
export class ContentUtils {
11-
// Clear: what types are considered text
12-
private static readonly TEXT_TYPES = ['input_text', 'text', 'output_text', 'summary_text'] as const;
13-
1414
/**
1515
* Check if a content part is text-based.
1616
*/
1717
static isText(part: ContentPart): boolean {
18-
return this.TEXT_TYPES.includes(part.type as typeof this.TEXT_TYPES[number]);
18+
return typeof part.type === 'string' && TEXT_CONTENT_TYPES_SET.has(part.type);
1919
}
2020

2121
/**

0 commit comments

Comments
 (0)