Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions packages/junior/src/chat/automations/instruction-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,58 @@ function taskTextProblem(
return `The ${label} names ${requester.fullName} without a Slack mention. Write ${mention} for this person.`;
}

/** People and the saved task that the task text checks compare against. */
type TaskTextReferences = {
creator: InstructionPerson;
/** The person who edits the task, when it is not a new Automation. */
requester?: InstructionPerson | undefined;
/** The task before this edit. Unset for a new Automation. */
current?: AutomationTask | undefined;
};

function previousTaskText(current: AutomationTask | undefined): string {
return (current && automationTaskText(current)) ?? "";
}

/**
* Reject task text that a later run cannot use. The Automation tools and the
* dashboard edit API call this. Restoring a saved version does not, because
* that text was accepted when it was saved. Text that the current task
* already had is not checked again.
* Return why each task text of an Automation tool call cannot run later.
* The tools check every text field, also when the fields do not form a
* valid task, so that one rejection names every problem of the call.
*/
export function automationTaskTextProblems(
texts: {
instruction?: string | undefined;
context?: string | null | undefined;
},
references: TaskTextReferences,
): string[] {
const previousText = previousTaskText(references.current);
const problems: string[] = [];
for (const label of ["instruction", "context"] as const) {
const text = texts[label];
if (!text || text === previousText) continue;
const problem = taskTextProblem(text, {
...references,
previousText,
label,
});
if (problem) problems.push(problem);
}
return problems;
}

