@praudent/dagma 0.0.0-stage → 0.1.1
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/admin-ui/assets/index-CHJ8lsZp.js +120 -0
- package/dist/admin-ui/assets/index-XyqtD-IA.css +1 -0
- package/dist/admin-ui/index.html +12 -0
- package/dist/blueprint/agent.d.ts +148 -0
- package/dist/blueprint/agent.js +1 -0
- package/dist/blueprint/declaration-site.d.ts +7 -0
- package/dist/blueprint/declaration-site.js +8 -0
- package/dist/blueprint/declared-structure.d.ts +145 -0
- package/dist/blueprint/declared-structure.js +213 -0
- package/dist/blueprint/define-run-state.d.ts +79 -0
- package/dist/blueprint/define-run-state.js +89 -0
- package/dist/blueprint/load-blueprint.d.ts +28 -0
- package/dist/blueprint/load-blueprint.js +85 -0
- package/dist/blueprint/outcomes.d.ts +76 -0
- package/dist/blueprint/outcomes.js +10 -0
- package/dist/blueprint/phase.d.ts +153 -0
- package/dist/blueprint/phase.js +1 -0
- package/dist/blueprint/places.d.ts +26 -0
- package/dist/blueprint/places.js +37 -0
- package/dist/blueprint/prompt-file.d.ts +81 -0
- package/dist/blueprint/prompt-file.js +131 -0
- package/dist/blueprint/reply-reader.d.ts +36 -0
- package/dist/blueprint/reply-reader.js +38 -0
- package/dist/blueprint/run-settings.d.ts +38 -0
- package/dist/blueprint/run-settings.js +1 -0
- package/dist/blueprint/serializer.d.ts +14 -0
- package/dist/blueprint/serializer.js +4 -0
- package/dist/blueprint/write-scope.d.ts +39 -0
- package/dist/blueprint/write-scope.js +106 -0
- package/dist/command.d.ts +2 -0
- package/dist/command.js +148 -0
- package/dist/commands/calls-answering.d.ts +16 -0
- package/dist/commands/calls-answering.js +63 -0
- package/dist/commands/json-file.d.ts +6 -0
- package/dist/commands/json-file.js +19 -0
- package/dist/commands/run-command.d.ts +23 -0
- package/dist/commands/run-command.js +172 -0
- package/dist/commands/typed-answers.d.ts +10 -0
- package/dist/commands/typed-answers.js +43 -0
- package/dist/config.d.ts +27 -0
- package/dist/config.js +20 -0
- package/dist/devtool-server/app-pages.d.ts +12 -0
- package/dist/devtool-server/app-pages.js +68 -0
- package/dist/devtool-server/blueprint-description.d.ts +21 -0
- package/dist/devtool-server/blueprint-description.js +27 -0
- package/dist/devtool-server/connected-runs.d.ts +36 -0
- package/dist/devtool-server/connected-runs.js +201 -0
- package/dist/devtool-server/devtool-server.d.ts +17 -0
- package/dist/devtool-server/devtool-server.js +294 -0
- package/dist/devtool-server/devtool-shapes.d.ts +312 -0
- package/dist/devtool-server/devtool-shapes.js +24 -0
- package/dist/devtool-server/listed-runs.d.ts +17 -0
- package/dist/devtool-server/listed-runs.js +42 -0
- package/dist/devtool-server/print-blueprint.d.ts +1 -0
- package/dist/devtool-server/print-blueprint.js +36 -0
- package/dist/example/added-species.d.ts +21 -0
- package/dist/example/added-species.js +19 -0
- package/dist/example/cover.d.ts +21 -0
- package/dist/example/cover.js +67 -0
- package/dist/example/cross-references.d.ts +21 -0
- package/dist/example/cross-references.js +77 -0
- package/dist/example/entry-drafts.d.ts +21 -0
- package/dist/example/entry-drafts.js +63 -0
- package/dist/example/entry-merge.d.ts +108 -0
- package/dist/example/entry-merge.js +284 -0
- package/dist/example/field-guide.d.ts +26 -0
- package/dist/example/field-guide.js +40 -0
- package/dist/example/guide-page.d.ts +21 -0
- package/dist/example/guide-page.js +15 -0
- package/dist/example/missing-species.d.ts +21 -0
- package/dist/example/missing-species.js +59 -0
- package/dist/example/run-state.d.ts +803 -0
- package/dist/example/run-state.js +58 -0
- package/dist/example/serializers.d.ts +32 -0
- package/dist/example/serializers.js +42 -0
- package/dist/example/species-list.d.ts +47 -0
- package/dist/example/species-list.js +93 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +25 -0
- package/dist/inference/anthropic-api.d.ts +37 -0
- package/dist/inference/anthropic-api.js +197 -0
- package/dist/inference/anthropic-prices.d.ts +18 -0
- package/dist/inference/anthropic-prices.js +61 -0
- package/dist/inference/anthropic-request.d.ts +51 -0
- package/dist/inference/anthropic-request.js +208 -0
- package/dist/inference/bedrock-api.d.ts +45 -0
- package/dist/inference/bedrock-api.js +242 -0
- package/dist/inference/bedrock-prices.d.ts +22 -0
- package/dist/inference/bedrock-prices.js +79 -0
- package/dist/inference/bedrock-request.d.ts +27 -0
- package/dist/inference/bedrock-request.js +193 -0
- package/dist/inference/call-queue-shapes.d.ts +273 -0
- package/dist/inference/call-queue-shapes.js +82 -0
- package/dist/inference/call-queue.d.ts +109 -0
- package/dist/inference/call-queue.js +408 -0
- package/dist/inference/gemini-api.d.ts +44 -0
- package/dist/inference/gemini-api.js +191 -0
- package/dist/inference/gemini-prices.d.ts +19 -0
- package/dist/inference/gemini-prices.js +33 -0
- package/dist/inference/gemini-request.d.ts +25 -0
- package/dist/inference/gemini-request.js +126 -0
- package/dist/inference/inference.d.ts +84 -0
- package/dist/inference/inference.js +9 -0
- package/dist/inference/openai-api.d.ts +29 -0
- package/dist/inference/openai-api.js +138 -0
- package/dist/inference/openai-prices.d.ts +12 -0
- package/dist/inference/openai-prices.js +25 -0
- package/dist/inference/openai-request.d.ts +50 -0
- package/dist/inference/openai-request.js +100 -0
- package/dist/inference/own-worker.d.ts +6 -0
- package/dist/inference/own-worker.js +16 -0
- package/dist/inference/scripted.d.ts +146 -0
- package/dist/inference/scripted.js +67 -0
- package/dist/run/assignments/agent-call.d.ts +32 -0
- package/dist/run/assignments/agent-call.js +218 -0
- package/dist/run/assignments/agent-phase.d.ts +14 -0
- package/dist/run/assignments/agent-phase.js +243 -0
- package/dist/run/assignments/assignment-memory.d.ts +42 -0
- package/dist/run/assignments/assignment-memory.js +52 -0
- package/dist/run/assignments/assignment.d.ts +17 -0
- package/dist/run/assignments/assignment.js +252 -0
- package/dist/run/assignments/model-settings.d.ts +39 -0
- package/dist/run/assignments/model-settings.js +57 -0
- package/dist/run/assignments/prepared-call.d.ts +28 -0
- package/dist/run/assignments/prepared-call.js +115 -0
- package/dist/run/assignments/tool-call.d.ts +26 -0
- package/dist/run/assignments/tool-call.js +108 -0
- package/dist/run/blueprint-mistake.d.ts +8 -0
- package/dist/run/blueprint-mistake.js +14 -0
- package/dist/run/code-phase.d.ts +11 -0
- package/dist/run/code-phase.js +59 -0
- package/dist/run/create-run.d.ts +36 -0
- package/dist/run/create-run.js +119 -0
- package/dist/run/execute-run.d.ts +7 -0
- package/dist/run/execute-run.js +158 -0
- package/dist/run/record/apply-event.d.ts +13 -0
- package/dist/run/record/apply-event.js +256 -0
- package/dist/run/record/events.d.ts +187 -0
- package/dist/run/record/events.js +1 -0
- package/dist/run/record/postgres-run-store.d.ts +13 -0
- package/dist/run/record/postgres-run-store.js +189 -0
- package/dist/run/record/progress-projection.d.ts +83 -0
- package/dist/run/record/progress-projection.js +167 -0
- package/dist/run/record/progress.d.ts +207 -0
- package/dist/run/record/progress.js +63 -0
- package/dist/run/record/run-store.d.ts +74 -0
- package/dist/run/record/run-store.js +1 -0
- package/dist/run/record/run-summary.d.ts +8 -0
- package/dist/run/record/run-summary.js +19 -0
- package/dist/run/record/run-types.d.ts +95 -0
- package/dist/run/record/run-types.js +1 -0
- package/dist/run/record/state-changes.d.ts +42 -0
- package/dist/run/record/state-changes.js +157 -0
- package/dist/run/resume-run.d.ts +25 -0
- package/dist/run/resume-run.js +99 -0
- package/dist/run/run-endings.d.ts +20 -0
- package/dist/run/run-endings.js +20 -0
- package/dist/run/run-option-shapes.d.ts +176 -0
- package/dist/run/run-option-shapes.js +47 -0
- package/dist/run/run-session.d.ts +144 -0
- package/dist/run/run-session.js +278 -0
- package/dist/worker/answer-queued-call.d.ts +19 -0
- package/dist/worker/answer-queued-call.js +100 -0
- package/dist/worker/launchers/claude-code.d.ts +7 -0
- package/dist/worker/launchers/claude-code.js +156 -0
- package/dist/worker/launchers/codex.d.ts +6 -0
- package/dist/worker/launchers/codex.js +141 -0
- package/dist/worker/launchers/launch.d.ts +31 -0
- package/dist/worker/launchers/launch.js +6 -0
- package/dist/worker/launchers/program-run.d.ts +30 -0
- package/dist/worker/launchers/program-run.js +92 -0
- package/dist/worker/launchers/tool-server.d.ts +26 -0
- package/dist/worker/launchers/tool-server.js +90 -0
- package/dist/worker/worker.d.ts +13 -0
- package/dist/worker/worker.js +170 -0
- package/package.json +43 -4
- package/README.md +0 -3
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { type Infer } from './inference.js';
|
|
3
|
+
/** One scripted answer to one call: optional tool calls made first, then the reply. `failure`
|
|
4
|
+
* makes the call fail instead: `connection` is retried, `provider` is not. `delayMs` is how long
|
|
5
|
+
* the call takes, so a scripted run can be watched while it runs. */
|
|
6
|
+
declare const ScriptedReplySchema: z.ZodObject<{
|
|
7
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
8
|
+
name: z.ZodString;
|
|
9
|
+
args: z.ZodUnknown;
|
|
10
|
+
}, "strict", z.ZodTypeAny, {
|
|
11
|
+
name: string;
|
|
12
|
+
args?: unknown;
|
|
13
|
+
}, {
|
|
14
|
+
name: string;
|
|
15
|
+
args?: unknown;
|
|
16
|
+
}>, "many">>;
|
|
17
|
+
reply: z.ZodOptional<z.ZodString>;
|
|
18
|
+
tokens: z.ZodOptional<z.ZodObject<{
|
|
19
|
+
input: z.ZodOptional<z.ZodNumber>;
|
|
20
|
+
cached: z.ZodOptional<z.ZodNumber>;
|
|
21
|
+
output: z.ZodOptional<z.ZodNumber>;
|
|
22
|
+
reasoning: z.ZodOptional<z.ZodNumber>;
|
|
23
|
+
}, "strict", z.ZodTypeAny, {
|
|
24
|
+
input?: number | undefined;
|
|
25
|
+
cached?: number | undefined;
|
|
26
|
+
output?: number | undefined;
|
|
27
|
+
reasoning?: number | undefined;
|
|
28
|
+
}, {
|
|
29
|
+
input?: number | undefined;
|
|
30
|
+
cached?: number | undefined;
|
|
31
|
+
output?: number | undefined;
|
|
32
|
+
reasoning?: number | undefined;
|
|
33
|
+
}>>;
|
|
34
|
+
programCostUsd: z.ZodOptional<z.ZodNumber>;
|
|
35
|
+
reasoning: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
36
|
+
failure: z.ZodOptional<z.ZodEnum<["connection", "provider"]>>;
|
|
37
|
+
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
38
|
+
}, "strict", z.ZodTypeAny, {
|
|
39
|
+
reply?: string | undefined;
|
|
40
|
+
reasoning?: string[] | undefined;
|
|
41
|
+
tokens?: {
|
|
42
|
+
input?: number | undefined;
|
|
43
|
+
cached?: number | undefined;
|
|
44
|
+
output?: number | undefined;
|
|
45
|
+
reasoning?: number | undefined;
|
|
46
|
+
} | undefined;
|
|
47
|
+
programCostUsd?: number | undefined;
|
|
48
|
+
failure?: "connection" | "provider" | undefined;
|
|
49
|
+
toolCalls?: {
|
|
50
|
+
name: string;
|
|
51
|
+
args?: unknown;
|
|
52
|
+
}[] | undefined;
|
|
53
|
+
delayMs?: number | undefined;
|
|
54
|
+
}, {
|
|
55
|
+
reply?: string | undefined;
|
|
56
|
+
reasoning?: string[] | undefined;
|
|
57
|
+
tokens?: {
|
|
58
|
+
input?: number | undefined;
|
|
59
|
+
cached?: number | undefined;
|
|
60
|
+
output?: number | undefined;
|
|
61
|
+
reasoning?: number | undefined;
|
|
62
|
+
} | undefined;
|
|
63
|
+
programCostUsd?: number | undefined;
|
|
64
|
+
failure?: "connection" | "provider" | undefined;
|
|
65
|
+
toolCalls?: {
|
|
66
|
+
name: string;
|
|
67
|
+
args?: unknown;
|
|
68
|
+
}[] | undefined;
|
|
69
|
+
delayMs?: number | undefined;
|
|
70
|
+
}>;
|
|
71
|
+
/** Scripted replies by agent name, then by subject key (`once` for work that runs once, `*` for
|
|
72
|
+
* any subject). An agent that two lanes run on the same key can have a list for one lane, under
|
|
73
|
+
* `<lane>/<subject key>`. Each list is used in order, one entry per call the agent has answered
|
|
74
|
+
* for that lane, subject and repeat across the whole run; the last entry repeats. */
|
|
75
|
+
export declare const ScriptedRepliesSchema: z.ZodRecord<z.ZodString, z.ZodRecord<z.ZodString, z.ZodArray<z.ZodObject<{
|
|
76
|
+
toolCalls: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
77
|
+
name: z.ZodString;
|
|
78
|
+
args: z.ZodUnknown;
|
|
79
|
+
}, "strict", z.ZodTypeAny, {
|
|
80
|
+
name: string;
|
|
81
|
+
args?: unknown;
|
|
82
|
+
}, {
|
|
83
|
+
name: string;
|
|
84
|
+
args?: unknown;
|
|
85
|
+
}>, "many">>;
|
|
86
|
+
reply: z.ZodOptional<z.ZodString>;
|
|
87
|
+
tokens: z.ZodOptional<z.ZodObject<{
|
|
88
|
+
input: z.ZodOptional<z.ZodNumber>;
|
|
89
|
+
cached: z.ZodOptional<z.ZodNumber>;
|
|
90
|
+
output: z.ZodOptional<z.ZodNumber>;
|
|
91
|
+
reasoning: z.ZodOptional<z.ZodNumber>;
|
|
92
|
+
}, "strict", z.ZodTypeAny, {
|
|
93
|
+
input?: number | undefined;
|
|
94
|
+
cached?: number | undefined;
|
|
95
|
+
output?: number | undefined;
|
|
96
|
+
reasoning?: number | undefined;
|
|
97
|
+
}, {
|
|
98
|
+
input?: number | undefined;
|
|
99
|
+
cached?: number | undefined;
|
|
100
|
+
output?: number | undefined;
|
|
101
|
+
reasoning?: number | undefined;
|
|
102
|
+
}>>;
|
|
103
|
+
programCostUsd: z.ZodOptional<z.ZodNumber>;
|
|
104
|
+
reasoning: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
105
|
+
failure: z.ZodOptional<z.ZodEnum<["connection", "provider"]>>;
|
|
106
|
+
delayMs: z.ZodOptional<z.ZodNumber>;
|
|
107
|
+
}, "strict", z.ZodTypeAny, {
|
|
108
|
+
reply?: string | undefined;
|
|
109
|
+
reasoning?: string[] | undefined;
|
|
110
|
+
tokens?: {
|
|
111
|
+
input?: number | undefined;
|
|
112
|
+
cached?: number | undefined;
|
|
113
|
+
output?: number | undefined;
|
|
114
|
+
reasoning?: number | undefined;
|
|
115
|
+
} | undefined;
|
|
116
|
+
programCostUsd?: number | undefined;
|
|
117
|
+
failure?: "connection" | "provider" | undefined;
|
|
118
|
+
toolCalls?: {
|
|
119
|
+
name: string;
|
|
120
|
+
args?: unknown;
|
|
121
|
+
}[] | undefined;
|
|
122
|
+
delayMs?: number | undefined;
|
|
123
|
+
}, {
|
|
124
|
+
reply?: string | undefined;
|
|
125
|
+
reasoning?: string[] | undefined;
|
|
126
|
+
tokens?: {
|
|
127
|
+
input?: number | undefined;
|
|
128
|
+
cached?: number | undefined;
|
|
129
|
+
output?: number | undefined;
|
|
130
|
+
reasoning?: number | undefined;
|
|
131
|
+
} | undefined;
|
|
132
|
+
programCostUsd?: number | undefined;
|
|
133
|
+
failure?: "connection" | "provider" | undefined;
|
|
134
|
+
toolCalls?: {
|
|
135
|
+
name: string;
|
|
136
|
+
args?: unknown;
|
|
137
|
+
}[] | undefined;
|
|
138
|
+
delayMs?: number | undefined;
|
|
139
|
+
}>, "many">>>;
|
|
140
|
+
export type ScriptedReply = z.infer<typeof ScriptedReplySchema>;
|
|
141
|
+
export type ScriptedReplies = z.infer<typeof ScriptedRepliesSchema>;
|
|
142
|
+
/** Answers calls from a script. No model is called. The tests and sample runs use it. */
|
|
143
|
+
export declare function scriptedInference({ replies }: {
|
|
144
|
+
replies: ScriptedReplies;
|
|
145
|
+
}): Infer;
|
|
146
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ProviderConnectionError } from './inference.js';
|
|
3
|
+
const TokensSchema = z
|
|
4
|
+
.object({
|
|
5
|
+
input: z.number().optional(),
|
|
6
|
+
cached: z.number().optional(),
|
|
7
|
+
output: z.number().optional(),
|
|
8
|
+
reasoning: z.number().optional(),
|
|
9
|
+
})
|
|
10
|
+
.strict();
|
|
11
|
+
/** One scripted answer to one call: optional tool calls made first, then the reply. `failure`
|
|
12
|
+
* makes the call fail instead: `connection` is retried, `provider` is not. `delayMs` is how long
|
|
13
|
+
* the call takes, so a scripted run can be watched while it runs. */
|
|
14
|
+
const ScriptedReplySchema = z
|
|
15
|
+
.object({
|
|
16
|
+
toolCalls: z.object({ name: z.string(), args: z.unknown() }).strict().array().optional(),
|
|
17
|
+
reply: z.string().optional(),
|
|
18
|
+
tokens: TokensSchema.optional(),
|
|
19
|
+
programCostUsd: z.number().optional(),
|
|
20
|
+
reasoning: z.string().array().optional(),
|
|
21
|
+
failure: z.enum(['connection', 'provider']).optional(),
|
|
22
|
+
delayMs: z.number().int().positive().optional(),
|
|
23
|
+
})
|
|
24
|
+
.strict();
|
|
25
|
+
/** Scripted replies by agent name, then by subject key (`once` for work that runs once, `*` for
|
|
26
|
+
* any subject). An agent that two lanes run on the same key can have a list for one lane, under
|
|
27
|
+
* `<lane>/<subject key>`. Each list is used in order, one entry per call the agent has answered
|
|
28
|
+
* for that lane, subject and repeat across the whole run; the last entry repeats. */
|
|
29
|
+
export const ScriptedRepliesSchema = z.record(z.record(ScriptedReplySchema.array()));
|
|
30
|
+
/** Answers calls from a script. No model is called. The tests and sample runs use it. */
|
|
31
|
+
export function scriptedInference({ replies }) {
|
|
32
|
+
return async ({ call, callTool, signal }) => {
|
|
33
|
+
const forAgent = Object.hasOwn(replies, call.agent) ? replies[call.agent] : {};
|
|
34
|
+
const listName = [`${call.lane}/${call.subjectKey}`, call.subjectKey, '*'].find((name) => Object.hasOwn(forAgent, name));
|
|
35
|
+
const script = listName === undefined ? undefined : forAgent[listName];
|
|
36
|
+
if (!script?.length)
|
|
37
|
+
throw new Error(`The scripted replies have none for ${call.agent} on ${call.subjectKey}.`);
|
|
38
|
+
const scripted = script[Math.min(call.callNumber, script.length) - 1];
|
|
39
|
+
if (scripted.delayMs)
|
|
40
|
+
await new Promise((resolve, reject) => {
|
|
41
|
+
const ended = () => {
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
reject(signal.reason);
|
|
44
|
+
};
|
|
45
|
+
const timer = setTimeout(() => {
|
|
46
|
+
signal.removeEventListener('abort', ended);
|
|
47
|
+
resolve();
|
|
48
|
+
}, scripted.delayMs);
|
|
49
|
+
signal.addEventListener('abort', ended, { once: true });
|
|
50
|
+
});
|
|
51
|
+
if (scripted.failure === 'connection')
|
|
52
|
+
throw new ProviderConnectionError('The scripted connection failed.');
|
|
53
|
+
if (scripted.failure)
|
|
54
|
+
throw new Error('The scripted provider refused the call.');
|
|
55
|
+
for (const toolCall of scripted.toolCalls ?? []) {
|
|
56
|
+
signal.throwIfAborted();
|
|
57
|
+
await callTool({ name: toolCall.name, args: toolCall.args });
|
|
58
|
+
}
|
|
59
|
+
signal.throwIfAborted();
|
|
60
|
+
return {
|
|
61
|
+
replyText: scripted.reply ?? '',
|
|
62
|
+
tokens: scripted.tokens ?? {},
|
|
63
|
+
...(scripted.programCostUsd !== undefined ? { programCostUsd: scripted.programCostUsd } : {}),
|
|
64
|
+
reasoning: (scripted.reasoning ?? []).map((text) => ({ text, afterToolCalls: 0 })),
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AgentInChain } from '../../blueprint/agent.js';
|
|
2
|
+
import type { AgentPhase } from '../../blueprint/phase.js';
|
|
3
|
+
import type { AssignmentProgress } from '../record/progress.js';
|
|
4
|
+
import type { RunSession } from '../run-session.js';
|
|
5
|
+
import type { AssignmentMemory } from './assignment-memory.js';
|
|
6
|
+
/** How one call of an agent ended: with a reply; because one of its tools asked the person
|
|
7
|
+
* something that now waits; or failed for good, for an agent that declares `whenCallFails`. */
|
|
8
|
+
export type AgentCallEnd<State> = {
|
|
9
|
+
ended: 'replied';
|
|
10
|
+
callId: string;
|
|
11
|
+
replyText: string;
|
|
12
|
+
stateAtPrompt: State;
|
|
13
|
+
} | {
|
|
14
|
+
ended: 'asked';
|
|
15
|
+
} | {
|
|
16
|
+
ended: 'failed';
|
|
17
|
+
callId: string;
|
|
18
|
+
failure: string;
|
|
19
|
+
} | {
|
|
20
|
+
ended: 'draining';
|
|
21
|
+
};
|
|
22
|
+
/** Calls one agent for one assignment and answers its tool calls. When the connection fails the
|
|
23
|
+
* call is made again, three attempts in all, each its own call with its prompt built again from
|
|
24
|
+
* the state as it stands. A call that fails for good fails the run, unless its agent declares
|
|
25
|
+
* `whenCallFails`. */
|
|
26
|
+
export declare function callAgent<State>({ session, phase, assignment, agent, memory, }: {
|
|
27
|
+
session: RunSession<State>;
|
|
28
|
+
phase: AgentPhase<State>;
|
|
29
|
+
assignment: AssignmentProgress;
|
|
30
|
+
agent: AgentInChain<State, never>;
|
|
31
|
+
memory: AssignmentMemory<State>;
|
|
32
|
+
}): Promise<AgentCallEnd<State>>;
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { ProviderAccessError, ProviderConnectionError, } from '../../inference/inference.js';
|
|
2
|
+
import { RunFailed, RunStopped } from '../run-endings.js';
|
|
3
|
+
import { modelSettingsFor, resolvedWorkerFor } from './model-settings.js';
|
|
4
|
+
import { byteLength, preparedCall } from './prepared-call.js';
|
|
5
|
+
import { answerToolCall } from './tool-call.js';
|
|
6
|
+
const MAX_REPLY_BYTES = 1_000_000;
|
|
7
|
+
const PROVIDER_ATTEMPTS = 3;
|
|
8
|
+
const messageOf = (error) => (error instanceof Error ? error.message : String(error));
|
|
9
|
+
/** Waits, ending early with the signal's reason when it is triggered. */
|
|
10
|
+
function wait({ ms, signal }) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
if (signal.aborted)
|
|
13
|
+
return reject(signal.reason);
|
|
14
|
+
const ended = () => {
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
reject(signal.reason);
|
|
17
|
+
};
|
|
18
|
+
const timer = setTimeout(() => {
|
|
19
|
+
signal.removeEventListener('abort', ended);
|
|
20
|
+
resolve();
|
|
21
|
+
}, ms);
|
|
22
|
+
signal.addEventListener('abort', ended, { once: true });
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** Calls one agent for one assignment and answers its tool calls. When the connection fails the
|
|
26
|
+
* call is made again, three attempts in all, each its own call with its prompt built again from
|
|
27
|
+
* the state as it stands. A call that fails for good fails the run, unless its agent declares
|
|
28
|
+
* `whenCallFails`. */
|
|
29
|
+
export async function callAgent({ session, phase, assignment, agent, memory, }) {
|
|
30
|
+
const callsAnsweredBy = session.progress.callsAnsweredBy ?? 'scripted';
|
|
31
|
+
const { worker, definition: workerDefinition } = resolvedWorkerFor({
|
|
32
|
+
workers: session.blueprint.workers,
|
|
33
|
+
phase,
|
|
34
|
+
agent,
|
|
35
|
+
});
|
|
36
|
+
const settings = modelSettingsFor({
|
|
37
|
+
blueprint: session.blueprint.modelSettings,
|
|
38
|
+
workers: session.blueprint.workers,
|
|
39
|
+
phase,
|
|
40
|
+
agent,
|
|
41
|
+
overrides: session.progress.overrides,
|
|
42
|
+
});
|
|
43
|
+
const model = callsAnsweredBy === 'scripted' || callsAnsweredBy === 'custom'
|
|
44
|
+
? undefined
|
|
45
|
+
: settings.model?.[callsAnsweredBy];
|
|
46
|
+
for (let providerAttempt = 1;; providerAttempt++) {
|
|
47
|
+
// No call starts once Stop or a graceful drain was asked for, or the run has failed.
|
|
48
|
+
session.stopSignal.throwIfAborted();
|
|
49
|
+
if (session.drainSignal.aborted)
|
|
50
|
+
return { ended: 'draining' };
|
|
51
|
+
const { callId, attempt, stateAtPrompt, stateSeq, callFile, call } = await preparedCall({
|
|
52
|
+
session,
|
|
53
|
+
assignment,
|
|
54
|
+
agent,
|
|
55
|
+
memory,
|
|
56
|
+
callsAnsweredBy,
|
|
57
|
+
worker,
|
|
58
|
+
workerLabel: workerDefinition?.label,
|
|
59
|
+
settings,
|
|
60
|
+
});
|
|
61
|
+
const callEnd = new AbortController();
|
|
62
|
+
/** Triggered by Stop, by the run's first failure and by the call's end. */
|
|
63
|
+
const callSignal = AbortSignal.any([session.stopSignal, callEnd.signal]);
|
|
64
|
+
await session.saveCallFile(callFile);
|
|
65
|
+
// Stop, drain, or the first failure may have arrived while the prompt was built and saved.
|
|
66
|
+
session.stopSignal.throwIfAborted();
|
|
67
|
+
if (session.drainSignal.aborted)
|
|
68
|
+
return { ended: 'draining' };
|
|
69
|
+
await session.record({
|
|
70
|
+
kind: 'call-started',
|
|
71
|
+
callId,
|
|
72
|
+
assignmentId: assignment.id,
|
|
73
|
+
phase: assignment.phase,
|
|
74
|
+
agent: agent.name,
|
|
75
|
+
placeInChain: assignment.placeInChain,
|
|
76
|
+
attempt,
|
|
77
|
+
answeredBy: callsAnsweredBy,
|
|
78
|
+
...(model ? { model } : {}),
|
|
79
|
+
...(settings.effort ? { effort: settings.effort } : {}),
|
|
80
|
+
promptSizeInBytes: byteLength(call.prompt),
|
|
81
|
+
stateSeq,
|
|
82
|
+
});
|
|
83
|
+
let toolAskedPerson = false;
|
|
84
|
+
let mistake;
|
|
85
|
+
let toolCallsInOrder = Promise.resolve();
|
|
86
|
+
/** Answers the call's tool calls one at a time, in the order the model made them. */
|
|
87
|
+
const callTool = (toolCall) => {
|
|
88
|
+
const answered = toolCallsInOrder.then(async () => {
|
|
89
|
+
// A tool call made together with one that ended the call is not answered.
|
|
90
|
+
callSignal.throwIfAborted();
|
|
91
|
+
try {
|
|
92
|
+
const { result, askedPerson } = await answerToolCall({
|
|
93
|
+
session,
|
|
94
|
+
assignment,
|
|
95
|
+
agent,
|
|
96
|
+
memory,
|
|
97
|
+
callFile,
|
|
98
|
+
callSignal,
|
|
99
|
+
toolCall,
|
|
100
|
+
});
|
|
101
|
+
if (askedPerson) {
|
|
102
|
+
toolAskedPerson = true;
|
|
103
|
+
callEnd.abort();
|
|
104
|
+
}
|
|
105
|
+
// A tool that returns nothing still answers the model.
|
|
106
|
+
return result ?? null;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
// A handler that gave up because the call ended made no mistake.
|
|
110
|
+
if (!callSignal.aborted)
|
|
111
|
+
mistake = error;
|
|
112
|
+
callEnd.abort();
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
toolCallsInOrder = answered.catch(() => undefined);
|
|
117
|
+
return answered;
|
|
118
|
+
};
|
|
119
|
+
const started = performance.now();
|
|
120
|
+
let reply;
|
|
121
|
+
/** What the call's provider requests add up to so far, for a call that ends without a reply. */
|
|
122
|
+
let counted;
|
|
123
|
+
let failure;
|
|
124
|
+
try {
|
|
125
|
+
session.stopSignal.throwIfAborted();
|
|
126
|
+
reply = await session.infer({
|
|
127
|
+
call,
|
|
128
|
+
callTool,
|
|
129
|
+
signal: callSignal,
|
|
130
|
+
onRequestCounted: (soFar) => {
|
|
131
|
+
counted = soFar;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
failure = error;
|
|
137
|
+
}
|
|
138
|
+
// No tool call of this call goes on, or saves anything, after the call's last line.
|
|
139
|
+
callEnd.abort();
|
|
140
|
+
await toolCallsInOrder;
|
|
141
|
+
const durationMs = Math.round(performance.now() - started);
|
|
142
|
+
/** Completes the call's file and adds the line that ends the call. */
|
|
143
|
+
const endCall = async ({ status, failure: reason, retried, }) => {
|
|
144
|
+
const used = reply ?? counted;
|
|
145
|
+
if (used) {
|
|
146
|
+
callFile.reasoning = used.reasoning;
|
|
147
|
+
callFile.tokens = used.tokens;
|
|
148
|
+
}
|
|
149
|
+
if (reply) {
|
|
150
|
+
if (byteLength(reply.replyText) <= MAX_REPLY_BYTES)
|
|
151
|
+
callFile.replyText = reply.replyText;
|
|
152
|
+
if (reply.launcherInstruction)
|
|
153
|
+
callFile.launcherInstruction = reply.launcherInstruction;
|
|
154
|
+
}
|
|
155
|
+
if (reason)
|
|
156
|
+
callFile.failure = reason;
|
|
157
|
+
await session.saveCallFile(callFile);
|
|
158
|
+
await session.record({
|
|
159
|
+
kind: 'call-ended',
|
|
160
|
+
callId,
|
|
161
|
+
status,
|
|
162
|
+
durationMs,
|
|
163
|
+
tokens: used?.tokens ?? {},
|
|
164
|
+
...(used?.costUsd !== undefined ? { costUsd: used.costUsd } : {}),
|
|
165
|
+
...(reply?.programCostUsd !== undefined ? { programCostUsd: reply.programCostUsd } : {}),
|
|
166
|
+
...(reason ? { failure: reason } : {}),
|
|
167
|
+
...(retried ? { retried } : {}),
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
if (mistake) {
|
|
171
|
+
await endCall({ status: 'failed', failure: messageOf(mistake) });
|
|
172
|
+
throw mistake;
|
|
173
|
+
}
|
|
174
|
+
if (toolAskedPerson) {
|
|
175
|
+
await endCall({ status: 'asked' });
|
|
176
|
+
return { ended: 'asked' };
|
|
177
|
+
}
|
|
178
|
+
if (session.stopSignal.aborted) {
|
|
179
|
+
await endCall({ status: 'stopped' });
|
|
180
|
+
throw session.stopSignal.reason;
|
|
181
|
+
}
|
|
182
|
+
if (reply && byteLength(reply.replyText) > MAX_REPLY_BYTES)
|
|
183
|
+
failure = new Error('The reply is over 1 MB.');
|
|
184
|
+
if (!failure && reply) {
|
|
185
|
+
await endCall({ status: 'replied' });
|
|
186
|
+
return { ended: 'replied', callId, replyText: reply.replyText, stateAtPrompt };
|
|
187
|
+
}
|
|
188
|
+
if (failure instanceof ProviderAccessError) {
|
|
189
|
+
await endCall({ status: 'stopped', failure: failure.message });
|
|
190
|
+
throw new RunStopped({ message: failure.message });
|
|
191
|
+
}
|
|
192
|
+
const reason = messageOf(failure);
|
|
193
|
+
const retried = failure instanceof ProviderConnectionError && providerAttempt < PROVIDER_ATTEMPTS;
|
|
194
|
+
await endCall({ status: 'failed', failure: reason, retried });
|
|
195
|
+
if (retried) {
|
|
196
|
+
if (session.drainSignal.aborted)
|
|
197
|
+
return { ended: 'draining' };
|
|
198
|
+
try {
|
|
199
|
+
await wait({
|
|
200
|
+
ms: session.retryWaitsMs[providerAttempt - 1] ?? session.retryWaitsMs.at(-1) ?? 0,
|
|
201
|
+
signal: AbortSignal.any([session.stopSignal, session.drainSignal]),
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
catch (error) {
|
|
205
|
+
if (!session.stopSignal.aborted && session.drainSignal.aborted)
|
|
206
|
+
return { ended: 'draining' };
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (agent.whenCallFails)
|
|
212
|
+
return { ended: 'failed', callId, failure: reason };
|
|
213
|
+
throw new RunFailed({
|
|
214
|
+
message: `${agent.name} for ${assignment.label}: ${reason}`,
|
|
215
|
+
assignmentId: assignment.id,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AgentPhase } from '../../blueprint/phase.js';
|
|
2
|
+
import type { RunSession } from '../run-session.js';
|
|
3
|
+
/** Runs an agent phase at one place: creates each lane's assignments, starts each when it may and
|
|
4
|
+
* its lane has a free slot, and goes on until every one has finished. Lanes run interleaved. After
|
|
5
|
+
* Resume it continues the place's saved assignments. */
|
|
6
|
+
export declare function runAgentPhase<State>({ session, phase, place, whenNothingCanRun, }: {
|
|
7
|
+
session: RunSession<State>;
|
|
8
|
+
phase: AgentPhase<State>;
|
|
9
|
+
/** Where the phase runs: its own name on the trunk, or its place on a side line. */
|
|
10
|
+
place: string;
|
|
11
|
+
/** Called when no assignment is running and none can start. When it returns true something
|
|
12
|
+
* was done that may have made more work for the phase, so the lanes are read again. */
|
|
13
|
+
whenNothingCanRun?: () => Promise<boolean>;
|
|
14
|
+
}): Promise<void>;
|