@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,106 @@
|
|
|
1
|
+
const ALL_ACTIONS = ['create', 'update', 'delete'];
|
|
2
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
3
|
+
function sameValue(left, right) {
|
|
4
|
+
if (left === right)
|
|
5
|
+
return true;
|
|
6
|
+
if (Array.isArray(left) && Array.isArray(right))
|
|
7
|
+
return left.length === right.length && left.every((item, index) => sameValue(item, right[index]));
|
|
8
|
+
if (isRecord(left) && isRecord(right)) {
|
|
9
|
+
const leftKeys = Object.keys(left).filter((key) => left[key] !== undefined);
|
|
10
|
+
const rightKeys = Object.keys(right).filter((key) => right[key] !== undefined);
|
|
11
|
+
return (leftKeys.length === rightKeys.length &&
|
|
12
|
+
leftKeys.every((key) => Object.hasOwn(right, key) && sameValue(left[key], right[key])));
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
/** Plain-language description of a write-scope rule for the Blueprint devtool. */
|
|
17
|
+
export function describeWriteScopeRule(rule) {
|
|
18
|
+
if (rule.description)
|
|
19
|
+
return rule.description;
|
|
20
|
+
if (!rule.records)
|
|
21
|
+
return `Any change to ${rule.path}.`;
|
|
22
|
+
const actions = (rule.records.actions ?? ALL_ACTIONS).join(', ');
|
|
23
|
+
const parts = [`${actions} records in ${rule.path}`];
|
|
24
|
+
if (rule.records.fields?.length)
|
|
25
|
+
parts.push(`fields: ${rule.records.fields.join(', ')}`);
|
|
26
|
+
if (rule.records.forbiddenFields?.length)
|
|
27
|
+
parts.push(`except ${rule.records.forbiddenFields.join(', ')}`);
|
|
28
|
+
if (rule.records.onlyIds)
|
|
29
|
+
parts.push('restricted to allowed ids');
|
|
30
|
+
return `${parts.join(' · ')}.`;
|
|
31
|
+
}
|
|
32
|
+
function indexById({ items, idField }) {
|
|
33
|
+
const indexed = Object.create(null);
|
|
34
|
+
for (const item of items) {
|
|
35
|
+
if (!isRecord(item) || typeof item[idField] !== 'string')
|
|
36
|
+
return undefined;
|
|
37
|
+
indexed[item[idField]] = item;
|
|
38
|
+
}
|
|
39
|
+
return indexed;
|
|
40
|
+
}
|
|
41
|
+
function changedRecordFields({ before, after, }) {
|
|
42
|
+
const keys = new Set([...Object.keys(before), ...Object.keys(after)]);
|
|
43
|
+
const changed = [];
|
|
44
|
+
for (const key of keys)
|
|
45
|
+
if (!sameValue(before[key], after[key]))
|
|
46
|
+
changed.push(key);
|
|
47
|
+
return changed;
|
|
48
|
+
}
|
|
49
|
+
/** Checks a proposed state transition against an agent's `mayChange` rules. Returns a list of
|
|
50
|
+
* human-readable violation messages; empty when every change is within scope. */
|
|
51
|
+
export function writeScopeViolations({ before, after, rules, state, subject, }) {
|
|
52
|
+
if (!isRecord(before) || !isRecord(after))
|
|
53
|
+
return [];
|
|
54
|
+
const violations = [];
|
|
55
|
+
const topKeys = new Set([...Object.keys(before), ...Object.keys(after)]);
|
|
56
|
+
for (const key of topKeys) {
|
|
57
|
+
if (sameValue(before[key], after[key]))
|
|
58
|
+
continue;
|
|
59
|
+
const matching = rules.filter((rule) => rule.path === key);
|
|
60
|
+
if (!matching.length) {
|
|
61
|
+
violations.push(`Changed ${key}, which this agent may not change.`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (matching.some((rule) => !rule.records))
|
|
65
|
+
continue;
|
|
66
|
+
if (!Array.isArray(before[key]) || !Array.isArray(after[key])) {
|
|
67
|
+
violations.push(`Changed ${key} to a non-list value, which its record scope does not allow.`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const idField = matching[0].records.idField ?? 'id';
|
|
71
|
+
const beforeById = indexById({ items: before[key], idField });
|
|
72
|
+
const afterById = indexById({ items: after[key], idField });
|
|
73
|
+
if (!beforeById || !afterById) {
|
|
74
|
+
violations.push(`Changed ${key} with an item missing string ${idField}.`);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const resolvedRules = matching.map((rule) => ({
|
|
78
|
+
actions: rule.records.actions ?? ALL_ACTIONS,
|
|
79
|
+
fields: rule.records.fields,
|
|
80
|
+
forbiddenFields: rule.records.forbiddenFields,
|
|
81
|
+
allowedIds: rule.records.onlyIds ? new Set(rule.records.onlyIds({ state, subject })) : undefined,
|
|
82
|
+
}));
|
|
83
|
+
const ruleAllowsAction = (rule, action, id) => rule.actions.includes(action) && (!rule.allowedIds || rule.allowedIds.has(id));
|
|
84
|
+
for (const id of Object.keys(afterById))
|
|
85
|
+
if (!Object.hasOwn(beforeById, id))
|
|
86
|
+
if (!resolvedRules.some((rule) => ruleAllowsAction(rule, 'create', id)))
|
|
87
|
+
violations.push(`Created ${key} record "${id}", which this agent may not create.`);
|
|
88
|
+
for (const id of Object.keys(beforeById))
|
|
89
|
+
if (!Object.hasOwn(afterById, id))
|
|
90
|
+
if (!resolvedRules.some((rule) => ruleAllowsAction(rule, 'delete', id)))
|
|
91
|
+
violations.push(`Removed ${key} record "${id}", which this agent may not remove.`);
|
|
92
|
+
for (const id of Object.keys(beforeById)) {
|
|
93
|
+
if (!Object.hasOwn(afterById, id))
|
|
94
|
+
continue;
|
|
95
|
+
const fields = changedRecordFields({ before: beforeById[id], after: afterById[id] });
|
|
96
|
+
if (!fields.length)
|
|
97
|
+
continue;
|
|
98
|
+
const allowed = resolvedRules.some((rule) => ruleAllowsAction(rule, 'update', id) &&
|
|
99
|
+
!fields.some((field) => rule.forbiddenFields?.includes(field)) &&
|
|
100
|
+
(!rule.fields || fields.every((field) => rule.fields.includes(field))));
|
|
101
|
+
if (!allowed)
|
|
102
|
+
violations.push(`Updated ${key} record "${id}" (${fields.join(', ')}), which this agent may not change.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return violations;
|
|
106
|
+
}
|
package/dist/command.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs } from 'node:util';
|
|
3
|
+
import { Pool } from 'pg';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { runOrAnswer } from './commands/run-command.js';
|
|
6
|
+
import { loadDagmaConfig } from './config.js';
|
|
7
|
+
import { ensureDagmaSchema, postgresRunStore } from './run/record/postgres-run-store.js';
|
|
8
|
+
const USAGE = `dagma [--port <number>] [--database-url <url>] [--config <file>]
|
|
9
|
+
dagma run <blueprint file> --input <file> [options]
|
|
10
|
+
dagma run <blueprint file> --from-run <run> --checkpoint <number> [options]
|
|
11
|
+
dagma run <blueprint file> --resume <run> [--answer <question>=<text>]... [--overrides <file>]
|
|
12
|
+
dagma answer <blueprint file> <run>
|
|
13
|
+
dagma worker --url <address> --token <secret>
|
|
14
|
+
dagma dev [blueprint file] [--port <number>] [--database-url <url>] [--scripted-replies <file>]
|
|
15
|
+
|
|
16
|
+
--database-url <url> Postgres connection URL (default: DATABASE_URL or dagma.config.ts)
|
|
17
|
+
--config <file> path to dagma.config.ts (default: ./dagma.config.ts)
|
|
18
|
+
--stop-after <phase> stop the first time this phase finishes
|
|
19
|
+
--calls-answered-by <what> scripted (default), codex, claude-code, openai-api, anthropic-api, bedrock-api or gemini-api
|
|
20
|
+
--scripted-replies <file> the replies file of a scripted run
|
|
21
|
+
--questions-answered-by <who> person (default) or recommendation
|
|
22
|
+
--overrides <file> model settings for every call, one phase or one agent
|
|
23
|
+
--print-events print which run it is, then each events line as JSON, and read
|
|
24
|
+
Stop and answers from input
|
|
25
|
+
--port <number> where the admin UI answers on this computer (default: 4490)`;
|
|
26
|
+
/** Answers a run's calls until the run's queue is gone or the worker is told to end. */
|
|
27
|
+
async function work({ address, secret }) {
|
|
28
|
+
const { runWorker } = await import('./worker/worker.js');
|
|
29
|
+
const leave = new AbortController();
|
|
30
|
+
const end = () => {
|
|
31
|
+
leave.abort();
|
|
32
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
33
|
+
};
|
|
34
|
+
for (const signal of ['SIGINT', 'SIGTERM'])
|
|
35
|
+
process.once(signal, end);
|
|
36
|
+
await runWorker({ address, secret, stopSignal: leave.signal });
|
|
37
|
+
}
|
|
38
|
+
/** Serves the Dagma admin UI backed by Postgres until the command is ended. */
|
|
39
|
+
async function serveAdmin({ blueprintFile, databaseUrl, configFile, port, scriptedRepliesFile, }) {
|
|
40
|
+
const loadedConfig = await loadDagmaConfig({ configFile });
|
|
41
|
+
const config = loadedConfig?.config;
|
|
42
|
+
const resolvedUrl = databaseUrl ?? config?.databaseUrl ?? process.env.DATABASE_URL;
|
|
43
|
+
const pool = config?.pool ?? (resolvedUrl ? new Pool({ connectionString: resolvedUrl }) : undefined);
|
|
44
|
+
const ownsPool = !config?.pool;
|
|
45
|
+
if (!pool)
|
|
46
|
+
throw new Error('Dagma admin UI needs a Postgres database: set DATABASE_URL, pass --database-url, or configure dagma.config.ts.');
|
|
47
|
+
await ensureDagmaSchema({ pool });
|
|
48
|
+
const store = postgresRunStore({ pool });
|
|
49
|
+
const { startDevtool } = await import('./devtool-server/devtool-server.js');
|
|
50
|
+
const resolvedPort = z.coerce
|
|
51
|
+
.number()
|
|
52
|
+
.int()
|
|
53
|
+
.positive()
|
|
54
|
+
.parse(port ?? config?.port ?? 4490);
|
|
55
|
+
const devtool = await startDevtool({
|
|
56
|
+
store,
|
|
57
|
+
databaseUrl: resolvedUrl,
|
|
58
|
+
blueprintFile,
|
|
59
|
+
blueprint: config?.blueprints?.[0],
|
|
60
|
+
port: resolvedPort,
|
|
61
|
+
scriptedRepliesFile,
|
|
62
|
+
});
|
|
63
|
+
console.log(`Dagma admin UI is at ${devtool.address}`);
|
|
64
|
+
const end = () => void devtool
|
|
65
|
+
.close()
|
|
66
|
+
.then(() => (ownsPool ? pool.end() : undefined))
|
|
67
|
+
.finally(() => process.exit(0));
|
|
68
|
+
for (const signal of ['SIGINT', 'SIGTERM'])
|
|
69
|
+
process.once(signal, end);
|
|
70
|
+
}
|
|
71
|
+
/** Reads the command's arguments and carries the command out. */
|
|
72
|
+
async function main() {
|
|
73
|
+
const { values, positionals } = parseArgs({
|
|
74
|
+
allowPositionals: true,
|
|
75
|
+
options: {
|
|
76
|
+
'database-url': { type: 'string' },
|
|
77
|
+
config: { type: 'string' },
|
|
78
|
+
input: { type: 'string' },
|
|
79
|
+
'from-run': { type: 'string' },
|
|
80
|
+
checkpoint: { type: 'string' },
|
|
81
|
+
resume: { type: 'string' },
|
|
82
|
+
answer: { type: 'string', multiple: true },
|
|
83
|
+
'stop-after': { type: 'string' },
|
|
84
|
+
'calls-answered-by': { type: 'string', default: 'scripted' },
|
|
85
|
+
'scripted-replies': { type: 'string' },
|
|
86
|
+
'questions-answered-by': { type: 'string', default: 'person' },
|
|
87
|
+
overrides: { type: 'string' },
|
|
88
|
+
'print-events': { type: 'boolean', default: false },
|
|
89
|
+
url: { type: 'string' },
|
|
90
|
+
token: { type: 'string' },
|
|
91
|
+
port: { type: 'string' },
|
|
92
|
+
help: { type: 'boolean', short: 'h', default: false },
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const [command, blueprintFile, runId] = positionals;
|
|
96
|
+
if (values.help) {
|
|
97
|
+
console.log(USAGE);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (command === 'worker') {
|
|
101
|
+
const secret = values.token ?? process.env.DAGMA_QUEUE_SECRET;
|
|
102
|
+
if (!values.url || !secret)
|
|
103
|
+
throw new Error('dagma worker needs --url and --token.');
|
|
104
|
+
return work({ address: values.url, secret });
|
|
105
|
+
}
|
|
106
|
+
if (!command || command === 'dev')
|
|
107
|
+
return serveAdmin({
|
|
108
|
+
blueprintFile,
|
|
109
|
+
databaseUrl: values['database-url'],
|
|
110
|
+
configFile: values.config,
|
|
111
|
+
port: values.port,
|
|
112
|
+
scriptedRepliesFile: values['scripted-replies'],
|
|
113
|
+
});
|
|
114
|
+
if ((command !== 'run' && command !== 'answer') || !blueprintFile) {
|
|
115
|
+
console.log(USAGE);
|
|
116
|
+
process.exitCode = 1;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
const status = await runOrAnswer({
|
|
121
|
+
command,
|
|
122
|
+
blueprintFile,
|
|
123
|
+
runId,
|
|
124
|
+
options: {
|
|
125
|
+
databaseUrl: values['database-url'],
|
|
126
|
+
input: values.input,
|
|
127
|
+
fromRun: values['from-run'],
|
|
128
|
+
checkpoint: values.checkpoint,
|
|
129
|
+
resume: values.resume,
|
|
130
|
+
answers: values.answer ?? [],
|
|
131
|
+
stopAfter: values['stop-after'],
|
|
132
|
+
callsAnsweredBy: values['calls-answered-by'],
|
|
133
|
+
scriptedReplies: values['scripted-replies'],
|
|
134
|
+
questionsAnsweredBy: values['questions-answered-by'],
|
|
135
|
+
overrides: values.overrides,
|
|
136
|
+
printEvents: values['print-events'],
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
process.exitCode = status === 'failed' ? 1 : 0;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
process.stdin.destroy();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
main().catch((error) => {
|
|
146
|
+
console.error(error instanceof Error ? error.message : error);
|
|
147
|
+
process.exitCode = 1;
|
|
148
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CallsAnsweredBy } from '../blueprint/run-settings.js';
|
|
2
|
+
import type { Infer } from '../inference/inference.js';
|
|
3
|
+
/** What answers a run's model calls for as long as the run executes. */
|
|
4
|
+
export type CallsAnswering = {
|
|
5
|
+
infer: Infer;
|
|
6
|
+
/** Ends it: closes the call queue and stops the run's own worker, when there are any. */
|
|
7
|
+
close: () => Promise<void>;
|
|
8
|
+
/** How to start another worker for this run by hand, when a call queue answers its calls. */
|
|
9
|
+
anotherWorkerLine?: string;
|
|
10
|
+
};
|
|
11
|
+
/** Starts whatever answers a run's model calls. For a run answered by Codex or Claude Code it
|
|
12
|
+
* opens the run's call queue and starts the run's own worker. */
|
|
13
|
+
export declare function startAnsweringCalls({ callsAnsweredBy, scriptedRepliesFile, }: {
|
|
14
|
+
callsAnsweredBy: CallsAnsweredBy;
|
|
15
|
+
scriptedRepliesFile?: string;
|
|
16
|
+
}): Promise<CallsAnswering>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { openCallQueue } from '../inference/call-queue.js';
|
|
2
|
+
import { anthropicApiInference } from '../inference/anthropic-api.js';
|
|
3
|
+
import { bedrockApiInference } from '../inference/bedrock-api.js';
|
|
4
|
+
import { geminiApiInference } from '../inference/gemini-api.js';
|
|
5
|
+
import { openAiApiInference } from '../inference/openai-api.js';
|
|
6
|
+
import { startOwnWorker } from '../inference/own-worker.js';
|
|
7
|
+
import { ScriptedRepliesSchema, scriptedInference } from '../inference/scripted.js';
|
|
8
|
+
import { readJsonFile } from './json-file.js';
|
|
9
|
+
/** Starts whatever answers a run's model calls. For a run answered by Codex or Claude Code it
|
|
10
|
+
* opens the run's call queue and starts the run's own worker. */
|
|
11
|
+
export async function startAnsweringCalls({ callsAnsweredBy, scriptedRepliesFile, }) {
|
|
12
|
+
if (callsAnsweredBy === 'scripted') {
|
|
13
|
+
if (!scriptedRepliesFile)
|
|
14
|
+
throw new Error('A scripted run needs --scripted-replies <file>.');
|
|
15
|
+
const replies = await readJsonFile({ file: scriptedRepliesFile, schema: ScriptedRepliesSchema });
|
|
16
|
+
return { infer: scriptedInference({ replies }), close: async () => { } };
|
|
17
|
+
}
|
|
18
|
+
if (callsAnsweredBy === 'openai-api') {
|
|
19
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
20
|
+
if (!apiKey)
|
|
21
|
+
throw new Error('A run answered by the OpenAI API needs OPENAI_API_KEY set.');
|
|
22
|
+
return { infer: openAiApiInference({ apiKey }), close: async () => { } };
|
|
23
|
+
}
|
|
24
|
+
if (callsAnsweredBy === 'anthropic-api') {
|
|
25
|
+
const apiKey = process.env.ANTHROPIC_API_KEY;
|
|
26
|
+
if (!apiKey)
|
|
27
|
+
throw new Error('A run answered by the Anthropic API needs ANTHROPIC_API_KEY set.');
|
|
28
|
+
return { infer: anthropicApiInference({ apiKey }), close: async () => { } };
|
|
29
|
+
}
|
|
30
|
+
if (callsAnsweredBy === 'bedrock-api') {
|
|
31
|
+
const region = process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION;
|
|
32
|
+
return {
|
|
33
|
+
infer: bedrockApiInference(region ? { region } : {}),
|
|
34
|
+
close: async () => { },
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (callsAnsweredBy === 'gemini-api') {
|
|
38
|
+
const project = process.env.GOOGLE_CLOUD_PROJECT ?? process.env.VERTEX_AI_PROJECT;
|
|
39
|
+
const apiKey = process.env.GEMINI_API_KEY;
|
|
40
|
+
if (project) {
|
|
41
|
+
const location = process.env.GOOGLE_CLOUD_LOCATION ?? 'global';
|
|
42
|
+
return {
|
|
43
|
+
infer: geminiApiInference({ vertexai: true, project, location }),
|
|
44
|
+
close: async () => { },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
if (!apiKey)
|
|
48
|
+
throw new Error('A run answered by the Gemini API needs GOOGLE_CLOUD_PROJECT (for Vertex AI) or GEMINI_API_KEY set.');
|
|
49
|
+
return { infer: geminiApiInference({ apiKey }), close: async () => { } };
|
|
50
|
+
}
|
|
51
|
+
if (callsAnsweredBy === 'custom')
|
|
52
|
+
throw new Error('Custom inference is supplied by the embedding host, not by the Dagma CLI.');
|
|
53
|
+
const queue = await openCallQueue({ program: callsAnsweredBy });
|
|
54
|
+
const stopOwnWorker = startOwnWorker({ address: queue.address, secret: queue.secret });
|
|
55
|
+
return {
|
|
56
|
+
infer: queue.infer,
|
|
57
|
+
anotherWorkerLine: `Calls wait at ${queue.address}. Another worker: pnpm dagma worker --url ${queue.address} --token ${queue.secret}`,
|
|
58
|
+
close: async () => {
|
|
59
|
+
stopOwnWorker();
|
|
60
|
+
await queue.close();
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
/** Reads a JSON file and checks it against a schema, naming the file when it does not fit. */
|
|
3
|
+
export declare function readJsonFile<Value>({ file, schema, }: {
|
|
4
|
+
file: string;
|
|
5
|
+
schema: z.ZodType<Value, z.ZodTypeDef, unknown>;
|
|
6
|
+
}): Promise<Value>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
/** Reads a JSON file and checks it against a schema, naming the file when it does not fit. */
|
|
4
|
+
export async function readJsonFile({ file, schema, }) {
|
|
5
|
+
const text = await readFile(resolve(file), 'utf8');
|
|
6
|
+
let value;
|
|
7
|
+
try {
|
|
8
|
+
value = JSON.parse(text);
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
throw new Error(`${file} is not JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
12
|
+
}
|
|
13
|
+
const parsed = schema.safeParse(value);
|
|
14
|
+
if (!parsed.success)
|
|
15
|
+
throw new Error(`${file} does not have the shape expected: ${parsed.error.issues
|
|
16
|
+
.map((issue) => `${issue.path.join('.') || 'file'}: ${issue.message}`)
|
|
17
|
+
.join('; ')}`);
|
|
18
|
+
return parsed.data;
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** The options of `dagma run` and `dagma answer`, as typed. */
|
|
2
|
+
export type RunCommandOptions = {
|
|
3
|
+
databaseUrl?: string;
|
|
4
|
+
input?: string;
|
|
5
|
+
fromRun?: string;
|
|
6
|
+
checkpoint?: string;
|
|
7
|
+
resume?: string;
|
|
8
|
+
/** Each `--answer`, as `<question>=<text>`. */
|
|
9
|
+
answers: string[];
|
|
10
|
+
stopAfter?: string;
|
|
11
|
+
callsAnsweredBy: string;
|
|
12
|
+
scriptedReplies?: string;
|
|
13
|
+
questionsAnsweredBy: string;
|
|
14
|
+
overrides?: string;
|
|
15
|
+
printEvents: boolean;
|
|
16
|
+
};
|
|
17
|
+
/** Carries out `dagma run` or `dagma answer` backed by Postgres (`--database-url` or `DATABASE_URL`). */
|
|
18
|
+
export declare function runOrAnswer({ command, blueprintFile, runId, options, }: {
|
|
19
|
+
command: 'run' | 'answer';
|
|
20
|
+
blueprintFile: string;
|
|
21
|
+
runId?: string;
|
|
22
|
+
options: RunCommandOptions;
|
|
23
|
+
}): Promise<import("../index.js").RunStatus>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { Pool } from 'pg';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { loadBlueprint } from '../blueprint/load-blueprint.js';
|
|
6
|
+
import { createRun } from '../run/create-run.js';
|
|
7
|
+
import { executeRun } from '../run/execute-run.js';
|
|
8
|
+
import { ensureDagmaSchema, postgresRunStore } from '../run/record/postgres-run-store.js';
|
|
9
|
+
import { resumeRun } from '../run/resume-run.js';
|
|
10
|
+
import { RunStopped } from '../run/run-endings.js';
|
|
11
|
+
import { CallsAnsweredBySchema, ModelOverridesSchema, QuestionsAnsweredBySchema, RunInputLineSchema, } from '../run/run-option-shapes.js';
|
|
12
|
+
import { startAnsweringCalls } from './calls-answering.js';
|
|
13
|
+
import { readJsonFile } from './json-file.js';
|
|
14
|
+
import { answersTypedInTerminal } from './typed-answers.js';
|
|
15
|
+
/** One readable line for an events line worth showing in a terminal; undefined for the rest. */
|
|
16
|
+
function terminalLine(event) {
|
|
17
|
+
switch (event.kind) {
|
|
18
|
+
case 'phase-started':
|
|
19
|
+
return event.phase;
|
|
20
|
+
case 'assignment-started':
|
|
21
|
+
return ` started ${event.agents.join(' → ')}`;
|
|
22
|
+
case 'reply-judged':
|
|
23
|
+
return ` ${event.callId} ${event.outcome}${event.correction ? `: ${event.correction}` : ''}`;
|
|
24
|
+
case 'call-ended':
|
|
25
|
+
return event.status === 'failed' ? ` ${event.callId} failed: ${event.failure}` : undefined;
|
|
26
|
+
case 'side-line-checked':
|
|
27
|
+
return event.entered ? ` onto ${event.sideLine}` : undefined;
|
|
28
|
+
case 'side-line-left':
|
|
29
|
+
return ` back from ${event.sideLine}`;
|
|
30
|
+
case 'run-waiting':
|
|
31
|
+
return 'Waiting for the person.';
|
|
32
|
+
case 'run-stopped':
|
|
33
|
+
return `Stopped. ${event.reason}`;
|
|
34
|
+
case 'run-failed':
|
|
35
|
+
return `Failed. ${event.reason}`;
|
|
36
|
+
case 'run-finished':
|
|
37
|
+
return 'Finished.';
|
|
38
|
+
default:
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Stop and answers reach an executing run through this process: Ctrl-C in a terminal, or one
|
|
43
|
+
* JSON line each on input when the devtool started it. */
|
|
44
|
+
function listenForStopAndAnswers({ session, fromInput, }) {
|
|
45
|
+
const stop = () => session.endWorkInProgress({ reason: new RunStopped({ message: 'Stopped by the person.' }) });
|
|
46
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP'])
|
|
47
|
+
process.once(signal, stop);
|
|
48
|
+
if (!fromInput)
|
|
49
|
+
return;
|
|
50
|
+
createInterface({ input: process.stdin }).on('line', (line) => {
|
|
51
|
+
let message;
|
|
52
|
+
try {
|
|
53
|
+
message = RunInputLineSchema.parse(JSON.parse(line));
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
console.error(`Ignored a line on input that is neither a Stop nor an answer: ${line}`);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if ('stop' in message)
|
|
60
|
+
return stop();
|
|
61
|
+
session.recordAnswer({ ...message.answer, answeredBy: 'person' }).catch((error) => {
|
|
62
|
+
console.error(error instanceof Error ? error.message : error);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/** Carries out `dagma run` or `dagma answer` backed by Postgres (`--database-url` or `DATABASE_URL`). */
|
|
67
|
+
export async function runOrAnswer({ command, blueprintFile, runId, options, }) {
|
|
68
|
+
const connectionString = options.databaseUrl ?? process.env.DATABASE_URL;
|
|
69
|
+
if (!connectionString)
|
|
70
|
+
throw new Error('DATABASE_URL (or --database-url) is required to store Dagma runs in Postgres.');
|
|
71
|
+
const pool = new Pool({ connectionString });
|
|
72
|
+
await ensureDagmaSchema({ pool });
|
|
73
|
+
const store = postgresRunStore({ pool });
|
|
74
|
+
const loaded = await loadBlueprint({ file: blueprintFile });
|
|
75
|
+
const { printEvents } = options;
|
|
76
|
+
const overrides = options.overrides
|
|
77
|
+
? await readJsonFile({ file: options.overrides, schema: ModelOverridesSchema })
|
|
78
|
+
: undefined;
|
|
79
|
+
const scriptedRepliesFile = options.scriptedReplies && resolve(options.scriptedReplies);
|
|
80
|
+
let heldEvents = printEvents ? [] : undefined;
|
|
81
|
+
const onEvent = (event) => {
|
|
82
|
+
if (heldEvents)
|
|
83
|
+
return void heldEvents.push(event);
|
|
84
|
+
const line = printEvents ? JSON.stringify(event) : terminalLine(event);
|
|
85
|
+
if (line)
|
|
86
|
+
console.log(line);
|
|
87
|
+
};
|
|
88
|
+
const resumed = command === 'answer' ? runId : options.resume;
|
|
89
|
+
let answering;
|
|
90
|
+
if (command === 'answer' && !runId)
|
|
91
|
+
throw new Error('dagma answer needs the run to answer.');
|
|
92
|
+
try {
|
|
93
|
+
let session;
|
|
94
|
+
if (resumed) {
|
|
95
|
+
const started = (await store.readEvents({ runId: resumed })).find((event) => event.kind === 'run-started');
|
|
96
|
+
if (started?.kind !== 'run-started')
|
|
97
|
+
throw new Error(`Run ${resumed} was never started; there is nothing to resume.`);
|
|
98
|
+
if (started.callsAnsweredBy === 'custom')
|
|
99
|
+
throw new Error('Runs with custom host inference must be resumed from their host application.');
|
|
100
|
+
answering = await startAnsweringCalls({
|
|
101
|
+
callsAnsweredBy: started.callsAnsweredBy,
|
|
102
|
+
scriptedRepliesFile: scriptedRepliesFile || started.scriptedRepliesFile,
|
|
103
|
+
});
|
|
104
|
+
const answers = command === 'answer'
|
|
105
|
+
? await answersTypedInTerminal({ store, runId: resumed })
|
|
106
|
+
: options.answers.map((given) => {
|
|
107
|
+
if (!given.includes('='))
|
|
108
|
+
throw new Error(`--answer takes <question>=<text>: ${given}`);
|
|
109
|
+
return {
|
|
110
|
+
questionId: given.slice(0, given.indexOf('=')),
|
|
111
|
+
answer: given.slice(given.indexOf('=') + 1),
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
session = await resumeRun({
|
|
115
|
+
blueprint: loaded,
|
|
116
|
+
store,
|
|
117
|
+
runId: resumed,
|
|
118
|
+
answers,
|
|
119
|
+
overrides,
|
|
120
|
+
infer: answering.infer,
|
|
121
|
+
onEvent,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const callsAnsweredBy = CallsAnsweredBySchema.parse(options.callsAnsweredBy);
|
|
126
|
+
const questionsAnsweredBy = QuestionsAnsweredBySchema.parse(options.questionsAnsweredBy);
|
|
127
|
+
const { fromRun } = options;
|
|
128
|
+
if (!options.input && !(fromRun && options.checkpoint))
|
|
129
|
+
throw new Error('A run starts from --input <file>, or from --from-run <run> --checkpoint <number>.');
|
|
130
|
+
let from;
|
|
131
|
+
if (fromRun) {
|
|
132
|
+
const names = await store.listCheckpoints({ runId: fromRun });
|
|
133
|
+
const checkpoint = names.find((name) => name === options.checkpoint || name.startsWith(`${options.checkpoint}-`));
|
|
134
|
+
if (!checkpoint)
|
|
135
|
+
throw new Error(`Run ${fromRun} has no checkpoint ${options.checkpoint} (${names.join(', ')}).`);
|
|
136
|
+
from = { runId: fromRun, checkpoint };
|
|
137
|
+
}
|
|
138
|
+
answering = await startAnsweringCalls({ callsAnsweredBy, scriptedRepliesFile });
|
|
139
|
+
session = await createRun({
|
|
140
|
+
blueprint: loaded,
|
|
141
|
+
store,
|
|
142
|
+
...(from
|
|
143
|
+
? { from }
|
|
144
|
+
: { input: await readJsonFile({ file: options.input, schema: z.unknown() }) }),
|
|
145
|
+
stopAfter: options.stopAfter,
|
|
146
|
+
callsAnsweredBy,
|
|
147
|
+
scriptedRepliesFile: scriptedRepliesFile || undefined,
|
|
148
|
+
questionsAnsweredBy,
|
|
149
|
+
overrides,
|
|
150
|
+
infer: answering.infer,
|
|
151
|
+
onEvent,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (heldEvents) {
|
|
155
|
+
const held = heldEvents;
|
|
156
|
+
heldEvents = undefined;
|
|
157
|
+
console.log(JSON.stringify({ runId: session.runId }));
|
|
158
|
+
held.forEach(onEvent);
|
|
159
|
+
}
|
|
160
|
+
const tell = printEvents ? console.error : console.log;
|
|
161
|
+
if (!printEvents)
|
|
162
|
+
tell(`Run ${session.runId}`);
|
|
163
|
+
if (answering.anotherWorkerLine)
|
|
164
|
+
tell(answering.anotherWorkerLine);
|
|
165
|
+
listenForStopAndAnswers({ session, fromInput: printEvents });
|
|
166
|
+
return await executeRun({ session });
|
|
167
|
+
}
|
|
168
|
+
finally {
|
|
169
|
+
await answering?.close();
|
|
170
|
+
await pool.end();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RunStore } from '../run/record/run-store.js';
|
|
2
|
+
/** Prints the run's waiting questions and reads a typed answer for each. Nothing is recorded
|
|
3
|
+
* unless every question got an answer. */
|
|
4
|
+
export declare function answersTypedInTerminal({ store, runId, }: {
|
|
5
|
+
store: RunStore;
|
|
6
|
+
runId: string;
|
|
7
|
+
}): Promise<{
|
|
8
|
+
questionId: string;
|
|
9
|
+
answer: string;
|
|
10
|
+
}[]>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import { progressFromEvents } from '../run/record/apply-event.js';
|
|
3
|
+
import { START_CHECKPOINT_NAME } from '../run/record/progress.js';
|
|
4
|
+
/** Prints the run's waiting questions and reads a typed answer for each. Nothing is recorded
|
|
5
|
+
* unless every question got an answer. */
|
|
6
|
+
export async function answersTypedInTerminal({ store, runId, }) {
|
|
7
|
+
const start = await store.readCheckpoint({ runId, name: START_CHECKPOINT_NAME });
|
|
8
|
+
if (!start)
|
|
9
|
+
throw new Error(`Run ${runId} is missing its starting checkpoint.`);
|
|
10
|
+
const progress = progressFromEvents({
|
|
11
|
+
start,
|
|
12
|
+
events: await store.readEvents({ runId }),
|
|
13
|
+
});
|
|
14
|
+
const waiting = progress.questionIds
|
|
15
|
+
.map((id) => progress.questions[id])
|
|
16
|
+
.filter((question) => question.answer === undefined);
|
|
17
|
+
if (!waiting.length)
|
|
18
|
+
throw new Error(`Run ${runId} has no waiting question.`);
|
|
19
|
+
const terminal = createInterface({ input: process.stdin, output: process.stdout, prompt: '> ' });
|
|
20
|
+
// Taken before anything is awaited, so lines piped in at once are kept until asked for.
|
|
21
|
+
const typedLines = terminal[Symbol.asyncIterator]();
|
|
22
|
+
const answers = [];
|
|
23
|
+
for (const { id, question } of waiting) {
|
|
24
|
+
console.log(`\n${question.text}`);
|
|
25
|
+
for (const choice of question.choices ?? []) {
|
|
26
|
+
const recommended = question.recommended?.choice === choice;
|
|
27
|
+
console.log(` - ${choice}${recommended ? ` (recommended: ${question.recommended.reason})` : ''}`);
|
|
28
|
+
}
|
|
29
|
+
let answer = '';
|
|
30
|
+
while (!answer) {
|
|
31
|
+
terminal.prompt();
|
|
32
|
+
const typed = await typedLines.next();
|
|
33
|
+
if (typed.done) {
|
|
34
|
+
terminal.close();
|
|
35
|
+
throw new Error('Input ended before every question was answered; nothing was recorded.');
|
|
36
|
+
}
|
|
37
|
+
answer = typed.value.trim();
|
|
38
|
+
}
|
|
39
|
+
answers.push({ questionId: id, answer });
|
|
40
|
+
}
|
|
41
|
+
terminal.close();
|
|
42
|
+
return answers;
|
|
43
|
+
}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Pool } from 'pg';
|
|
2
|
+
import type { Blueprint } from './blueprint/phase.js';
|
|
3
|
+
import type { Infer } from './inference/inference.js';
|
|
4
|
+
export type DagmaConfig = {
|
|
5
|
+
/** Postgres connection URL (defaults to `process.env.DATABASE_URL`). */
|
|
6
|
+
databaseUrl?: string;
|
|
7
|
+
/** Optional pre-configured `pg.Pool` instead of `databaseUrl`. */
|
|
8
|
+
pool?: Pool;
|
|
9
|
+
/** Blueprints available for inspection and starting/resuming runs from `npx dagma`. */
|
|
10
|
+
blueprints?: Blueprint<any, any, any>[];
|
|
11
|
+
/** Optional host inference implementation for starting/resuming runs from `npx dagma`. */
|
|
12
|
+
infer?: Infer;
|
|
13
|
+
/** Optional host services passed into code phases when starting/resuming runs from `npx dagma`. */
|
|
14
|
+
services?: unknown;
|
|
15
|
+
/** Default port for `npx dagma` admin server (default: 4490). */
|
|
16
|
+
port?: number;
|
|
17
|
+
};
|
|
18
|
+
/** Identity helper providing type-checking for `dagma.config.ts`. */
|
|
19
|
+
export declare function defineDagmaConfig(config: DagmaConfig): DagmaConfig;
|
|
20
|
+
/** Finds and loads `dagma.config.*` in `cwd` (or returns undefined if none exists). */
|
|
21
|
+
export declare function loadDagmaConfig({ cwd, configFile, }?: {
|
|
22
|
+
cwd?: string;
|
|
23
|
+
configFile?: string;
|
|
24
|
+
}): Promise<{
|
|
25
|
+
path: string;
|
|
26
|
+
config: DagmaConfig;
|
|
27
|
+
} | undefined>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
/** Identity helper providing type-checking for `dagma.config.ts`. */
|
|
5
|
+
export function defineDagmaConfig(config) {
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
const CONFIG_FILENAMES = ['dagma.config.ts', 'dagma.config.mts', 'dagma.config.mjs', 'dagma.config.js'];
|
|
9
|
+
/** Finds and loads `dagma.config.*` in `cwd` (or returns undefined if none exists). */
|
|
10
|
+
export async function loadDagmaConfig({ cwd = process.cwd(), configFile, } = {}) {
|
|
11
|
+
const target = configFile
|
|
12
|
+
? resolve(cwd, configFile)
|
|
13
|
+
: CONFIG_FILENAMES.map((name) => resolve(cwd, name)).find((candidate) => existsSync(candidate));
|
|
14
|
+
if (!target)
|
|
15
|
+
return undefined;
|
|
16
|
+
const loaded = (await import(pathToFileURL(target).href));
|
|
17
|
+
if (!loaded.default || typeof loaded.default !== 'object')
|
|
18
|
+
throw new Error(`${target} must export a Dagma config as its default export.`);
|
|
19
|
+
return { path: target, config: loaded.default };
|
|
20
|
+
}
|