-
Notifications
You must be signed in to change notification settings - Fork 8
Agent parse conversation history #36
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
Changes from all commits
154d545
612a5c4
f684ff0
793b39c
8583cdd
f2b5cb9
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,8 +13,8 @@ import type { | |||||||||
| InputGuardrailFunctionArgs, | ||||||||||
| OutputGuardrailFunctionArgs, | ||||||||||
| } from '@openai/agents-core'; | ||||||||||
| import { GuardrailLLMContext, GuardrailResult, TextOnlyContent, ContentPart } from './types'; | ||||||||||
| import { ContentUtils } from './utils/content'; | ||||||||||
| import { GuardrailLLMContext, GuardrailResult, TextOnlyContent } from './types'; | ||||||||||
| import { TEXT_CONTENT_TYPES } from './utils/content'; | ||||||||||
| import { | ||||||||||
| loadPipelineBundles, | ||||||||||
| instantiateGuardrails, | ||||||||||
|
|
@@ -250,6 +250,180 @@ function ensureGuardrailContext( | |||||||||
| } as GuardrailLLMContext; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const TEXTUAL_CONTENT_TYPES = new Set<string>(TEXT_CONTENT_TYPES); | ||||||||||
| const MAX_CONTENT_EXTRACTION_DEPTH = 10; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Extract text from any nested content value with optional type filtering. | ||||||||||
| * | ||||||||||
| * @param value Arbitrary content value (string, array, or object) to inspect. | ||||||||||
| * @param depth Current recursion depth, used to guard against circular structures. | ||||||||||
| * @param filterByType When true, only content parts with recognized text types are returned. | ||||||||||
| * @returns The extracted text, or an empty string when no text is found. | ||||||||||
| */ | ||||||||||
| function extractTextFromValue(value: unknown, depth: number, filterByType: boolean): string { | ||||||||||
| if (depth > MAX_CONTENT_EXTRACTION_DEPTH) { | ||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof value === 'string') { | ||||||||||
| return value.trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (Array.isArray(value)) { | ||||||||||
| const parts: string[] = []; | ||||||||||
| for (const item of value) { | ||||||||||
| const text = extractTextFromValue(item, depth + 1, filterByType); | ||||||||||
| if (text) { | ||||||||||
| parts.push(text); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return parts.join(' ').trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (value && typeof value === 'object') { | ||||||||||
| const record = value as Record<string, unknown>; | ||||||||||
| const typeValue = typeof record.type === 'string' ? record.type : null; | ||||||||||
| const isRecognizedTextType = typeValue ? TEXTUAL_CONTENT_TYPES.has(typeValue) : false; | ||||||||||
|
|
||||||||||
| if (typeof record.text === 'string') { | ||||||||||
| if (!filterByType || isRecognizedTextType || typeValue === null) { | ||||||||||
| return record.text.trim(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const contentValue = record.content; | ||||||||||
| // If a direct text field was skipped due to type filtering, fall back to nested content. | ||||||||||
| if (contentValue != null) { | ||||||||||
| const nested = extractTextFromValue(contentValue, depth + 1, filterByType); | ||||||||||
| if (nested) { | ||||||||||
| return nested; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Extract text from structured content parts (e.g., the `content` field on a message). | ||||||||||
| * | ||||||||||
| * Only textual content-part types enumerated in TEXTUAL_CONTENT_TYPES are considered so | ||||||||||
| * that non-text modalities (images, tools, etc.) remain ignored. | ||||||||||
| */ | ||||||||||
| function extractTextFromContentParts(content: unknown, depth = 0): string { | ||||||||||
| return extractTextFromValue(content, depth, true); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Extract text from a single message entry. | ||||||||||
| * | ||||||||||
| * Handles strings, arrays of content parts, or message-like objects that contain a | ||||||||||
| * `content` collection or a plain `text` field. | ||||||||||
| */ | ||||||||||
| function extractTextFromMessageEntry(entry: unknown, depth = 0): string { | ||||||||||
| if (depth > MAX_CONTENT_EXTRACTION_DEPTH) { | ||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (entry == null) { | ||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof entry === 'string') { | ||||||||||
| return entry.trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (Array.isArray(entry)) { | ||||||||||
| return extractTextFromContentParts(entry, depth + 1); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof entry === 'object') { | ||||||||||
| const record = entry as Record<string, unknown>; | ||||||||||
|
|
||||||||||
| if (record.content !== undefined) { | ||||||||||
| const contentText = extractTextFromContentParts(record.content, depth + 1); | ||||||||||
| if (contentText) { | ||||||||||
| return contentText; | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof record.text === 'string') { | ||||||||||
| return record.text.trim(); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
||||||||||
| // Last-resort extraction: if entry does not match standard message patterns, | |
| // attempt to extract text from any value type. |
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.
[nit]
Uh oh!
There was an error while loading. Please reload this page.