-
Notifications
You must be signed in to change notification settings - Fork 9
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 1 commit
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,7 @@ 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 { | ||||||||||
| loadPipelineBundles, | ||||||||||
| instantiateGuardrails, | ||||||||||
|
|
@@ -250,6 +249,122 @@ function ensureGuardrailContext( | |||||||||
| } as GuardrailLLMContext; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function extractTextFromContentParts(content: unknown): string { | ||||||||||
| if (typeof content === 'string') { | ||||||||||
| return content.trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (Array.isArray(content)) { | ||||||||||
| const parts: string[] = []; | ||||||||||
| for (const item of content) { | ||||||||||
| const text = extractTextFromMessageEntry(item); | ||||||||||
| if (text) { | ||||||||||
| parts.push(text); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| return parts.join(' ').trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (content && typeof content === 'object') { | ||||||||||
| const record = content as Record<string, unknown>; | ||||||||||
| if (typeof record.text === 'string') { | ||||||||||
steven10a marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
| return record.text.trim(); | ||||||||||
| } | ||||||||||
| if (record.content !== undefined) { | ||||||||||
| const nested = extractTextFromContentParts(record.content); | ||||||||||
| if (nested) { | ||||||||||
| return nested; | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| return ''; | ||||||||||
| } | ||||||||||
steven10a marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| function extractTextFromMessageEntry(entry: unknown): string { | ||||||||||
| if (entry == null) { | ||||||||||
| return ''; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof entry === 'string') { | ||||||||||
| return entry.trim(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (Array.isArray(entry)) { | ||||||||||
| return extractTextFromContentParts(entry); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (typeof entry === 'object') { | ||||||||||
| const record = entry as Record<string, unknown>; | ||||||||||
| if (record.content !== undefined) { | ||||||||||
| const contentText = extractTextFromContentParts(record.content); | ||||||||||
| 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.