@praudent/dagma 0.0.0-stage → 0.1.0
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 +39 -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,58 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { defineRunState } from '../index.js';
|
|
3
|
+
export const SpeciesSchema = z
|
|
4
|
+
.object({ id: z.string(), name: z.string(), subspecies: z.string().array() })
|
|
5
|
+
.strict();
|
|
6
|
+
export const DraftSchema = z.object({ speciesId: z.string(), text: z.string() }).strict();
|
|
7
|
+
export const EntrySchema = z
|
|
8
|
+
.object({
|
|
9
|
+
speciesId: z.string(),
|
|
10
|
+
name: z.string(),
|
|
11
|
+
text: z.string(),
|
|
12
|
+
/** Names of the other species the text mentions. */
|
|
13
|
+
mentions: z.string().array(),
|
|
14
|
+
/** Null until the cross-references phase has written them. */
|
|
15
|
+
seeAlso: z.string().array().nullable(),
|
|
16
|
+
})
|
|
17
|
+
.strict();
|
|
18
|
+
export const FieldGuideSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
topic: z.string(),
|
|
21
|
+
species: SpeciesSchema.array(),
|
|
22
|
+
drafts: DraftSchema.array(),
|
|
23
|
+
entries: EntrySchema.array(),
|
|
24
|
+
/** Names the gap finder says the guide lacks; the species author lists them and empties this. */
|
|
25
|
+
missingSpecies: z.string().array(),
|
|
26
|
+
title: z.string().nullable(),
|
|
27
|
+
})
|
|
28
|
+
.strict();
|
|
29
|
+
export const { defineBlueprint, defineSideLine, defineAgentPhase, defineLane, defineCodePhase, defineAgent, defineTool, accept, ask, } = defineRunState({ schema: FieldGuideSchema });
|
|
30
|
+
export function speciesWithoutEntry({ state }) {
|
|
31
|
+
return state.species.filter((species) => !state.entries.some((entry) => entry.speciesId === species.id));
|
|
32
|
+
}
|
|
33
|
+
/** The species on the list that have neither a draft nor an entry: added after the drafts were
|
|
34
|
+
* written. */
|
|
35
|
+
export function speciesNotStarted({ state }) {
|
|
36
|
+
return speciesWithoutEntry({ state }).filter((species) => !state.drafts.some((draft) => draft.speciesId === species.id));
|
|
37
|
+
}
|
|
38
|
+
export function draftsOf({ state, species }) {
|
|
39
|
+
return state.drafts.filter((draft) => draft.speciesId === species.id);
|
|
40
|
+
}
|
|
41
|
+
/** The entries this entry mentions that stand before it in the guide. */
|
|
42
|
+
export function entriesMentionedBefore({ state, entry }) {
|
|
43
|
+
const position = state.entries.findIndex((other) => other.speciesId === entry.speciesId);
|
|
44
|
+
return state.entries.slice(0, position).filter((other) => entry.mentions.includes(other.name));
|
|
45
|
+
}
|
|
46
|
+
export function speciesNamed({ name, subspecies, }) {
|
|
47
|
+
return { id: name.toLowerCase().replaceAll(/[^a-z0-9]+/g, '-'), name, subspecies };
|
|
48
|
+
}
|
|
49
|
+
export function withEntry({ state, species, entry, }) {
|
|
50
|
+
const added = {
|
|
51
|
+
speciesId: species.id,
|
|
52
|
+
name: species.name,
|
|
53
|
+
text: entry.text,
|
|
54
|
+
mentions: entry.mentions,
|
|
55
|
+
seeAlso: null,
|
|
56
|
+
};
|
|
57
|
+
return { ...state, entries: [...state.entries, added] };
|
|
58
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { z } from 'zod';
|
|
2
|
+
import { type Correction } from '../index.js';
|
|
3
|
+
import type { Draft, Entry, Species } from './run-state.js';
|
|
4
|
+
export declare const topicText: import("../index.js").Serializer<{
|
|
5
|
+
topic: string;
|
|
6
|
+
}>;
|
|
7
|
+
export declare const namesText: import("../index.js").Serializer<{
|
|
8
|
+
names: string[];
|
|
9
|
+
}>;
|
|
10
|
+
export declare const speciesText: import("../index.js").Serializer<{
|
|
11
|
+
species: Species;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const draftsText: import("../index.js").Serializer<{
|
|
14
|
+
drafts: Draft[];
|
|
15
|
+
}>;
|
|
16
|
+
export declare const draftAngleText: import("../index.js").Serializer<{
|
|
17
|
+
repeat: number;
|
|
18
|
+
}>;
|
|
19
|
+
export declare const entriesText: import("../index.js").Serializer<{
|
|
20
|
+
entries: Entry[];
|
|
21
|
+
}>;
|
|
22
|
+
export declare const correctionText: import("../index.js").Serializer<{
|
|
23
|
+
correction: Correction | undefined;
|
|
24
|
+
}>;
|
|
25
|
+
/** The correction wording for every agent of the example that replies in JSON. */
|
|
26
|
+
export declare function jsonReplyCorrection({ issues, schema, }: {
|
|
27
|
+
replyText: string;
|
|
28
|
+
issues: z.ZodIssue[];
|
|
29
|
+
schema: {
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
};
|
|
32
|
+
}): string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { defineSerializer, formatJsonReplyCorrection } from '../index.js';
|
|
2
|
+
export const topicText = defineSerializer({
|
|
3
|
+
name: 'topic',
|
|
4
|
+
description: 'The topic of the guide as the person typed it.',
|
|
5
|
+
render: ({ topic }) => topic,
|
|
6
|
+
});
|
|
7
|
+
export const namesText = defineSerializer({
|
|
8
|
+
name: 'names',
|
|
9
|
+
description: 'Names, one per line; "None." when there are none.',
|
|
10
|
+
render: ({ names }) => names.join('\n') || 'None.',
|
|
11
|
+
});
|
|
12
|
+
export const speciesText = defineSerializer({
|
|
13
|
+
name: 'species',
|
|
14
|
+
description: 'One species: its name, then its subspecies, one per line.',
|
|
15
|
+
render: ({ species }) => [species.name, ...species.subspecies].join('\n'),
|
|
16
|
+
});
|
|
17
|
+
export const draftsText = defineSerializer({
|
|
18
|
+
name: 'drafts',
|
|
19
|
+
description: 'Each draft of the species, numbered, as written.',
|
|
20
|
+
render: ({ drafts }) => drafts.map((draft, index) => `Draft ${index + 1}\n${draft.text}`).join('\n\n'),
|
|
21
|
+
});
|
|
22
|
+
export const draftAngleText = defineSerializer({
|
|
23
|
+
name: 'draft-angle',
|
|
24
|
+
description: 'What this draft leads with: identification for the first, habitat for the second.',
|
|
25
|
+
render: ({ repeat }) => repeat === 1 ? 'Lead with how to identify it.' : 'Lead with where it lives.',
|
|
26
|
+
});
|
|
27
|
+
export const entriesText = defineSerializer({
|
|
28
|
+
name: 'entries',
|
|
29
|
+
description: 'Entries in full: the species name, then the text; "None." when there are none.',
|
|
30
|
+
render: ({ entries }) => entries.map((entry) => `${entry.name}\n${entry.text}`).join('\n\n') || 'None.',
|
|
31
|
+
});
|
|
32
|
+
export const correctionText = defineSerializer({
|
|
33
|
+
name: 'correction',
|
|
34
|
+
description: 'The reply that was not accepted and what to fix; nothing on a first call.',
|
|
35
|
+
render: ({ correction }) => correction
|
|
36
|
+
? `Your last reply was not accepted.\n${correction.text}\n\nYour last reply:\n${correction.rejectedReply}`
|
|
37
|
+
: '',
|
|
38
|
+
});
|
|
39
|
+
/** The correction wording for every agent of the example that replies in JSON. */
|
|
40
|
+
export function jsonReplyCorrection({ issues, schema, }) {
|
|
41
|
+
return formatJsonReplyCorrection({ issues, schema });
|
|
42
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare const speciesAuthor: import("../index.js").Agent<{
|
|
2
|
+
entries: {
|
|
3
|
+
name: string;
|
|
4
|
+
text: string;
|
|
5
|
+
speciesId: string;
|
|
6
|
+
mentions: string[];
|
|
7
|
+
seeAlso: string[] | null;
|
|
8
|
+
}[];
|
|
9
|
+
topic: string;
|
|
10
|
+
species: {
|
|
11
|
+
name: string;
|
|
12
|
+
id: string;
|
|
13
|
+
subspecies: string[];
|
|
14
|
+
}[];
|
|
15
|
+
drafts: {
|
|
16
|
+
text: string;
|
|
17
|
+
speciesId: string;
|
|
18
|
+
}[];
|
|
19
|
+
missingSpecies: string[];
|
|
20
|
+
title: string | null;
|
|
21
|
+
}, unknown, {
|
|
22
|
+
species: {
|
|
23
|
+
name: string;
|
|
24
|
+
subspecies: string[];
|
|
25
|
+
}[];
|
|
26
|
+
}>;
|
|
27
|
+
export declare const speciesList: import("../index.js").Phase<{
|
|
28
|
+
entries: {
|
|
29
|
+
name: string;
|
|
30
|
+
text: string;
|
|
31
|
+
speciesId: string;
|
|
32
|
+
mentions: string[];
|
|
33
|
+
seeAlso: string[] | null;
|
|
34
|
+
}[];
|
|
35
|
+
topic: string;
|
|
36
|
+
species: {
|
|
37
|
+
name: string;
|
|
38
|
+
id: string;
|
|
39
|
+
subspecies: string[];
|
|
40
|
+
}[];
|
|
41
|
+
drafts: {
|
|
42
|
+
text: string;
|
|
43
|
+
speciesId: string;
|
|
44
|
+
}[];
|
|
45
|
+
missingSpecies: string[];
|
|
46
|
+
title: string | null;
|
|
47
|
+
}, unknown>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { jsonReply, sendBack } from '../index.js';
|
|
3
|
+
import { accept, defineAgent, defineAgentPhase, speciesNamed } from './run-state.js';
|
|
4
|
+
import { correctionText, jsonReplyCorrection, namesText, topicText } from './serializers.js';
|
|
5
|
+
// species-list: once; species-author then species-reviewer.
|
|
6
|
+
// Shows: the reviewer's prompt shows what the author added.
|
|
7
|
+
const SpeciesAuthorReply = z
|
|
8
|
+
.object({ species: z.object({ name: z.string(), subspecies: z.string().array() }).array() })
|
|
9
|
+
.strict();
|
|
10
|
+
export const speciesAuthor = defineAgent({
|
|
11
|
+
name: 'species-author',
|
|
12
|
+
description: 'Lists the species the guide covers.',
|
|
13
|
+
prompt: 'species-author.md',
|
|
14
|
+
inputs: {
|
|
15
|
+
topic: {
|
|
16
|
+
description: 'The topic the person gave.',
|
|
17
|
+
serializer: topicText,
|
|
18
|
+
from: ({ state }) => ({ topic: state.topic }),
|
|
19
|
+
},
|
|
20
|
+
correction: {
|
|
21
|
+
description: 'The reply that was sent back and why, on a call after one.',
|
|
22
|
+
serializer: correctionText,
|
|
23
|
+
from: ({ correction }) => ({ correction }),
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
replyReader: jsonReply({ schema: SpeciesAuthorReply, correctionFor: jsonReplyCorrection }),
|
|
27
|
+
replyOutcome: {
|
|
28
|
+
description: 'Lists at least one species. Adds species only.',
|
|
29
|
+
decide: ({ reply }) => reply.species.length === 0
|
|
30
|
+
? sendBack({ correction: 'List at least one species.' })
|
|
31
|
+
: accept({
|
|
32
|
+
change: (current) => ({
|
|
33
|
+
...current,
|
|
34
|
+
species: [...current.species, ...reply.species.map(speciesNamed)],
|
|
35
|
+
}),
|
|
36
|
+
}),
|
|
37
|
+
},
|
|
38
|
+
maxCorrections: 2,
|
|
39
|
+
});
|
|
40
|
+
const speciesReviewer = defineAgent({
|
|
41
|
+
name: 'species-reviewer',
|
|
42
|
+
description: 'Removes species the author added that do not belong to the topic.',
|
|
43
|
+
prompt: 'species-reviewer.md',
|
|
44
|
+
inputs: {
|
|
45
|
+
topic: {
|
|
46
|
+
description: 'The topic the person gave.',
|
|
47
|
+
serializer: topicText,
|
|
48
|
+
from: ({ state }) => ({ topic: state.topic }),
|
|
49
|
+
},
|
|
50
|
+
added: {
|
|
51
|
+
description: 'The species the author added in this assignment.',
|
|
52
|
+
serializer: namesText,
|
|
53
|
+
// `earlier` returns the author's reply with the type the author's reader gave it.
|
|
54
|
+
from: ({ earlier }) => ({
|
|
55
|
+
names: earlier({ agent: speciesAuthor }).reply.species.map((species) => species.name),
|
|
56
|
+
}),
|
|
57
|
+
},
|
|
58
|
+
correction: {
|
|
59
|
+
description: 'The reply that was sent back and why, on a call after one.',
|
|
60
|
+
serializer: correctionText,
|
|
61
|
+
from: ({ correction }) => ({ correction }),
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
replyReader: jsonReply({
|
|
65
|
+
schema: z.object({ remove: z.string().array() }).strict(),
|
|
66
|
+
correctionFor: jsonReplyCorrection,
|
|
67
|
+
}),
|
|
68
|
+
replyOutcome: {
|
|
69
|
+
description: 'Removes only species the author added in this assignment.',
|
|
70
|
+
decide: ({ reply, earlier }) => {
|
|
71
|
+
// The same answer from the two states: on the list after the author's change, not before.
|
|
72
|
+
const author = earlier({ agent: speciesAuthor });
|
|
73
|
+
const added = author.stateAfter.species.filter((species) => !author.stateBefore.species.some((before) => before.id === species.id));
|
|
74
|
+
const notAdded = reply.remove.filter((name) => !added.some((species) => species.name === name));
|
|
75
|
+
if (notAdded.length)
|
|
76
|
+
return sendBack({ correction: `Not added by the author: ${notAdded.join(', ')}.` });
|
|
77
|
+
return accept({
|
|
78
|
+
change: (current) => ({
|
|
79
|
+
...current,
|
|
80
|
+
species: current.species.filter((species) => !reply.remove.includes(species.name)),
|
|
81
|
+
}),
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
maxCorrections: 2,
|
|
86
|
+
modelSettings: { effort: 'high' },
|
|
87
|
+
});
|
|
88
|
+
export const speciesList = defineAgentPhase({
|
|
89
|
+
name: 'species-list',
|
|
90
|
+
description: 'Lists the species the guide covers, then removes any that do not belong.',
|
|
91
|
+
assignments: 'once',
|
|
92
|
+
chain: [speciesAuthor, speciesReviewer],
|
|
93
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export { defineRunState } from './blueprint/define-run-state.js';
|
|
2
|
+
export { defineSerializer, type Serializer } from './blueprint/serializer.js';
|
|
3
|
+
export { formatJsonReplyCorrection, jsonReply, type ReplyReader, } from './blueprint/reply-reader.js';
|
|
4
|
+
export { sendBack } from './blueprint/outcomes.js';
|
|
5
|
+
export type { Answer, Artifact, Change, Correction, Question } from './blueprint/outcomes.js';
|
|
6
|
+
export type { BlueprintWorkers, CallsAnsweredBy, Condition, Effort, ModelSettings, QuestionsAnsweredBy, WorkerDefinition, } from './blueprint/run-settings.js';
|
|
7
|
+
export type { Agent, ReplyCheck, Tool } from './blueprint/agent.js';
|
|
8
|
+
export type { CollectionAction, RecordScope, WriteScopeRule } from './blueprint/write-scope.js';
|
|
9
|
+
export type { Blueprint, Lane, Phase } from './blueprint/phase.js';
|
|
10
|
+
export type { DeclaredStructure } from './blueprint/declared-structure.js';
|
|
11
|
+
export { loadBlueprint, prepareBlueprint, type LoadedBlueprint, } from './blueprint/load-blueprint.js';
|
|
12
|
+
export { defineDagmaConfig, loadDagmaConfig, type DagmaConfig } from './config.js';
|
|
13
|
+
export { createRun } from './run/create-run.js';
|
|
14
|
+
export { resumeRun } from './run/resume-run.js';
|
|
15
|
+
export { executeRun } from './run/execute-run.js';
|
|
16
|
+
export { RunSession, type RunEventObserver } from './run/run-session.js';
|
|
17
|
+
export { RunStopped } from './run/run-endings.js';
|
|
18
|
+
export type { RunStore } from './run/record/run-store.js';
|
|
19
|
+
export { ensureDagmaSchema, postgresRunStore } from './run/record/postgres-run-store.js';
|
|
20
|
+
export type { CallFile, ReasoningSummary, RunSummary, SavedToolCall, } from './run/record/run-types.js';
|
|
21
|
+
export { progressFromEvents } from './run/record/apply-event.js';
|
|
22
|
+
export { applyProgressDelta, progressDeltaFromSnapshots, progressSnapshot, type ProgressAssignment, type ProgressDelta, type ProgressPhase, type ProgressQuestion, type ProgressSnapshot, } from './run/record/progress-projection.js';
|
|
23
|
+
export type { Checkpoint, RunProgress, RunStatus } from './run/record/progress.js';
|
|
24
|
+
export type { ModelOverride, RunEvent, Tokens } from './run/record/events.js';
|
|
25
|
+
export { ProviderAccessError, ProviderConnectionError, type Infer, type InferenceCall, type InferenceReply, type OfferedTool, type PromptBlock, type PromptCacheTtl, } from './inference/inference.js';
|
|
26
|
+
export { openAiApiInference, type OpenAiBeforeRequest, type OpenAiProviderResponse, } from './inference/openai-api.js';
|
|
27
|
+
export { anthropicApiInference, type AnthropicBeforeRequest, type AnthropicCacheTokens, type AnthropicProviderResponse, } from './inference/anthropic-api.js';
|
|
28
|
+
export { anthropicRequestCostUsd, hasAnthropicPrice } from './inference/anthropic-prices.js';
|
|
29
|
+
export { bedrockApiInference, type BedrockBeforeRequest, type BedrockCacheTokens, type BedrockProviderResponse, } from './inference/bedrock-api.js';
|
|
30
|
+
export { bedrockRequestCostUsd, bedrockSupportsExtendedTtl, hasBedrockPrice, } from './inference/bedrock-prices.js';
|
|
31
|
+
export { geminiApiInference, type GeminiBeforeRequest, type GeminiCacheTokens, type GeminiProviderResponse, } from './inference/gemini-api.js';
|
|
32
|
+
export { geminiRequestCostUsd, hasGeminiPrice } from './inference/gemini-prices.js';
|
|
33
|
+
export { CallQueue, CallQueueError, openCallQueue } from './inference/call-queue.js';
|
|
34
|
+
export { ForwardedToolCallSchema, QueueConnectionStatusSchema, QueuedCallSchema, QueueEventSchema, TokensSchema, ToolCallAnswerSchema, WorkerFailureSchema, WorkerProgramSchema, WorkerReplySchema, WorkerStatusSchema, type QueueConnectionStatus, type QueuedCall, type QueueEvent, type WorkerFailure, type WorkerProgram, type WorkerReply, type WorkerStatus, } from './inference/call-queue-shapes.js';
|
|
35
|
+
export { ScriptedRepliesSchema, scriptedInference, type ScriptedReplies, type ScriptedReply, } from './inference/scripted.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export { defineRunState } from './blueprint/define-run-state.js';
|
|
2
|
+
export { defineSerializer } from './blueprint/serializer.js';
|
|
3
|
+
export { formatJsonReplyCorrection, jsonReply, } from './blueprint/reply-reader.js';
|
|
4
|
+
export { sendBack } from './blueprint/outcomes.js';
|
|
5
|
+
export { loadBlueprint, prepareBlueprint, } from './blueprint/load-blueprint.js';
|
|
6
|
+
export { defineDagmaConfig, loadDagmaConfig } from './config.js';
|
|
7
|
+
export { createRun } from './run/create-run.js';
|
|
8
|
+
export { resumeRun } from './run/resume-run.js';
|
|
9
|
+
export { executeRun } from './run/execute-run.js';
|
|
10
|
+
export { RunSession } from './run/run-session.js';
|
|
11
|
+
export { RunStopped } from './run/run-endings.js';
|
|
12
|
+
export { ensureDagmaSchema, postgresRunStore } from './run/record/postgres-run-store.js';
|
|
13
|
+
export { progressFromEvents } from './run/record/apply-event.js';
|
|
14
|
+
export { applyProgressDelta, progressDeltaFromSnapshots, progressSnapshot, } from './run/record/progress-projection.js';
|
|
15
|
+
export { ProviderAccessError, ProviderConnectionError, } from './inference/inference.js';
|
|
16
|
+
export { openAiApiInference, } from './inference/openai-api.js';
|
|
17
|
+
export { anthropicApiInference, } from './inference/anthropic-api.js';
|
|
18
|
+
export { anthropicRequestCostUsd, hasAnthropicPrice } from './inference/anthropic-prices.js';
|
|
19
|
+
export { bedrockApiInference, } from './inference/bedrock-api.js';
|
|
20
|
+
export { bedrockRequestCostUsd, bedrockSupportsExtendedTtl, hasBedrockPrice, } from './inference/bedrock-prices.js';
|
|
21
|
+
export { geminiApiInference, } from './inference/gemini-api.js';
|
|
22
|
+
export { geminiRequestCostUsd, hasGeminiPrice } from './inference/gemini-prices.js';
|
|
23
|
+
export { CallQueue, CallQueueError, openCallQueue } from './inference/call-queue.js';
|
|
24
|
+
export { ForwardedToolCallSchema, QueueConnectionStatusSchema, QueuedCallSchema, QueueEventSchema, TokensSchema, ToolCallAnswerSchema, WorkerFailureSchema, WorkerProgramSchema, WorkerReplySchema, WorkerStatusSchema, } from './inference/call-queue-shapes.js';
|
|
25
|
+
export { ScriptedRepliesSchema, scriptedInference, } from './inference/scripted.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Tokens } from '../run/record/events.js';
|
|
2
|
+
import { type AnthropicResponseStatus } from './anthropic-request.js';
|
|
3
|
+
import type { Infer, InferenceCall } from './inference.js';
|
|
4
|
+
export type AnthropicCacheTokens = {
|
|
5
|
+
uncachedInput: number;
|
|
6
|
+
cacheCreation5m: number;
|
|
7
|
+
cacheCreation1h: number;
|
|
8
|
+
cacheRead: number;
|
|
9
|
+
};
|
|
10
|
+
export type AnthropicBeforeRequest = (input: {
|
|
11
|
+
call: InferenceCall;
|
|
12
|
+
requestIndex: number;
|
|
13
|
+
model: string;
|
|
14
|
+
}) => void | Promise<void>;
|
|
15
|
+
export type AnthropicProviderResponse = (input: {
|
|
16
|
+
call: InferenceCall;
|
|
17
|
+
responseId: string;
|
|
18
|
+
model: string;
|
|
19
|
+
tokens: Tokens;
|
|
20
|
+
cacheTokens: AnthropicCacheTokens;
|
|
21
|
+
costUsd?: number;
|
|
22
|
+
status: AnthropicResponseStatus;
|
|
23
|
+
}) => void | Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Answers calls with the Anthropic Messages API. Supports explicit `promptBlocks` cache
|
|
26
|
+
* breakpoints (`1h` and `5m`), automatic rolling `5m` caching across multi-turn tool loops,
|
|
27
|
+
* adaptive thinking + effort, structured output formats, and per-response credit hooks.
|
|
28
|
+
*/
|
|
29
|
+
export declare function anthropicApiInference({ apiKey, defaultModel, baseUrl, outputFormat, maxOutputTokens, beforeRequest, onProviderResponse, }: {
|
|
30
|
+
apiKey: string;
|
|
31
|
+
defaultModel?: string;
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
outputFormat?: unknown;
|
|
34
|
+
maxOutputTokens?: number;
|
|
35
|
+
beforeRequest?: AnthropicBeforeRequest;
|
|
36
|
+
onProviderResponse?: AnthropicProviderResponse;
|
|
37
|
+
}): Infer;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { anthropicRequestCostUsd, hasAnthropicPrice } from './anthropic-prices.js';
|
|
2
|
+
import { completedAnthropicMessage, } from './anthropic-request.js';
|
|
3
|
+
const DEFAULT_MAX_OUTPUT_TOKENS = 16_384;
|
|
4
|
+
const MAX_CACHE_BREAKPOINTS = 4;
|
|
5
|
+
function cacheControlFor(ttl) {
|
|
6
|
+
return ttl === '1h' ? { type: 'ephemeral', ttl: '1h' } : { type: 'ephemeral' };
|
|
7
|
+
}
|
|
8
|
+
function initialUserContent({ call }) {
|
|
9
|
+
const blocks = call.promptBlocks?.length
|
|
10
|
+
? call.promptBlocks
|
|
11
|
+
: [{ text: call.prompt }];
|
|
12
|
+
let sawShortTtl = false;
|
|
13
|
+
let staticBreakpoints = 0;
|
|
14
|
+
const content = [];
|
|
15
|
+
for (const block of blocks) {
|
|
16
|
+
if (!block.text)
|
|
17
|
+
continue;
|
|
18
|
+
if (block.cache === '1h' && sawShortTtl)
|
|
19
|
+
throw new Error(`${call.agent} puts a 1h cache breakpoint after a 5m cache breakpoint; longer TTLs must come first.`);
|
|
20
|
+
if (block.cache === '5m')
|
|
21
|
+
sawShortTtl = true;
|
|
22
|
+
if (block.cache) {
|
|
23
|
+
staticBreakpoints += 1;
|
|
24
|
+
if (staticBreakpoints > MAX_CACHE_BREAKPOINTS)
|
|
25
|
+
throw new Error(`${call.agent} defines more than ${MAX_CACHE_BREAKPOINTS} prompt cache breakpoints.`);
|
|
26
|
+
}
|
|
27
|
+
content.push({
|
|
28
|
+
type: 'text',
|
|
29
|
+
text: block.text,
|
|
30
|
+
...(block.cache ? { cache_control: cacheControlFor(block.cache) } : {}),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
for (const { mediaType, bytes } of call.images)
|
|
34
|
+
content.push({
|
|
35
|
+
type: 'image',
|
|
36
|
+
source: {
|
|
37
|
+
type: 'base64',
|
|
38
|
+
media_type: mediaType,
|
|
39
|
+
data: Buffer.from(bytes).toString('base64'),
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return { content, staticBreakpoints };
|
|
43
|
+
}
|
|
44
|
+
function extractCacheTokens(response) {
|
|
45
|
+
const { usage } = response;
|
|
46
|
+
const uncachedInput = typeof usage.input_tokens === 'number' ? usage.input_tokens : 0;
|
|
47
|
+
const cacheRead = typeof usage.cache_read_input_tokens === 'number' ? usage.cache_read_input_tokens : 0;
|
|
48
|
+
const hasSplit = typeof usage.cache_creation?.ephemeral_5m_input_tokens === 'number' ||
|
|
49
|
+
typeof usage.cache_creation?.ephemeral_1h_input_tokens === 'number';
|
|
50
|
+
const cacheCreation5m = hasSplit
|
|
51
|
+
? (usage.cache_creation?.ephemeral_5m_input_tokens ?? 0)
|
|
52
|
+
: typeof usage.cache_creation_input_tokens === 'number'
|
|
53
|
+
? usage.cache_creation_input_tokens
|
|
54
|
+
: 0;
|
|
55
|
+
const cacheCreation1h = hasSplit ? (usage.cache_creation?.ephemeral_1h_input_tokens ?? 0) : 0;
|
|
56
|
+
return { uncachedInput, cacheCreation5m, cacheCreation1h, cacheRead };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Answers calls with the Anthropic Messages API. Supports explicit `promptBlocks` cache
|
|
60
|
+
* breakpoints (`1h` and `5m`), automatic rolling `5m` caching across multi-turn tool loops,
|
|
61
|
+
* adaptive thinking + effort, structured output formats, and per-response credit hooks.
|
|
62
|
+
*/
|
|
63
|
+
export function anthropicApiInference({ apiKey, defaultModel, baseUrl, outputFormat, maxOutputTokens, beforeRequest, onProviderResponse, }) {
|
|
64
|
+
return async ({ call, callTool, signal, onRequestCounted }) => {
|
|
65
|
+
const model = call.model ?? defaultModel;
|
|
66
|
+
if (!model || !hasAnthropicPrice({ model }))
|
|
67
|
+
throw new Error(`${call.agent} names ${model ?? 'no model'}, which has no price on the Anthropic API.`);
|
|
68
|
+
const { content: firstContent, staticBreakpoints } = initialUserContent({ call });
|
|
69
|
+
const messages = [{ role: 'user', content: firstContent }];
|
|
70
|
+
const tokens = {};
|
|
71
|
+
const reasoning = [];
|
|
72
|
+
let costUsd;
|
|
73
|
+
let toolCallsAnswered = 0;
|
|
74
|
+
const countRequest = async ({ response, status, }) => {
|
|
75
|
+
const cacheTokens = extractCacheTokens(response);
|
|
76
|
+
const totalInput = cacheTokens.uncachedInput +
|
|
77
|
+
cacheTokens.cacheCreation5m +
|
|
78
|
+
cacheTokens.cacheCreation1h +
|
|
79
|
+
cacheTokens.cacheRead;
|
|
80
|
+
const reported = {};
|
|
81
|
+
if (typeof response.usage.input_tokens === 'number' ||
|
|
82
|
+
typeof response.usage.cache_creation_input_tokens === 'number' ||
|
|
83
|
+
typeof response.usage.cache_read_input_tokens === 'number')
|
|
84
|
+
reported.input = totalInput;
|
|
85
|
+
if (cacheTokens.cacheRead > 0 || typeof response.usage.cache_read_input_tokens === 'number')
|
|
86
|
+
reported.cached = cacheTokens.cacheRead;
|
|
87
|
+
if (typeof response.usage.output_tokens === 'number')
|
|
88
|
+
reported.output = response.usage.output_tokens;
|
|
89
|
+
for (const name of ['input', 'cached', 'output'])
|
|
90
|
+
if (typeof reported[name] === 'number')
|
|
91
|
+
tokens[name] = (tokens[name] ?? 0) + reported[name];
|
|
92
|
+
let responseCostUsd;
|
|
93
|
+
if (typeof reported.input === 'number' && typeof reported.output === 'number') {
|
|
94
|
+
responseCostUsd = anthropicRequestCostUsd({
|
|
95
|
+
model,
|
|
96
|
+
uncachedInputTokens: cacheTokens.uncachedInput,
|
|
97
|
+
cacheCreation5mTokens: cacheTokens.cacheCreation5m,
|
|
98
|
+
cacheCreation1hTokens: cacheTokens.cacheCreation1h,
|
|
99
|
+
cacheReadTokens: cacheTokens.cacheRead,
|
|
100
|
+
outputTokens: reported.output,
|
|
101
|
+
});
|
|
102
|
+
costUsd = (costUsd ?? 0) + responseCostUsd;
|
|
103
|
+
}
|
|
104
|
+
let toolCallsBefore = toolCallsAnswered;
|
|
105
|
+
for (const block of response.content) {
|
|
106
|
+
if (block.type === 'tool_use')
|
|
107
|
+
toolCallsBefore += 1;
|
|
108
|
+
else if (block.type === 'thinking' && block.thinking)
|
|
109
|
+
reasoning.push({ text: block.thinking, afterToolCalls: toolCallsBefore });
|
|
110
|
+
}
|
|
111
|
+
onRequestCounted?.({
|
|
112
|
+
tokens: { ...tokens },
|
|
113
|
+
...(costUsd !== undefined ? { costUsd } : {}),
|
|
114
|
+
reasoning: [...reasoning],
|
|
115
|
+
});
|
|
116
|
+
await onProviderResponse?.({
|
|
117
|
+
call,
|
|
118
|
+
responseId: response.id,
|
|
119
|
+
model,
|
|
120
|
+
tokens: reported,
|
|
121
|
+
cacheTokens,
|
|
122
|
+
...(responseCostUsd !== undefined ? { costUsd: responseCostUsd } : {}),
|
|
123
|
+
status,
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
const effort = call.effort === 'low' || call.effort === 'medium' || call.effort === 'high'
|
|
127
|
+
? call.effort
|
|
128
|
+
: undefined;
|
|
129
|
+
const outputConfig = outputFormat || effort
|
|
130
|
+
? {
|
|
131
|
+
...(outputFormat ? { format: outputFormat } : {}),
|
|
132
|
+
...(effort ? { effort } : {}),
|
|
133
|
+
}
|
|
134
|
+
: undefined;
|
|
135
|
+
for (let requestIndex = 1;; requestIndex += 1) {
|
|
136
|
+
await beforeRequest?.({ call, requestIndex, model });
|
|
137
|
+
const response = await completedAnthropicMessage({
|
|
138
|
+
apiKey,
|
|
139
|
+
baseUrl,
|
|
140
|
+
signal,
|
|
141
|
+
onResponse: countRequest,
|
|
142
|
+
body: {
|
|
143
|
+
model,
|
|
144
|
+
max_tokens: maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS,
|
|
145
|
+
messages,
|
|
146
|
+
...(call.tools.length
|
|
147
|
+
? {
|
|
148
|
+
tools: call.tools.map((tool) => ({
|
|
149
|
+
name: tool.name,
|
|
150
|
+
description: tool.description,
|
|
151
|
+
input_schema: tool.inputSchema,
|
|
152
|
+
})),
|
|
153
|
+
}
|
|
154
|
+
: {}),
|
|
155
|
+
...(effort ? { thinking: { type: 'adaptive' } } : {}),
|
|
156
|
+
...(outputConfig ? { output_config: outputConfig } : {}),
|
|
157
|
+
stream: true,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
const toolUses = response.content.filter((block) => block.type === 'tool_use');
|
|
161
|
+
if (!toolUses.length) {
|
|
162
|
+
const replyText = response.content
|
|
163
|
+
.filter((block) => block.type === 'text')
|
|
164
|
+
.map((block) => block.text)
|
|
165
|
+
.join('');
|
|
166
|
+
return { replyText, tokens, ...(costUsd !== undefined ? { costUsd } : {}), reasoning };
|
|
167
|
+
}
|
|
168
|
+
// Preserve the assistant's exact blocks (including thinking + signatures) for the next turn.
|
|
169
|
+
messages.push({ role: 'assistant', content: response.content });
|
|
170
|
+
// Move any previous rolling 5m breakpoint off earlier tool-result turns before adding the new one.
|
|
171
|
+
for (let i = 1; i < messages.length; i += 1) {
|
|
172
|
+
const message = messages[i];
|
|
173
|
+
if (message.role === 'user')
|
|
174
|
+
for (const block of message.content)
|
|
175
|
+
if (block.type === 'tool_result' && block.cache_control)
|
|
176
|
+
delete block.cache_control;
|
|
177
|
+
}
|
|
178
|
+
const toolResults = [];
|
|
179
|
+
for (const toolUse of toolUses) {
|
|
180
|
+
signal.throwIfAborted();
|
|
181
|
+
const result = await callTool({ name: toolUse.name, args: toolUse.input });
|
|
182
|
+
toolCallsAnswered += 1;
|
|
183
|
+
toolResults.push({
|
|
184
|
+
type: 'tool_result',
|
|
185
|
+
tool_use_id: toolUse.id,
|
|
186
|
+
content: typeof result === 'string' ? result : JSON.stringify(result ?? null),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (staticBreakpoints < MAX_CACHE_BREAKPOINTS && toolResults.length > 0) {
|
|
190
|
+
const last = toolResults.at(-1);
|
|
191
|
+
if (last.type === 'tool_result')
|
|
192
|
+
last.cache_control = { type: 'ephemeral' };
|
|
193
|
+
}
|
|
194
|
+
messages.push({ role: 'user', content: toolResults });
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Whether the Anthropic API route can price a model. A run on the API is refused before any call
|
|
2
|
+
* when an agent names a model that has no price. */
|
|
3
|
+
export declare function hasAnthropicPrice({ model }: {
|
|
4
|
+
model: string;
|
|
5
|
+
}): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* The cost in dollars of one request to the Anthropic Messages API, priced by its own token counts.
|
|
8
|
+
* Note: Anthropic's `input_tokens` counts only uncached input tokens (separate from cache creation
|
|
9
|
+
* and cache read tokens).
|
|
10
|
+
*/
|
|
11
|
+
export declare function anthropicRequestCostUsd({ model, uncachedInputTokens, cacheCreation5mTokens, cacheCreation1hTokens, cacheReadTokens, outputTokens, }: {
|
|
12
|
+
model: string;
|
|
13
|
+
uncachedInputTokens: number;
|
|
14
|
+
cacheCreation5mTokens?: number;
|
|
15
|
+
cacheCreation1hTokens?: number;
|
|
16
|
+
cacheReadTokens?: number;
|
|
17
|
+
outputTokens: number;
|
|
18
|
+
}): number;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dollars per million tokens, from Anthropic's public pricing for Claude 4.6 models.
|
|
3
|
+
* Cache write multipliers: 5m = 1.25x base input, 1h = 2.0x base input; cache read = 0.1x.
|
|
4
|
+
*/
|
|
5
|
+
const PRICES = {
|
|
6
|
+
'claude-sonnet-4-6': {
|
|
7
|
+
input: 3,
|
|
8
|
+
output: 15,
|
|
9
|
+
cacheWrite5m: 3.75,
|
|
10
|
+
cacheWrite1h: 6,
|
|
11
|
+
cacheRead: 0.3,
|
|
12
|
+
},
|
|
13
|
+
'claude-opus-4-6': {
|
|
14
|
+
input: 5,
|
|
15
|
+
output: 25,
|
|
16
|
+
cacheWrite5m: 6.25,
|
|
17
|
+
cacheWrite1h: 10,
|
|
18
|
+
cacheRead: 0.5,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
/** Whether the Anthropic API route can price a model. A run on the API is refused before any call
|
|
22
|
+
* when an agent names a model that has no price. */
|
|
23
|
+
export function hasAnthropicPrice({ model }) {
|
|
24
|
+
return Object.hasOwn(PRICES, model);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The cost in dollars of one request to the Anthropic Messages API, priced by its own token counts.
|
|
28
|
+
* Note: Anthropic's `input_tokens` counts only uncached input tokens (separate from cache creation
|
|
29
|
+
* and cache read tokens).
|
|
30
|
+
*/
|
|
31
|
+
export function anthropicRequestCostUsd({ model, uncachedInputTokens, cacheCreation5mTokens = 0, cacheCreation1hTokens = 0, cacheReadTokens = 0, outputTokens, }) {
|
|
32
|
+
const rates = PRICES[model];
|
|
33
|
+
return ((uncachedInputTokens * rates.input +
|
|
34
|
+
cacheCreation5mTokens * rates.cacheWrite5m +
|
|
35
|
+
cacheCreation1hTokens * rates.cacheWrite1h +
|
|
36
|
+
cacheReadTokens * rates.cacheRead +
|
|
37
|
+
outputTokens * rates.output) /
|
|
38
|
+
1_000_000);
|
|
39
|
+
}
|