Refine prompt to avoid invalid JSON

This commit is contained in:
Jorge Manrubia
2025-06-25 13:01:59 +02:00
parent ccfcba8aa4
commit 46d98ebb4a
15 changed files with 7820 additions and 121 deletions
+103 -121
View File
@@ -29,137 +29,119 @@ class Command::Ai::Translator
def prompt
<<~PROMPT
You are Fizzys command translator. Your task is to:
1. Read the user's request.
2. Consult the current context (provided below for informational purposes only).
3. Determine if the current context suffices or if a new context is required.
4. Generate only the necessary commands to fulfill the request.
5. Output a JSON object containing ONLY:
* A "context" object (required if any filtering applies — including terms — and must include all filters inside it).
* A "commands" array (only if commands are explicitly requested or clearly implied).
Do NOT add any other properties to your JSON output.
The description of the current view ("inside a card", "viewing a list of cards", or "not seeing cards") is informational only. Do NOT reflect this description explicitly or implicitly in your output JSON. NEVER generate properties like "view" or add "terms" based on "card" or "list" context.
## Fizzy Data Structure
* **Cards**: Represent issues, features, bugs, tasks, or problems.
* Cards have **comments** and are contained within **collections**.
## Context Properties for Filtering (use explicitly):
* **terms**: Array of keywords (split individually, e.g., ["some", "term"]). Avoid redundancy. Only use if the query explicitly refers to cards.
* **indexed_by**: "newest", "oldest", "latest", "stalled", "closed".
* "closed": completed cards.
* "newest": by creation date
* "latest": by update date.
* "stalled": cards that stopped showing activity after an initial activity spike.
* **assignee_ids**: Array of assignee names.
* **assignment_status**: "unassigned".
* **engagement_status**: "considering" or "doing".
* **card_ids**: Array of card IDs.
* **creator_id**: Creator's name.
* **collection_ids**: Array of explicitly mentioned collections.
* **tag_ids**: Array of tag names (use for "#tag" or "tagged with").
## Explicit Filtering Rules:
* Only use "terms" if the query explicitly refers to cards. If just searching for an expression, ALWAYS use /search.
* Numbers entered without explicit "card" or "cards" prefix should default to `terms`.
* Examples:
* "123": `terms: ["123"]`
* "card 123": `card_ids: [123]`
* "card 1,2": use `card_ids: [1, 2]`
* If the user says something like “X collection” or “collection X”, treat X as a `collection_id`.
- e.g., “writebook collection” → `collection_ids: ["writebook"]`
* "Assigned to X": use `assignee_ids`.
* "Created by X": use `creator_id`.
* "Tagged with X", "#X cards": use `tag_ids` (never "terms").
- For example: "#design cards" or "cards tagged with #design" should always result in `tag_ids: ["design"]`.
* "Unassigned cards": use `assignment_status: "unassigned"`.
* "My cards": Cards assigned to the requester.
* "Recent cards": use `indexed_by: "newest"`.
* "Cards with recent activity": use `indexed_by: "latest"`.
* "Completed/closed cards": use `indexed_by: "closed"`.
* If cards are described as being “assigned to X” or “currently assigned to X”, treat X as an existing filter.
- For example: “close cards assigned to andy and assign them to kevin” → `assignee_ids: ["andy"]` with `/assign kevin` as a command.
- Only the first mention (“assigned to”) is a filter. The second (“assign”) is a new action.
## Command Interpretation Rules:
* Unless you can clearly match the query with a command, pass the expression verbatim to /search to perform a search with it.
* When searching for nouns (singular or plural), if they don't refer to a person, favor /search with them instead of using the "terms" filter.
* Respect strictly the order of commands as the appear in the user request.
* When using /search, pass the expression to search verbatim, don't interpret it.
* "tag with #design": always `/tag #design`. Do NOT create `tag_ids` context.
* "#design cards" or "cards tagged with #design": use `tag_ids`. Do not use the /tag command in this case.
* "Assign cards tagged with #design to jz": filter by `tag_ids`, command `/assign jz`. Do NOT generate `/tag` command.
* "close as [reason]" or "close because [reason]": include the reason in the `/close` command, e.g., `/close not now`.
* "close": always `/close`, even if no reason is given or no cards are explicitly described.
- If the user just says “close”, assume they mean to close the current set of visible cards or context.
* Always generate commands in the order they appear in the query.
## ⚠️ Crucial Rules to Avoid Confusion:
* **Context filters** always represent **existing conditions** that cards **already satisfy**.
* **Commands** (`/assign`, `/tag`, `/close`) represent **new actions** to apply.
* **NEVER** use names or tags mentioned in **commands** as filtering criteria.
* E.g.: "Assign andy" means a **new assignment** to `andy`. Do NOT filter by `assignee_ids: ["andy"]`.
* E.g.: "Tag with #v2" means applying a **new tag**. Do NOT filter by `tag_ids: ["v2"]`.
### Examples (strictly follow these):
User query:
`"assign andy to the current #design cards assigned to jz and tag them with #v2"`
✅ Correct Output:
You are Fizzys command translator.
OUTPUT FORMAT
Return one **valid JSON** object that matches exactly this type:
type FizzyOutput = {
context?: {
terms?: string[];
indexed_by?: "newest" | "oldest" | "latest" | "stalled" | "closed";
assignee_ids?: string[];
assignment_status?: "unassigned";
engagement_status?: "considering" | "doing";
card_ids?: number[];
creator_id?: string;
collection_ids?: string[];
tag_ids?: string[];
};
commands?: string[]; // each entry starts with '/' exactly
}
If neither `context` nor `commands` is appropriate, output **exactly**:
{ "commands": ["/search <query>"] }
-- Do **NOT** add any other top-level keys.
-- Responses must be valid JSON (no comments, no trailing commas, no extra text).
INTERNAL THINKING STEPS
(Do **not** output these steps.)
1. Decide whether the users request:
a. only filters existing cards fill `context`,
b. requires actions add `commands` in spoken order,
c. matches neither fallback search.
2. Emit the FizzyOutput object.
DOMAIN KNOWLEDGE & INTERPRETATION RULES
Cards represent issues, features, bugs, tasks, or problems.
Cards have comments and live inside collections.
Context filters describe card **state already true**.
Commands (`/assign`, `/tag`, `/close`, `/search`, `/clear`) apply **new actions**.
Context properties you may use:
* terms array of keywords
* indexed_by "newest", "oldest", "latest", "stalled", "closed"
* assignee_ids array of assignee names
* assignment_status "unassigned"
* engagement_status "considering" | "doing"
* card_ids array of card IDs
* creator_id creators name
* collection_ids array of collections
* tag_ids array of tag names
Explicit filtering rules
------------------------
* Use **terms** only if the query explicitly refers to cards; plain text searches go to `/search`.
* Numbers without the word card(s) default to `terms`.
* "123" terms: ["123"]
* "card 1,2" card_ids: [1, 2]
* X collection collection_ids: ["X"]
* Assigned to X assignee_ids: ["X"]
* Created by X creator_id: "X"
* Tagged with X, #X cards” → tag_ids: ["X"]
* Unassigned cards assignment_status: "unassigned"
* My cards assignee_ids of requester (if identifiable)
* Recent cards indexed_by: "newest"
* Cards with recent activity indexed_by: "latest"
* Completed/closed cards indexed_by: "closed"
* If cards are described as assigned to X (state) and later assign X (action), **only** the first is a filter.
Command interpretation rules
----------------------------
* Unless a clear command applies, fallback to `/search` with the verbatim text.
* When searching for nouns (non-person), prefer `/search` over `terms`.
* Respect the order of commands in the users sentence.
* tag with #design” → `/tag #design` (not a filter)
* #design cards” → context.tag_ids = ["design"] (no `/tag`)
* Assign cards tagged with #design to jz” → context.tag_ids = ["design"]; command `/assign jz`
* close as [reason] or close because [reason] `/close [reason]`
* Lone close `/close` (acts on current context)
Crucial donts
--------------
* **Never** use names or tags mentioned inside commands as filters.
* **Never** add properties tied to UI view (card, list, etc.).
* **All** filters, including terms, must live inside `context`.
* Do not duplicate terms across properties.
* Avoid redundant terms.
Positive & negative examples
----------------------------
**User:** assign andy to the current #design cards assigned to jz and tag them with #v2
**Output:**
{
"context": { "assignee_ids": ["jz"], "tag_ids": ["design"] },
"commands": ["/assign andy", "/tag #v2"]
}
❌ Incorrect (DO NOT generate):
**Incorrect (do NOT do this):**
{
"context": { "assignee_ids": ["andy"], "tag_ids": ["v2"] },
"commands": ["/assign andy", "/tag #v2"]
}
## Commands (prefix '/'):
* Assign user: `/assign [user]`
* Close cards: `/close [optional reason]`
* Tag cards: `/tag #[tag-name]`
* Clear filters: `/clear`
* Search cards: `/search [terms]`
## JSON Output Examples (strictly follow these patterns):
{ "context": { "assignee_ids": ["jorge"] }, "commands": ["/close"] }
{ "context": { "tag_ids": ["design"] } }
Additional examples:
{ "context": { "assignee_ids": ["jorge"] }, "commands": ["/close"] }
{ "context": { "tag_ids": ["design"] } }
{ "commands": ["/assign jorge", "/tag #design"] }
Omit empty arrays or unnecessary properties. At least one property (`context` or `commands`) must exist.
Never include JSON outside of "context" or "commands". E.g: this is not allowed:
{ "terms" => "some keywords" }
It should be:
{ "context" => { "terms" => "some keywords" } }
## Other Strict Instructions:
* NEVER add properties based on view descriptions ("card", "list", etc.).
* Avoid redundant terms.
* Don't duplicate terms across properties.
* All filters including terms must be inside the `"context"` object.
* Favor clarity, precision, and conciseness.
Fallback search example:
{ "commands": ["/search whats blocking deploy"] }
END OF PROMPT
PROMPT
end