loopengine 0.5.7 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/approval-links.d.ts +9 -0
- package/dist/core/approval-links.js +38 -0
- package/dist/core/workflow-runs.d.ts +10 -0
- package/dist/core/workflow-runs.js +3 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.js +6 -4
- package/dist/web/agents-config-page.js +281 -53
- package/dist/web/agents-list-page.js +1 -1
- package/dist/web/console-shell.js +8 -36
- package/dist/web/dev-ui-styles.js +0 -4
- package/dist/web/feedback-page.d.ts +14 -0
- package/dist/web/feedback-page.js +56 -0
- package/dist/web/global-config-page.js +45 -3
- package/dist/web/global-config.d.ts +8 -0
- package/dist/web/global-config.js +16 -0
- package/dist/web/inbox-page.js +73 -0
- package/dist/web/notifier-admin.d.ts +4 -1
- package/dist/web/notifier-admin.js +12 -5
- package/dist/web/schedules-page.js +10 -22
- package/dist/web/workflow-evidence.d.ts +10 -3
- package/dist/web/workflow-evidence.js +11 -2
- package/dist/web/workflow-feedback.d.ts +76 -0
- package/dist/web/workflow-feedback.js +119 -0
- package/package.json +1 -1
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ModelCall } from '../core/run-agent.js';
|
|
2
|
+
import type { FeedbackSource, WorkflowRun } from '../core/workflow-runs.js';
|
|
3
|
+
import { type EvalCriterion } from './workflow-evidence.js';
|
|
4
|
+
/** One piece of feedback, as the Feedback tab lists it. */
|
|
5
|
+
export interface FeedbackItem {
|
|
6
|
+
runId: string;
|
|
7
|
+
at: string;
|
|
8
|
+
verdict?: 'good' | 'bad';
|
|
9
|
+
edit?: 'none' | 'light' | 'heavy';
|
|
10
|
+
note?: string;
|
|
11
|
+
by?: string;
|
|
12
|
+
source?: FeedbackSource;
|
|
13
|
+
/** The run's inputs, in a line. */
|
|
14
|
+
inputs: string;
|
|
15
|
+
/** The start of its result, and of the text used instead when it was
|
|
16
|
+
* changed. */
|
|
17
|
+
result: string;
|
|
18
|
+
final?: string;
|
|
19
|
+
workflowVersion?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface FeedbackSummary {
|
|
22
|
+
days: number;
|
|
23
|
+
/** Finished runs in the period, and how many got feedback. */
|
|
24
|
+
runs: number;
|
|
25
|
+
total: number;
|
|
26
|
+
good: number;
|
|
27
|
+
bad: number;
|
|
28
|
+
/** Results changed before they were used, a little or a lot. */
|
|
29
|
+
edited: number;
|
|
30
|
+
/** Feedback by where it came from. */
|
|
31
|
+
sources: Partial<Record<FeedbackSource | 'unknown', number>>;
|
|
32
|
+
/** Newest first. */
|
|
33
|
+
items: FeedbackItem[];
|
|
34
|
+
/** Bad, edited or with a note: what Self-Improve learns from. */
|
|
35
|
+
actionable: number;
|
|
36
|
+
}
|
|
37
|
+
/** Whether a piece of feedback says something to learn from. */
|
|
38
|
+
export declare function isActionable(f: {
|
|
39
|
+
verdict?: string;
|
|
40
|
+
edit?: string;
|
|
41
|
+
note?: string;
|
|
42
|
+
}): boolean;
|
|
43
|
+
/** A workflow's feedback over `days`, from its runs (each carries the
|
|
44
|
+
* latest feedback given on it). */
|
|
45
|
+
export declare function feedbackSummary(runs: WorkflowRun[], days: number): FeedbackSummary;
|
|
46
|
+
export declare const IMPROVE_PROMPT = "You improve an AI workflow from what people said about its results. You get its prompt (what the agent follows on every run), its result format, its success criteria, its tools, and feedback on recent runs: each run's inputs, the result it gave, whether a person marked it good or bad, their note, and the text they actually used when they changed the result.\n\nFind what the feedback says should change: mistakes that recur, rules the agent missed, the tone, length or format people keep fixing, facts it needed and didn't have. Then change the prompt as little as it takes to fix them:\n- Keep everything that works, word for word: its structure, its rules, its examples. Edit or add only what the feedback supports; never remove a rule because one run broke it.\n- Write each change as a plain instruction the agent can follow, in the prompt's own style and language. Put it where it belongs (a rule with the rules, an example with the examples).\n- Never invent facts, policies, prices or names that aren't in the prompt or the feedback. When a fix needs a fact you don't have, say so in \"open\" instead.\n- Ignore feedback that is about one case only, or that contradicts other feedback, unless a pattern is clear.\n- Good feedback tells you what to keep: don't change what people marked good.\n\nAlso suggest up to four new success criteria: yes or no checks that a good result passes, for what people kept correcting, each under 20 words (\"The reply states the refund amount\", \"It signs off with the team's name\"). Leave out any the current criteria already cover. Each has \"kind\": \"judge\" (a reviewer decides, most of them), or \"contains\" / \"not_contains\" with \"value\" for an exact phrase the feedback demands.\n\nReply with JSON only:\n{\"summary\": \"<one or two sentences: what the feedback shows>\",\n \"changes\": [{\"what\": \"<the change, in a short sentence>\", \"why\": \"<the feedback behind it, briefly>\"}],\n \"instructions\": \"<the whole prompt, with the changes made>\",\n \"criteria\": [{\"text\": \"...\", \"kind\": \"judge\"}],\n \"open\": [\"<a question only a person can answer, if any>\"]}\nIf the feedback doesn't call for any change, keep \"instructions\" exactly as given, and leave \"changes\" and \"criteria\" empty.";
|
|
47
|
+
export interface ImproveSuggestion {
|
|
48
|
+
summary: string;
|
|
49
|
+
changes: {
|
|
50
|
+
what: string;
|
|
51
|
+
why: string;
|
|
52
|
+
}[];
|
|
53
|
+
/** The whole prompt with the changes made (the same as given when
|
|
54
|
+
* nothing should change). */
|
|
55
|
+
instructions: string;
|
|
56
|
+
criteria: EvalCriterion[];
|
|
57
|
+
open: string[];
|
|
58
|
+
/** How many pieces of feedback it read. */
|
|
59
|
+
basedOn: number;
|
|
60
|
+
}
|
|
61
|
+
/** Changes to a workflow's prompt and new success criteria, suggested from
|
|
62
|
+
* its feedback, for a person to review. Reads the feedback worth learning
|
|
63
|
+
* from (bad, changed or with a note), newest first, and a few marked good
|
|
64
|
+
* for what to keep. */
|
|
65
|
+
export declare function suggestImprovements(model: ModelCall, input: {
|
|
66
|
+
title: string;
|
|
67
|
+
instructions: string;
|
|
68
|
+
output?: string;
|
|
69
|
+
criteria: {
|
|
70
|
+
text: string;
|
|
71
|
+
kind: string;
|
|
72
|
+
value?: string;
|
|
73
|
+
}[];
|
|
74
|
+
tools: string[];
|
|
75
|
+
items: FeedbackItem[];
|
|
76
|
+
}): Promise<ImproveSuggestion>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Feedback on a workflow's runs, and Self-Improve: what people said about
|
|
2
|
+
// its results (from the Inbox, a signed link in a messaging app or an
|
|
3
|
+
// email, a callback, or an app through the API: see FeedbackSource), as a
|
|
4
|
+
// summary for its Feedback tab; and, from that, changes to its prompt and
|
|
5
|
+
// new success criteria a model suggests, for a person to review, add to
|
|
6
|
+
// the workflow's draft (web/workflow-drafts.ts) and publish as its next
|
|
7
|
+
// version. Nothing here changes a workflow by itself.
|
|
8
|
+
import { randomUUID } from 'node:crypto';
|
|
9
|
+
import { EvidenceError } from './workflow-evidence.js';
|
|
10
|
+
const excerpt = (text, max) => {
|
|
11
|
+
const t = (text ?? '').trim();
|
|
12
|
+
return t.length > max ? t.slice(0, max - 1) + '…' : t;
|
|
13
|
+
};
|
|
14
|
+
/** Whether a piece of feedback says something to learn from. */
|
|
15
|
+
export function isActionable(f) {
|
|
16
|
+
return f.verdict === 'bad' || f.edit === 'light' || f.edit === 'heavy' || Boolean(f.note);
|
|
17
|
+
}
|
|
18
|
+
/** A workflow's feedback over `days`, from its runs (each carries the
|
|
19
|
+
* latest feedback given on it). */
|
|
20
|
+
export function feedbackSummary(runs, days) {
|
|
21
|
+
const finished = runs.filter((r) => r.status === 'done');
|
|
22
|
+
const given = finished.filter((r) => r.feedback).sort((a, b) => b.feedback.at.localeCompare(a.feedback.at));
|
|
23
|
+
const sources = {};
|
|
24
|
+
for (const r of given) {
|
|
25
|
+
const k = r.feedback.source ?? 'unknown';
|
|
26
|
+
sources[k] = (sources[k] ?? 0) + 1;
|
|
27
|
+
}
|
|
28
|
+
const items = given.map((r) => {
|
|
29
|
+
const f = r.feedback;
|
|
30
|
+
return {
|
|
31
|
+
runId: r.run_id, at: f.at,
|
|
32
|
+
...(f.verdict ? { verdict: f.verdict } : {}), ...(f.edit ? { edit: f.edit } : {}), ...(f.note ? { note: f.note } : {}),
|
|
33
|
+
...(f.by ? { by: f.by } : {}), ...(f.source ? { source: f.source } : {}),
|
|
34
|
+
inputs: excerpt(Object.entries(r.params ?? {}).map(([k, v]) => `${k}: ${v}`).join(' · '), 200),
|
|
35
|
+
result: excerpt(r.result, 600),
|
|
36
|
+
...(f.final !== undefined && f.edit !== 'none' ? { final: excerpt(f.final, 600) } : {}),
|
|
37
|
+
...((r.versions?.workflow ?? r.versions?.brief) ? { workflowVersion: r.versions.workflow ?? r.versions.brief } : {}),
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
days, runs: finished.length, total: given.length,
|
|
42
|
+
good: given.filter((r) => r.feedback.verdict === 'good').length,
|
|
43
|
+
bad: given.filter((r) => r.feedback.verdict === 'bad').length,
|
|
44
|
+
edited: given.filter((r) => r.feedback.edit === 'light' || r.feedback.edit === 'heavy').length,
|
|
45
|
+
sources, items: items.slice(0, 100), actionable: given.filter((r) => isActionable(r.feedback)).length,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export const IMPROVE_PROMPT = `You improve an AI workflow from what people said about its results. You get its prompt (what the agent follows on every run), its result format, its success criteria, its tools, and feedback on recent runs: each run's inputs, the result it gave, whether a person marked it good or bad, their note, and the text they actually used when they changed the result.
|
|
49
|
+
|
|
50
|
+
Find what the feedback says should change: mistakes that recur, rules the agent missed, the tone, length or format people keep fixing, facts it needed and didn't have. Then change the prompt as little as it takes to fix them:
|
|
51
|
+
- Keep everything that works, word for word: its structure, its rules, its examples. Edit or add only what the feedback supports; never remove a rule because one run broke it.
|
|
52
|
+
- Write each change as a plain instruction the agent can follow, in the prompt's own style and language. Put it where it belongs (a rule with the rules, an example with the examples).
|
|
53
|
+
- Never invent facts, policies, prices or names that aren't in the prompt or the feedback. When a fix needs a fact you don't have, say so in "open" instead.
|
|
54
|
+
- Ignore feedback that is about one case only, or that contradicts other feedback, unless a pattern is clear.
|
|
55
|
+
- Good feedback tells you what to keep: don't change what people marked good.
|
|
56
|
+
|
|
57
|
+
Also suggest up to four new success criteria: yes or no checks that a good result passes, for what people kept correcting, each under 20 words ("The reply states the refund amount", "It signs off with the team's name"). Leave out any the current criteria already cover. Each has "kind": "judge" (a reviewer decides, most of them), or "contains" / "not_contains" with "value" for an exact phrase the feedback demands.
|
|
58
|
+
|
|
59
|
+
Reply with JSON only:
|
|
60
|
+
{"summary": "<one or two sentences: what the feedback shows>",
|
|
61
|
+
"changes": [{"what": "<the change, in a short sentence>", "why": "<the feedback behind it, briefly>"}],
|
|
62
|
+
"instructions": "<the whole prompt, with the changes made>",
|
|
63
|
+
"criteria": [{"text": "...", "kind": "judge"}],
|
|
64
|
+
"open": ["<a question only a person can answer, if any>"]}
|
|
65
|
+
If the feedback doesn't call for any change, keep "instructions" exactly as given, and leave "changes" and "criteria" empty.`;
|
|
66
|
+
const KINDS = ['judge', 'contains', 'not_contains'];
|
|
67
|
+
/** Changes to a workflow's prompt and new success criteria, suggested from
|
|
68
|
+
* its feedback, for a person to review. Reads the feedback worth learning
|
|
69
|
+
* from (bad, changed or with a note), newest first, and a few marked good
|
|
70
|
+
* for what to keep. */
|
|
71
|
+
export async function suggestImprovements(model, input) {
|
|
72
|
+
const learn = input.items.filter(isActionable).slice(0, 14);
|
|
73
|
+
const keep = input.items.filter((i) => !isActionable(i) && i.verdict === 'good').slice(0, 4);
|
|
74
|
+
if (!learn.length)
|
|
75
|
+
throw new EvidenceError('There is no feedback to learn from yet: mark a result bad, change it, or add a note, from the Inbox, a messaging app or the API.');
|
|
76
|
+
const block = (i, n) => `<run n="${n}">\n<inputs>${i.inputs || 'none'}</inputs>\n<result>${i.result}</result>\n` +
|
|
77
|
+
`<feedback verdict="${i.verdict ?? 'none'}" edited="${i.edit ?? 'no'}">${i.note ?? ''}</feedback>\n` +
|
|
78
|
+
(i.final ? `<used_instead>${i.final}</used_instead>\n` : '') + '</run>';
|
|
79
|
+
const content = `The workflow: ${input.title}\n\n<prompt>\n${input.instructions.slice(0, 20000)}\n</prompt>\n\n` +
|
|
80
|
+
`<result_format>${input.output ?? 'none'}</result_format>\n\n` +
|
|
81
|
+
`<success_criteria>\n${input.criteria.map((c) => `- [${c.kind}${c.value ? `: ${c.value}` : ''}] ${c.text}`).join('\n') || 'none'}\n</success_criteria>\n\n` +
|
|
82
|
+
`Its tools: ${input.tools.length ? input.tools.join(', ') : 'none'}\n\n` +
|
|
83
|
+
`Feedback to learn from (newest first):\n${learn.map((i, n) => block(i, n + 1)).join('\n')}\n\n` +
|
|
84
|
+
(keep.length ? `Results people marked good (keep what made them good):\n${keep.map((i, n) => block(i, n + 1)).join('\n')}` : '');
|
|
85
|
+
const response = await model([{ role: 'user', content }], IMPROVE_PROMPT, []);
|
|
86
|
+
const text = response.content.filter((b) => b.type === 'text').map((b) => b.text ?? '').join('');
|
|
87
|
+
const match = /\{[\s\S]*\}/.exec(text);
|
|
88
|
+
let parsed = {};
|
|
89
|
+
try {
|
|
90
|
+
parsed = JSON.parse(match?.[0] ?? '');
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
throw new EvidenceError('The model didn’t give usable suggestions this time: try again.');
|
|
94
|
+
}
|
|
95
|
+
const str = (v, max) => (typeof v === 'string' ? v.trim().slice(0, max) : '');
|
|
96
|
+
const instructions = str(parsed.instructions, 30000) || input.instructions;
|
|
97
|
+
const have = new Set(input.criteria.map((c) => c.text.toLowerCase()));
|
|
98
|
+
const criteria = (Array.isArray(parsed.criteria) ? parsed.criteria : []).flatMap((raw) => {
|
|
99
|
+
const c = (raw ?? {});
|
|
100
|
+
const t = str(c.text, 300);
|
|
101
|
+
let kind = KINDS.includes(c.kind) ? c.kind : 'judge';
|
|
102
|
+
const value = str(c.value, 300);
|
|
103
|
+
if (kind !== 'judge' && !value)
|
|
104
|
+
kind = 'judge';
|
|
105
|
+
return t && !have.has(t.toLowerCase()) ? [{ id: randomUUID().slice(0, 8), text: t, kind, ...(kind !== 'judge' ? { value } : {}) }] : [];
|
|
106
|
+
}).slice(0, 4);
|
|
107
|
+
return {
|
|
108
|
+
summary: str(parsed.summary, 600),
|
|
109
|
+
changes: (Array.isArray(parsed.changes) ? parsed.changes : []).flatMap((raw) => {
|
|
110
|
+
const c = (raw ?? {});
|
|
111
|
+
const what = str(c.what, 300);
|
|
112
|
+
return what ? [{ what, why: str(c.why, 400) }] : [];
|
|
113
|
+
}).slice(0, 10),
|
|
114
|
+
instructions,
|
|
115
|
+
criteria,
|
|
116
|
+
open: (Array.isArray(parsed.open) ? parsed.open : []).map((o) => str(o, 300)).filter(Boolean).slice(0, 5),
|
|
117
|
+
basedOn: learn.length + keep.length,
|
|
118
|
+
};
|
|
119
|
+
}
|