on performSmartRule(theRecords)
tell application id "DNtp"
repeat with theRecord in theRecords
-- Retrieve custom metadata attached to the record
set theMetaData to custom meta data of theRecord
-- Prevent null reference exceptions if no metadata exists
if theMetaData is not missing value then
try
-- Read the property populated by the previous Smart Rule action
set theSummary to mdchatresponse of theMetaData
-- Validate the summary text before creating a new record
if theSummary is not missing value and theSummary is not "" then
set originalName to name of theRecord as string
-- Opcional: puedes quitar & ".md" si prefieres que no se vea la extensión en el título
set theName to originalName & " (Resumen).md"
-- Extract the DEVONthink item link and format it as a Markdown hyperlink
set theRefURL to reference URL of theRecord
set markdownLink to return & return & "---" & return & "**Documento original:** [" & originalName & "](" & theRefURL & ")"
-- Append the hyperlink to the LLM response
set finalContent to theSummary & markdownLink
-- Instantiate the new markdown record using 'plain text' for raw Markdown source
create record with {name:theName, type:markdown, plain text:finalContent} in (parent 1 of theRecord)
end if
on error
log message "Key 'mdchatresponse' not found in metadata for: " & (name of theRecord as string)
end try
else
log message "No custom metadata found for: " & (name of theRecord as string)
end if
end repeat
end tell
end performSmartRule
This is actually not necessary, just use the actions Set Script Input and Script with Input/Output instead of custom metadata and the Apply Script action. See for example the default Append Chat response to annotation batch processing configuration.