Files
supabase/apps/studio/components/ui/AIAssistantPanel/Message.utils.ts
T
d52e2c252b Update AI SDK 5 (#37656)
* attempt to update packages and useChat

* update endpoints

* update zod

* zod

* update to v5

* message update

* Revert "zod"

This reverts commit ec39bac6b6.

* revert zod

* zod i

* fix complete endpoints

* remove async

* change to content

* type cleanup

* Revert the package bumps to rebuild them.

* Bump zod to 2.25.76 in all packages.

* Bump openai in all packages.

* Bump ai and ai-related packages.

* Remove unneeded files.

* Fix the rest of the migration stuff.

* Prettier fixes.

* zod

* v4 again

---------

Co-authored-by: Jordi Enric <jordi.err@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-08-08 14:15:39 +07:00

75 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const extractDataFromSafetyMessage = (text: string): string | null => {
const openingTags = [...text.matchAll(/<untrusted-data-[a-z0-9-]+>/gi)]
if (openingTags.length < 2) return null
const closingTagMatch = text.match(/<\/untrusted-data-[a-z0-9-]+>/i)
if (!closingTagMatch) return null
const secondOpeningEnd = openingTags[1].index! + openingTags[1][0].length
const closingStart = text.indexOf(closingTagMatch[0])
const content = text.substring(secondOpeningEnd, closingStart)
return content.replace(/\\n/g, '').replace(/\\"/g, '"').replace(/\n/g, '').trim()
}
// Helper function to find result data directly from parts array
export const findResultForManualId = (
parts: any[] | undefined,
manualId: string
): any[] | undefined => {
if (!parts) return undefined
const invocationPart = parts.find(
(part) =>
part.type === 'tool-invocation' &&
'toolInvocation' in part &&
part.toolInvocation.state === 'result' &&
'result' in part.toolInvocation &&
part.toolInvocation.result?.manualToolCallId === manualId
)
if (
invocationPart &&
'toolInvocation' in invocationPart &&
'result' in invocationPart.toolInvocation &&
invocationPart.toolInvocation.result?.content?.[0]?.text
) {
try {
const rawText = invocationPart.toolInvocation.result.content[0].text
const extractedData = extractDataFromSafetyMessage(rawText) || rawText
let parsedData = JSON.parse(extractedData.trim())
return Array.isArray(parsedData) ? parsedData : undefined
} catch (error) {
console.error('Failed to parse tool invocation result data for manualId:', manualId, error)
return undefined
}
}
return undefined
}
// [Joshen] From https://github.com/remarkjs/react-markdown/blob/fda7fa560bec901a6103e195f9b1979dab543b17/lib/index.js#L425
export function defaultUrlTransform(value: string) {
const safeProtocol = /^(https?|ircs?|mailto|xmpp)$/i
const colon = value.indexOf(':')
const questionMark = value.indexOf('?')
const numberSign = value.indexOf('#')
const slash = value.indexOf('/')
if (
// If there is no protocol, it’s relative.
colon === -1 ||
// If the first colon is after a `?`, `#`, or `/`, it’s not a protocol.
(slash !== -1 && colon > slash) ||
(questionMark !== -1 && colon > questionMark) ||
(numberSign !== -1 && colon > numberSign) ||
// It is a protocol, it should be allowed.
safeProtocol.test(value.slice(0, colon))
) {
return value
}
return ''
}