/**
* Reject task text that a later run cannot use. The dashboard edit API calls
* this. Restoring a saved version does not, because that text was accepted
* when it was saved. Text that the current task already had is not checked
* again.
*/
export function requireAutomationTaskText(
task: AutomationTask,
references: {
creator: InstructionPerson;
/** The person who edits the task, when it is not a new Automation. */
requester?: InstructionPerson | undefined;
/** The task before this edit. Unset for a new Automation. */
current?: AutomationTask | undefined;
},
references: TaskTextReferences,
): void {
const text = automationTaskText(task);
const previousText =
(references.current && automationTaskText(references.current)) ?? "";
const previousText = previousTaskText(references.current);
if (text === undefined || text === previousText) return;
const label = task.kind === "instruction" ? "instruction" : "context";
const problem = taskTextProblem(text, { ...references, previousText, label });
Expand Down
61 changes: 44 additions & 17 deletions packages/junior/src/chat/automations/tool-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { TaskCandidate } from "./read";
import { automationSkillNameSchema, type AutomationTask } from "./task";
import type { SkillMetadata } from "@/chat/skills";
import { ToolInputError } from "@/chat/tool-support/tool-input-error";
import { automationTaskTextProblems } from "./instruction-references";

const automationResultSchema = z.discriminatedUnion("type", [
compactTaskResultSchema,
Expand Down Expand Up @@ -101,9 +102,38 @@ type TaskToolFields = {
context?: string | null | undefined;
};

/** People that the task text checks need. */
type TaskTextPeople = Omit<
Parameters<typeof automationTaskTextProblems>[1],
"current"
>;

/** Return why the task fields do not form one task mode, or undefined. */
function taskModeProblem(
current: AutomationTask | undefined,
{ instruction, skill, context }: TaskToolFields,
): string | undefined {
if (instruction !== undefined && skill !== undefined) {
return context === undefined
? "Set instruction or skill, not both."
: "Set instruction or skill, not both. Context goes only with a skill: for an instruction, drop skill and context and put everything in the instruction.";
}
if (context === undefined) return undefined;
if (instruction !== undefined) {
return "Context goes only with a skill. Put everything in the instruction.";
}
if (skill === undefined && current?.kind !== "skill") {
return "Context goes only with a skill. Set skill, or change the instruction instead.";
}
return undefined;
}

/** Map the task fields of a new Automation to its task. */
export function newAutomationTask(input: TaskToolFields): AutomationTask {
const task = editedAutomationTask(undefined, input);
export function newAutomationTask(
input: TaskToolFields,
people: TaskTextPeople,
): AutomationTask {
const task = editedAutomationTask(undefined, input, people);
if (!task) throw new ToolInputError("Set instruction or skill.");
return task;
}
Expand All @@ -112,29 +142,26 @@ export function newAutomationTask(input: TaskToolFields): AutomationTask {
* Map the task fields of an update to the saved task, or undefined when they
* change nothing. `instruction` switches to an instruction. `skill` switches
* to a skill and keeps the current context. `context` needs skill mode.
*
* One rejection names every problem of the fields. Each rejection costs the
* model another call, so naming one at a time can take several calls.
*/
export function editedAutomationTask(
current: AutomationTask | undefined,
input: TaskToolFields,
people: TaskTextPeople,
): AutomationTask | undefined {
const modeProblem = taskModeProblem(current, input);
const problems = [
...(modeProblem ? [modeProblem] : []),
...automationTaskTextProblems(input, { ...people, current }),
];
if (problems.length > 0) throw new ToolInputError(problems.join(" "));
const { instruction, skill, context } = input;
if (instruction !== undefined && skill !== undefined)
throw new ToolInputError("Set instruction or skill, not both.");
if (instruction !== undefined) {
if (context !== undefined)
throw new ToolInputError(
"Context goes only with a skill. Put everything in the instruction.",
);
return { kind: "instruction", instruction };
}
if (instruction !== undefined) return { kind: "instruction", instruction };
const kept = current?.kind === "skill" ? current : undefined;
const nextSkill = skill ?? kept?.skill;
if (nextSkill === undefined) {
if (context === undefined) return undefined;
throw new ToolInputError(
"Context goes only with a skill. Set skill, or change the instruction instead.",
);
}
if (nextSkill === undefined) return undefined;
if (skill === undefined && context === undefined) return undefined;
const nextContext = context === undefined ? kept?.context : context;
return {
Expand Down
17 changes: 8 additions & 9 deletions packages/junior/src/chat/automations/tools/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
type AutomationTask,
} from "@/chat/automations/task";
import type { SkillMetadata } from "@/chat/skills";
import { requireAutomationTaskText } from "@/chat/automations/instruction-references";
import {
requireToolCaller,
requireToolCreator,
Expand Down Expand Up @@ -308,15 +307,15 @@ export function createAutomationTool(
const creator = requireToolCreator(caller, destination, kind);
const { createdBy } = creator;
// prepareArguments removed the nulls.
const task = newAutomationTask({
instruction: input.instruction ?? undefined,
skill: input.skill ?? undefined,
context: input.context ?? undefined,
});
const task = newAutomationTask(
{
instruction: input.instruction ?? undefined,
skill: input.skill ?? undefined,
context: input.context ?? undefined,
},
{ creator: createdBy },
);
await requireAutomationSkill(task, undefined);
requireAutomationTaskText(task, {
creator: createdBy,
});
const eventTrigger =
input.trigger.type === "event"
? requireEventAutomationTrigger(
Expand Down
13 changes: 4 additions & 9 deletions packages/junior/src/chat/automations/tools/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from "@/chat/automations/edit-schema";
import type { AutomationTask } from "@/chat/automations/task";
import type { TaskCandidate } from "@/chat/automations/read";
import { requireAutomationTaskText } from "@/chat/automations/instruction-references";
import { automationRevision } from "@/chat/automations/revision";
import {
requireToolAutomation,
Expand Down Expand Up @@ -360,14 +359,10 @@ export function createUpdateAutomationTool(
);
}
const editor = automationEditor(caller.viewer, current);
const editedTask = editedAutomationTask(current.task.task, input);
if (editedTask) {
requireAutomationTaskText(editedTask, {
creator: current.task.createdBy,
requester: editor,
current: current.task.task,
});
}
const editedTask = editedAutomationTask(current.task.task, input, {
creator: current.task.createdBy,
requester: editor,
});
let saved = current;
if (Object.keys(input).length > 0) {
saved =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,15 @@ describe("event automation blocking", () => {
},
]);
try {
const create = (skill: string) =>
const create = (skill: string, fields: Record<string, string> = {}) =>
execute(
createAutomationTool(
automationToolContext(context()),
getEventCatalog(),
),
{
skill,
...fields,
outcomes: [],
trigger: {
type: "event",
Expand All @@ -213,6 +214,15 @@ describe("event automation blocking", () => {
},
`skill-create-${skill}`,
) as Promise<{ automation: { id: string } }>;
// One rejection names every problem, so one more call can fix them.
await expect(
create("release-check", {
instruction: "Post the review summary in this channel.",
context: "Only check getsentry/junior.",
}),
).rejects.toThrow(
'Set instruction or skill, not both. Context goes only with a skill: for an instruction, drop skill and context and put everything in the instruction. The run does not see this conversation, and its outcomes already set where it posts. Remove "this channel"',
);
await expect(create("unknown-check")).rejects.toThrow(
"The skill unknown-check is not installed.",
);
Expand Down
Loading