|
| 1 | +# Data migration: sync the seeded "Document Description Updater" template's |
| 2 | +# task instructions and description to the current values. |
| 3 | +# |
| 4 | +# The template is seeded once (agents/0010) via the idempotent |
| 5 | +# ``create_default_action_templates`` helper, which SKIPS any template that |
| 6 | +# already exists by name. So edits to ``DOCUMENT_DESCRIPTION_INSTRUCTIONS`` or |
| 7 | +# the template's ``description`` in ``corpuses/template_seeds.py`` never reach |
| 8 | +# already-seeded databases on their own — they must be propagated here. |
| 9 | +# Mirrors agents/0016 (CAML writer prompt). |
| 10 | +# |
| 11 | +# This run rewrites the prompt to lead with the document's subject matter |
| 12 | +# (goods, services, rights, obligations) instead of restating its type/title, |
| 13 | +# and to drop the "This document is …" lead-in. The UI-visible description is |
| 14 | +# updated to match the new behaviour. |
| 15 | +# |
| 16 | +# Strings are inlined here (not imported from template_seeds) so this migration |
| 17 | +# remains a self-contained frozen snapshot: a future rename or move of the |
| 18 | +# source constant cannot break ``migrate`` from a clean database. |
| 19 | + |
| 20 | +from django.db import migrations |
| 21 | + |
| 22 | +_TASK_INSTRUCTIONS = ( |
| 23 | + "Write ONE concise sentence (about 12-25 words) capturing what this " |
| 24 | + "document is actually about: the specific subject matter -- the goods, " |
| 25 | + "services, rights, obligations, or events it concerns -- and the key " |
| 26 | + "parties involved. Lead with the substance, not the document type or title " |
| 27 | + '(e.g. "Renewal of an agreement with Acme Foods to supply potatoes to city ' |
| 28 | + 'facilities", not "A renewal agreement titled ..."). Do not begin with ' |
| 29 | + '"This document is" or "This is", and do not simply restate the title. ' |
| 30 | + "Mention the document or instrument type only as brief context, not the " |
| 31 | + "focus. Use update_document_description to save your result; rewrite any " |
| 32 | + "existing description to meet this bar." |
| 33 | +) |
| 34 | + |
| 35 | +_UI_DESCRIPTION = ( |
| 36 | + "Reads a newly added document and writes a one-sentence summary " |
| 37 | + "leading with its subject matter and key parties." |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +def update_document_description_prompt(apps, schema_editor): # pragma: no cover |
| 42 | + """Set the Document Description Updater template's task instructions and description. |
| 43 | +
|
| 44 | + Idempotent: each field is only written when the stored value differs from |
| 45 | + the target, so re-running (or running after a fresh seed that already used |
| 46 | + the new values) is a no-op. |
| 47 | + """ |
| 48 | + CorpusActionTemplate = apps.get_model("corpuses", "CorpusActionTemplate") |
| 49 | + tmpl = CorpusActionTemplate.objects.filter( |
| 50 | + name="Document Description Updater" |
| 51 | + ).first() |
| 52 | + if tmpl is None: |
| 53 | + return |
| 54 | + |
| 55 | + update_fields = [] |
| 56 | + if tmpl.task_instructions != _TASK_INSTRUCTIONS: |
| 57 | + tmpl.task_instructions = _TASK_INSTRUCTIONS |
| 58 | + update_fields.append("task_instructions") |
| 59 | + if tmpl.description != _UI_DESCRIPTION: |
| 60 | + tmpl.description = _UI_DESCRIPTION |
| 61 | + update_fields.append("description") |
| 62 | + if update_fields: |
| 63 | + tmpl.save(update_fields=update_fields) |
| 64 | + |
| 65 | + AgentConfiguration = apps.get_model("agents", "AgentConfiguration") |
| 66 | + agent = AgentConfiguration.objects.filter( |
| 67 | + name="Document Description Updater Agent" |
| 68 | + ).first() |
| 69 | + if agent is not None and agent.description != _UI_DESCRIPTION: |
| 70 | + agent.description = _UI_DESCRIPTION |
| 71 | + agent.save(update_fields=["description"]) |
| 72 | + |
| 73 | + |
| 74 | +class Migration(migrations.Migration): |
| 75 | + |
| 76 | + dependencies = [ |
| 77 | + ("agents", "0016_update_caml_article_writer_prompt"), |
| 78 | + ] |
| 79 | + |
| 80 | + operations = [ |
| 81 | + migrations.RunPython( |
| 82 | + update_document_description_prompt, migrations.RunPython.noop |
| 83 | + ), |
| 84 | + ] |
0 commit comments