paracosm 0.4.146 → 0.4.148
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/src/runtime/citations-catalog.d.ts +34 -0
- package/dist/src/runtime/citations-catalog.d.ts.map +1 -0
- package/dist/src/runtime/citations-catalog.js +52 -0
- package/dist/src/runtime/citations-catalog.js.map +1 -0
- package/dist/src/runtime/commander-setup.d.ts +70 -0
- package/dist/src/runtime/commander-setup.d.ts.map +1 -0
- package/dist/src/runtime/commander-setup.js +116 -0
- package/dist/src/runtime/commander-setup.js.map +1 -0
- package/dist/src/runtime/cost-tracker.d.ts +87 -0
- package/dist/src/runtime/cost-tracker.d.ts.map +1 -0
- package/dist/src/runtime/cost-tracker.js +119 -0
- package/dist/src/runtime/cost-tracker.js.map +1 -0
- package/dist/src/runtime/orchestrator.d.ts +2 -41
- package/dist/src/runtime/orchestrator.d.ts.map +1 -1
- package/dist/src/runtime/orchestrator.js +41 -292
- package/dist/src/runtime/orchestrator.js.map +1 -1
- package/dist/src/runtime/tool-ledger.d.ts +40 -0
- package/dist/src/runtime/tool-ledger.d.ts.map +1 -1
- package/dist/src/runtime/tool-ledger.js +57 -0
- package/dist/src/runtime/tool-ledger.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flat, deduplicated citation catalog builder.
|
|
3
|
+
*
|
|
4
|
+
* Every department report in a run carries its own citations. The
|
|
5
|
+
* catalog merges them by URL (falling back to text when no URL is
|
|
6
|
+
* present) and tracks which departments + turns referenced each
|
|
7
|
+
* citation, so the References section in reports can show
|
|
8
|
+
* provenance across the whole run.
|
|
9
|
+
*
|
|
10
|
+
* Pure — takes the per-turn report list, returns a serialisable array.
|
|
11
|
+
*
|
|
12
|
+
* @module paracosm/runtime/citations-catalog
|
|
13
|
+
*/
|
|
14
|
+
import type { DepartmentReport } from './contracts.js';
|
|
15
|
+
/** One row in the run's flat citation catalog. */
|
|
16
|
+
export interface CitationCatalogEntry {
|
|
17
|
+
text: string;
|
|
18
|
+
url: string;
|
|
19
|
+
doi?: string;
|
|
20
|
+
departments: string[];
|
|
21
|
+
turns: number[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Build the canonical citation catalog from a run's department reports.
|
|
25
|
+
* Dedup key is URL when present, else the citation text. DOI is
|
|
26
|
+
* preserved from whichever occurrence first supplies it. Turn numbers
|
|
27
|
+
* are sorted ascending in the output so consumers render a clean
|
|
28
|
+
* chronological trail.
|
|
29
|
+
*/
|
|
30
|
+
export declare function buildCitationCatalog(allDepartmentReports: ReadonlyArray<{
|
|
31
|
+
turn: number;
|
|
32
|
+
report: DepartmentReport;
|
|
33
|
+
}>): CitationCatalogEntry[];
|
|
34
|
+
//# sourceMappingURL=citations-catalog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"citations-catalog.d.ts","sourceRoot":"","sources":["../../../src/runtime/citations-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,kDAAkD;AAClD,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,oBAAoB,EAAE,aAAa,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,gBAAgB,CAAA;CAAE,CAAC,GAC9E,oBAAoB,EAAE,CAiCxB"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flat, deduplicated citation catalog builder.
|
|
3
|
+
*
|
|
4
|
+
* Every department report in a run carries its own citations. The
|
|
5
|
+
* catalog merges them by URL (falling back to text when no URL is
|
|
6
|
+
* present) and tracks which departments + turns referenced each
|
|
7
|
+
* citation, so the References section in reports can show
|
|
8
|
+
* provenance across the whole run.
|
|
9
|
+
*
|
|
10
|
+
* Pure — takes the per-turn report list, returns a serialisable array.
|
|
11
|
+
*
|
|
12
|
+
* @module paracosm/runtime/citations-catalog
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Build the canonical citation catalog from a run's department reports.
|
|
16
|
+
* Dedup key is URL when present, else the citation text. DOI is
|
|
17
|
+
* preserved from whichever occurrence first supplies it. Turn numbers
|
|
18
|
+
* are sorted ascending in the output so consumers render a clean
|
|
19
|
+
* chronological trail.
|
|
20
|
+
*/
|
|
21
|
+
export function buildCitationCatalog(allDepartmentReports) {
|
|
22
|
+
const byKey = new Map();
|
|
23
|
+
for (const { turn, report } of allDepartmentReports) {
|
|
24
|
+
for (const c of report.citations) {
|
|
25
|
+
const key = (c.url || '').trim() || `text:${(c.text || '').trim()}`;
|
|
26
|
+
if (!key)
|
|
27
|
+
continue;
|
|
28
|
+
let entry = byKey.get(key);
|
|
29
|
+
if (!entry) {
|
|
30
|
+
entry = {
|
|
31
|
+
text: c.text || c.url || '',
|
|
32
|
+
url: c.url || '',
|
|
33
|
+
doi: c.doi,
|
|
34
|
+
departments: new Set(),
|
|
35
|
+
turns: new Set(),
|
|
36
|
+
};
|
|
37
|
+
byKey.set(key, entry);
|
|
38
|
+
}
|
|
39
|
+
if (report.department)
|
|
40
|
+
entry.departments.add(report.department);
|
|
41
|
+
entry.turns.add(turn);
|
|
42
|
+
if (!entry.doi && c.doi)
|
|
43
|
+
entry.doi = c.doi;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [...byKey.values()].map(e => ({
|
|
47
|
+
...e,
|
|
48
|
+
departments: [...e.departments],
|
|
49
|
+
turns: [...e.turns].sort((a, b) => a - b),
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=citations-catalog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"citations-catalog.js","sourceRoot":"","sources":["../../../src/runtime/citations-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAaH;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,oBAA+E;IAE/E,MAAM,KAAK,GAAG,IAAI,GAAG,EAMjB,CAAC;IACL,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,oBAAoB,EAAE,CAAC;QACpD,KAAK,MAAM,CAAC,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;YACjC,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,QAAQ,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;YACpE,IAAI,CAAC,GAAG;gBAAE,SAAS;YACnB,IAAI,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC3B,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,KAAK,GAAG;oBACN,IAAI,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,EAAE;oBAC3B,GAAG,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE;oBAChB,GAAG,EAAE,CAAC,CAAC,GAAG;oBACV,WAAW,EAAE,IAAI,GAAG,EAAE;oBACtB,KAAK,EAAE,IAAI,GAAG,EAAE;iBACjB,CAAC;gBACF,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;YACxB,CAAC;YACD,IAAI,MAAM,CAAC,UAAU;gBAAE,KAAK,CAAC,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YAChE,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACtB,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG;gBAAE,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC;QAC7C,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACnC,GAAG,CAAC;QACJ,WAAW,EAAE,CAAC,GAAG,CAAC,CAAC,WAAW,CAAC;QAC/B,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC;KAC1C,CAAC,CAAC,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commander bootstrap + turn-0 department-head promotions.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from orchestrator.ts so runSimulation reads as a turn loop
|
|
5
|
+
* rather than a 100-line setup block before the loop even starts.
|
|
6
|
+
* The two responsibilities are:
|
|
7
|
+
*
|
|
8
|
+
* 1. Build a personality-cue line from the leader's HEXACO so the
|
|
9
|
+
* commander's first message reinforces trait-driven decision
|
|
10
|
+
* style (high-openness → "favor novel approaches", etc.).
|
|
11
|
+
* 2. Fire the turn-0 promotion LLM call, parse the response, and
|
|
12
|
+
* ask the kernel to promote the named candidates. A top-candidate
|
|
13
|
+
* fallback runs for any department the commander skipped so
|
|
14
|
+
* every dept always has a head going into turn 1.
|
|
15
|
+
*
|
|
16
|
+
* All kernel mutations, SSE emits, and cost-tracker calls flow through
|
|
17
|
+
* callbacks passed in by the orchestrator, so this module stays free
|
|
18
|
+
* of the turn-loop's closure state.
|
|
19
|
+
*
|
|
20
|
+
* @module paracosm/runtime/commander-setup
|
|
21
|
+
*/
|
|
22
|
+
import type { HexacoProfile } from '../engine/core/state.js';
|
|
23
|
+
import type { SimulationKernel } from '../engine/core/kernel.js';
|
|
24
|
+
import type { ScenarioPackage, LeaderConfig } from '../engine/types.js';
|
|
25
|
+
import type { CallUsage } from './cost-tracker.js';
|
|
26
|
+
/** Build a short "Your decision style" line from the leader's HEXACO profile. */
|
|
27
|
+
export declare function buildPersonalityCue(h: HexacoProfile): string;
|
|
28
|
+
/**
|
|
29
|
+
* Assemble the bootstrap message sent to the commander session right
|
|
30
|
+
* after it's created. Reinforces the leader's personality cue + the
|
|
31
|
+
* selectedOptionId JSON format the downstream turn loop expects.
|
|
32
|
+
*/
|
|
33
|
+
export declare function buildCommanderBootstrap(personalityCue: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Candidate-summary dependencies needed to run the turn-0 promotion.
|
|
36
|
+
* Kept minimal so the flow doesn't couple to the full kernel surface —
|
|
37
|
+
* tests can stub getCandidates + promoteAgent with ~20 lines of fakes.
|
|
38
|
+
*/
|
|
39
|
+
export interface PromotionKernel {
|
|
40
|
+
getCandidates: SimulationKernel['getCandidates'];
|
|
41
|
+
promoteAgent: SimulationKernel['promoteAgent'];
|
|
42
|
+
getState: SimulationKernel['getState'];
|
|
43
|
+
}
|
|
44
|
+
export interface RunPromotionArgs {
|
|
45
|
+
kernel: PromotionKernel;
|
|
46
|
+
scenario: ScenarioPackage;
|
|
47
|
+
leader: LeaderConfig;
|
|
48
|
+
startYear: number;
|
|
49
|
+
/** Commander session `.send(prompt)` — returns whatever AgentOS returns. */
|
|
50
|
+
sendToCommander: (prompt: string) => Promise<{
|
|
51
|
+
text: string;
|
|
52
|
+
usage?: CallUsage;
|
|
53
|
+
}>;
|
|
54
|
+
/** Tagged cost-tracker entry point so the commander bucket gets charged. */
|
|
55
|
+
trackUsage: (result: {
|
|
56
|
+
usage?: CallUsage;
|
|
57
|
+
}, site?: 'commander') => void;
|
|
58
|
+
/** SSE emit used to publish each successful promotion. */
|
|
59
|
+
emit: (type: 'promotion', data?: Record<string, unknown>) => void;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Run the turn-0 promotion flow end to end: build the candidate summary
|
|
63
|
+
* from the kernel, send it to the commander session, parse the returned
|
|
64
|
+
* JSON, and tell the kernel to promote each accepted candidate. When
|
|
65
|
+
* the commander skips a department (bad JSON, refused promotion, etc.)
|
|
66
|
+
* the top kernel candidate for that department is promoted as a
|
|
67
|
+
* fallback so no department enters turn 1 without a head.
|
|
68
|
+
*/
|
|
69
|
+
export declare function runDepartmentPromotions(args: RunPromotionArgs): Promise<void>;
|
|
70
|
+
//# sourceMappingURL=commander-setup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commander-setup.d.ts","sourceRoot":"","sources":["../../../src/runtime/commander-setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EAAc,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAGnD,iFAAiF;AACjF,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,aAAa,GAAG,MAAM,CAW5D;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,CAYtE;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,aAAa,EAAE,gBAAgB,CAAC,eAAe,CAAC,CAAC;IACjD,YAAY,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAAC;IAC/C,QAAQ,EAAE,gBAAgB,CAAC,UAAU,CAAC,CAAC;CACxC;AAED,MAAM,WAAW,gBAAgB;IAC/B,MAAM,EAAE,eAAe,CAAC;IACxB,QAAQ,EAAE,eAAe,CAAC;IAC1B,MAAM,EAAE,YAAY,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,eAAe,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,SAAS,CAAA;KAAE,CAAC,CAAC;IAClF,4EAA4E;IAC5E,UAAU,EAAE,CAAC,MAAM,EAAE;QAAE,KAAK,CAAC,EAAE,SAAS,CAAA;KAAE,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,IAAI,CAAC;IACxE,0DAA0D;IAC1D,IAAI,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;CACnE;AAED;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CA6CnF"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commander bootstrap + turn-0 department-head promotions.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from orchestrator.ts so runSimulation reads as a turn loop
|
|
5
|
+
* rather than a 100-line setup block before the loop even starts.
|
|
6
|
+
* The two responsibilities are:
|
|
7
|
+
*
|
|
8
|
+
* 1. Build a personality-cue line from the leader's HEXACO so the
|
|
9
|
+
* commander's first message reinforces trait-driven decision
|
|
10
|
+
* style (high-openness → "favor novel approaches", etc.).
|
|
11
|
+
* 2. Fire the turn-0 promotion LLM call, parse the response, and
|
|
12
|
+
* ask the kernel to promote the named candidates. A top-candidate
|
|
13
|
+
* fallback runs for any department the commander skipped so
|
|
14
|
+
* every dept always has a head going into turn 1.
|
|
15
|
+
*
|
|
16
|
+
* All kernel mutations, SSE emits, and cost-tracker calls flow through
|
|
17
|
+
* callbacks passed in by the orchestrator, so this module stays free
|
|
18
|
+
* of the turn-loop's closure state.
|
|
19
|
+
*
|
|
20
|
+
* @module paracosm/runtime/commander-setup
|
|
21
|
+
*/
|
|
22
|
+
import { buildPromotionPrompt } from './runtime-helpers.js';
|
|
23
|
+
/** Build a short "Your decision style" line from the leader's HEXACO profile. */
|
|
24
|
+
export function buildPersonalityCue(h) {
|
|
25
|
+
const cues = [];
|
|
26
|
+
if (h.openness > 0.7)
|
|
27
|
+
cues.push('You favor novel, untested approaches over proven ones');
|
|
28
|
+
if (h.openness < 0.3)
|
|
29
|
+
cues.push('You favor proven protocols over experiments');
|
|
30
|
+
if (h.conscientiousness > 0.7)
|
|
31
|
+
cues.push('You demand evidence and contingency plans before committing');
|
|
32
|
+
if (h.conscientiousness < 0.3)
|
|
33
|
+
cues.push('You move fast and accept ambiguity');
|
|
34
|
+
if (h.emotionality > 0.7)
|
|
35
|
+
cues.push('You weigh human cost heavily — even small mortality risks deter you');
|
|
36
|
+
if (h.emotionality < 0.3)
|
|
37
|
+
cues.push('You will accept casualties for strategic gain');
|
|
38
|
+
if (h.agreeableness < 0.4)
|
|
39
|
+
cues.push('You override department consensus when you see a better path');
|
|
40
|
+
if (h.honestyHumility < 0.4)
|
|
41
|
+
cues.push('You leverage information asymmetries when useful');
|
|
42
|
+
return cues.length ? `Your decision style: ${cues.join('. ')}.` : '';
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Assemble the bootstrap message sent to the commander session right
|
|
46
|
+
* after it's created. Reinforces the leader's personality cue + the
|
|
47
|
+
* selectedOptionId JSON format the downstream turn loop expects.
|
|
48
|
+
*/
|
|
49
|
+
export function buildCommanderBootstrap(personalityCue) {
|
|
50
|
+
return (`You are the colony commander. You receive department reports and make strategic decisions. ` +
|
|
51
|
+
`${personalityCue} ` +
|
|
52
|
+
`Your personality MUST visibly shape your choices — do not converge on a centrist option just because ` +
|
|
53
|
+
`it sounds reasonable. If your traits push you toward the risky option, take it; if they push you toward ` +
|
|
54
|
+
`the safe option, take it. The simulation's value is in how different leaders produce different outcomes ` +
|
|
55
|
+
`from the same starting state. ` +
|
|
56
|
+
`When the crisis includes options with IDs, you MUST include selectedOptionId in your JSON response. ` +
|
|
57
|
+
`Return JSON with selectedOptionId, decision, rationale, selectedPolicies, rejectedPolicies, ` +
|
|
58
|
+
`expectedTradeoffs, watchMetricsNextTurn. Acknowledge.`);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Run the turn-0 promotion flow end to end: build the candidate summary
|
|
62
|
+
* from the kernel, send it to the commander session, parse the returned
|
|
63
|
+
* JSON, and tell the kernel to promote each accepted candidate. When
|
|
64
|
+
* the commander skips a department (bad JSON, refused promotion, etc.)
|
|
65
|
+
* the top kernel candidate for that department is promoted as a
|
|
66
|
+
* fallback so no department enters turn 1 without a head.
|
|
67
|
+
*/
|
|
68
|
+
export async function runDepartmentPromotions(args) {
|
|
69
|
+
const { kernel, scenario, leader, startYear, sendToCommander, trackUsage, emit } = args;
|
|
70
|
+
console.log(' [Turn 0] Commander evaluating roster for promotions...');
|
|
71
|
+
const promotionDepts = scenario.departments.map(d => d.id);
|
|
72
|
+
const roleNames = Object.fromEntries(scenario.departments.map(d => [d.id, d.role]));
|
|
73
|
+
const candidateSummaries = promotionDepts.map(dept => {
|
|
74
|
+
const candidates = kernel.getCandidates(dept, 5);
|
|
75
|
+
return `## ${dept.toUpperCase()} — Top 5 Candidates:\n${candidates.map(c => {
|
|
76
|
+
const age = startYear - c.core.birthYear;
|
|
77
|
+
const h = c.hexaco;
|
|
78
|
+
return `- ${c.core.name} (${c.core.id}), age ${age}, spec: ${c.career.specialization}, O:${h.openness.toFixed(2)} C:${h.conscientiousness.toFixed(2)} E:${h.extraversion.toFixed(2)} A:${h.agreeableness.toFixed(2)} Em:${h.emotionality.toFixed(2)} HH:${h.honestyHumility.toFixed(2)}`;
|
|
79
|
+
}).join('\n')}`;
|
|
80
|
+
}).join('\n\n');
|
|
81
|
+
const promoResult = await sendToCommander(buildPromotionPrompt(candidateSummaries));
|
|
82
|
+
trackUsage(promoResult, 'commander');
|
|
83
|
+
const promoMatch = promoResult.text.match(/\{[\s\S]*"promotions"[\s\S]*\}/);
|
|
84
|
+
if (promoMatch) {
|
|
85
|
+
try {
|
|
86
|
+
const pd = JSON.parse(promoMatch[0]);
|
|
87
|
+
for (const p of pd.promotions || []) {
|
|
88
|
+
try {
|
|
89
|
+
kernel.promoteAgent(p.agentId, p.department, p.role, leader.name);
|
|
90
|
+
console.log(` ✦ ${p.agentId} → ${p.role}: ${p.reason?.slice(0, 80)}`);
|
|
91
|
+
emit('promotion', { agentId: p.agentId, department: p.department, role: p.role, reason: p.reason?.slice(0, 120) });
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
console.log(` ✦ Promotion failed: ${err}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
console.warn(' [promotion] Failed to parse promotion JSON:', e);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Fallback: promote the top candidate for any department the commander
|
|
103
|
+
// left unfilled, so turn 1 starts with a full cabinet regardless of
|
|
104
|
+
// how well the LLM followed instructions.
|
|
105
|
+
for (const dept of promotionDepts) {
|
|
106
|
+
const hasLeader = kernel.getState().agents.some(c => c.promotion?.department === dept);
|
|
107
|
+
if (!hasLeader) {
|
|
108
|
+
const top = kernel.getCandidates(dept, 1)[0];
|
|
109
|
+
if (top) {
|
|
110
|
+
kernel.promoteAgent(top.core.id, dept, roleNames[dept] || `Head of ${dept}`, leader.name);
|
|
111
|
+
console.log(` ✦ [fallback] ${top.core.name} → ${roleNames[dept]}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=commander-setup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commander-setup.js","sourceRoot":"","sources":["../../../src/runtime/commander-setup.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAMH,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAE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,OAAO,GAAG,EAAE,CAAC;oBAAC,OAAO,CAAC,GAAG,CAAC,yBAAyB,GAAG,EAAE,CAAC,CAAC;gBAAC,CAAC;YAChE,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YAAC,OAAO,CAAC,IAAI,CAAC,+CAA+C,EAAE,CAAC,CAAC,CAAC;QAAC,CAAC;IACnF,CAAC;IAED,uEAAuE;IACvE,oEAAoE;IACpE,0CAA0C;IAC1C,KAAK,MAAM,IAAI,IAAI,cAAc,EAAE,CAAC;QAClC,MAAM,SAAS,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,UAAU,KAAK,IAAI,CAAC,CAAC;QACvF,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,MAAM,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YAC7C,IAAI,GAAG,EAAE,CAAC;gBACR,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,IAAI,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,WAAW,IAAI,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;gBAC1F,OAAO,CAAC,GAAG,CAAC,kBAAkB,GAAG,CAAC,IAAI,CAAC,IAAI,MAAM,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACtE,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-run cost + token telemetry with per-stage buckets.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from orchestrator.ts so the trackUsage / cache-savings math
|
|
5
|
+
* has one home, testable in isolation from the turn loop. The tracker
|
|
6
|
+
* is stateful (totals accumulate across every tagged LLM call) and
|
|
7
|
+
* returns a buildCostPayload() helper the orchestrator invokes before
|
|
8
|
+
* every SSE emit so the dashboard breakdown modal reflects the latest
|
|
9
|
+
* per-stage split + cache hit rate.
|
|
10
|
+
*
|
|
11
|
+
* Pricing lookup comes from pricing.ts so MODEL_PRICING has one home.
|
|
12
|
+
*
|
|
13
|
+
* @module paracosm/runtime/cost-tracker
|
|
14
|
+
*/
|
|
15
|
+
import type { SimulationModelConfig } from '../engine/types.js';
|
|
16
|
+
import { type CostSite } from './pricing.js';
|
|
17
|
+
/** Token + cost rollup for a single pipeline stage across a run. */
|
|
18
|
+
export interface CostBucket {
|
|
19
|
+
totalTokens: number;
|
|
20
|
+
totalCostUSD: number;
|
|
21
|
+
calls: number;
|
|
22
|
+
/** Cache-read tokens billed at 0.1× input rate (prompt-cache hit). */
|
|
23
|
+
cacheReadTokens: number;
|
|
24
|
+
/** Cache-write tokens billed at 1.25× input rate (new cache entry). */
|
|
25
|
+
cacheCreationTokens: number;
|
|
26
|
+
}
|
|
27
|
+
/** Shape of the `usage` field providers return on each LLM call. */
|
|
28
|
+
export interface CallUsage {
|
|
29
|
+
totalTokens?: number;
|
|
30
|
+
promptTokens?: number;
|
|
31
|
+
completionTokens?: number;
|
|
32
|
+
costUSD?: number;
|
|
33
|
+
cacheReadTokens?: number;
|
|
34
|
+
cacheCreationTokens?: number;
|
|
35
|
+
}
|
|
36
|
+
/** Per-stage entry on the breakdown payload attached to every SSE emit. */
|
|
37
|
+
export interface CostBreakdownEntry {
|
|
38
|
+
totalTokens: number;
|
|
39
|
+
totalCostUSD: number;
|
|
40
|
+
calls: number;
|
|
41
|
+
cacheReadTokens?: number;
|
|
42
|
+
cacheCreationTokens?: number;
|
|
43
|
+
/** USD saved vs no-caching run. Negative during turn 1, positive turn 2+. */
|
|
44
|
+
cacheSavingsUSD?: number;
|
|
45
|
+
}
|
|
46
|
+
/** Aggregate cost payload embedded on every SSE event's `_cost` field. */
|
|
47
|
+
export interface CostPayload {
|
|
48
|
+
totalTokens: number;
|
|
49
|
+
totalCostUSD: number;
|
|
50
|
+
llmCalls: number;
|
|
51
|
+
cacheReadTokens: number;
|
|
52
|
+
cacheCreationTokens: number;
|
|
53
|
+
/** Run-wide USD saved by prompt caching vs a no-cache run. */
|
|
54
|
+
cacheSavingsUSD: number;
|
|
55
|
+
breakdown: Record<string, CostBreakdownEntry>;
|
|
56
|
+
}
|
|
57
|
+
/** Cost tracker bundle returned by createCostTracker. */
|
|
58
|
+
export interface CostTracker {
|
|
59
|
+
/** Record token + cost usage from a single LLM call, tagged by site. */
|
|
60
|
+
trackUsage(result: {
|
|
61
|
+
usage?: CallUsage;
|
|
62
|
+
}, site?: CostSite): void;
|
|
63
|
+
/** Build the _cost payload attached to every SSE event. */
|
|
64
|
+
buildCostPayload(): CostPayload;
|
|
65
|
+
/** Final-run snapshot for the output JSON. */
|
|
66
|
+
finalCost(): {
|
|
67
|
+
totalTokens: number;
|
|
68
|
+
totalCostUSD: number;
|
|
69
|
+
llmCalls: number;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create a per-run cost tracker bound to a specific model config.
|
|
74
|
+
*
|
|
75
|
+
* Cache cost math: Anthropic bills cache_read at 0.10× input (saves
|
|
76
|
+
* 0.90×) and cache_creation at 1.25× (costs +0.25×). Net savings per
|
|
77
|
+
* site = (reads × 0.90 − creates × 0.25) × inputPrice. Turn 1 is a
|
|
78
|
+
* net COST because the cache hasn't amortized. Turn 3+ with reads
|
|
79
|
+
* dominating produces substantial savings.
|
|
80
|
+
*
|
|
81
|
+
* Fallback cost estimate (when provider omits costUSD) uses
|
|
82
|
+
* commander-tier pricing as an approximation. Previously this path
|
|
83
|
+
* ignored cache entirely so heavy-caching runs under-reported billed
|
|
84
|
+
* amount by ~10-15%.
|
|
85
|
+
*/
|
|
86
|
+
export declare function createCostTracker(modelConfig: SimulationModelConfig): CostTracker;
|
|
87
|
+
//# sourceMappingURL=cost-tracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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AAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;CAC/C;AAED,yDAAyD;AACzD,MAAM,WAAW,WAAW;IAC1B,wEAAwE;IACxE,UAAU,CAAC,MAAM,EAAE;QAAE,KAAK,CAAC,EAAE,SAAS,CAAA;KAAE,EAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,CAAC;IACjE,2DAA2D;IAC3D,gBAAgB,IAAI,WAAW,CAAC;IAChC,8CAA8C;IAC9C,SAAS,IAAI;QAAE,WAAW,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;CAC9E;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,qBAAqB,GAAG,WAAW,CA6FjF"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-run cost + token telemetry with per-stage buckets.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from orchestrator.ts so the trackUsage / cache-savings math
|
|
5
|
+
* has one home, testable in isolation from the turn loop. The tracker
|
|
6
|
+
* is stateful (totals accumulate across every tagged LLM call) and
|
|
7
|
+
* returns a buildCostPayload() helper the orchestrator invokes before
|
|
8
|
+
* every SSE emit so the dashboard breakdown modal reflects the latest
|
|
9
|
+
* per-stage split + cache hit rate.
|
|
10
|
+
*
|
|
11
|
+
* Pricing lookup comes from pricing.ts so MODEL_PRICING has one home.
|
|
12
|
+
*
|
|
13
|
+
* @module paracosm/runtime/cost-tracker
|
|
14
|
+
*/
|
|
15
|
+
import { buildPriceForSite, getDefaultPricing, } from './pricing.js';
|
|
16
|
+
/**
|
|
17
|
+
* Create a per-run cost tracker bound to a specific model config.
|
|
18
|
+
*
|
|
19
|
+
* Cache cost math: Anthropic bills cache_read at 0.10× input (saves
|
|
20
|
+
* 0.90×) and cache_creation at 1.25× (costs +0.25×). Net savings per
|
|
21
|
+
* site = (reads × 0.90 − creates × 0.25) × inputPrice. Turn 1 is a
|
|
22
|
+
* net COST because the cache hasn't amortized. Turn 3+ with reads
|
|
23
|
+
* dominating produces substantial savings.
|
|
24
|
+
*
|
|
25
|
+
* Fallback cost estimate (when provider omits costUSD) uses
|
|
26
|
+
* commander-tier pricing as an approximation. Previously this path
|
|
27
|
+
* ignored cache entirely so heavy-caching runs under-reported billed
|
|
28
|
+
* amount by ~10-15%.
|
|
29
|
+
*/
|
|
30
|
+
export function createCostTracker(modelConfig) {
|
|
31
|
+
let totalTokens = 0;
|
|
32
|
+
let totalCostUSD = 0;
|
|
33
|
+
let llmCalls = 0;
|
|
34
|
+
let cacheReadTokens = 0;
|
|
35
|
+
let cacheCreationTokens = 0;
|
|
36
|
+
const defaultPricing = getDefaultPricing(modelConfig);
|
|
37
|
+
const priceForSite = buildPriceForSite(modelConfig);
|
|
38
|
+
const newBucket = () => ({
|
|
39
|
+
totalTokens: 0, totalCostUSD: 0, calls: 0,
|
|
40
|
+
cacheReadTokens: 0, cacheCreationTokens: 0,
|
|
41
|
+
});
|
|
42
|
+
const costBySite = {
|
|
43
|
+
director: newBucket(),
|
|
44
|
+
commander: newBucket(),
|
|
45
|
+
departments: newBucket(),
|
|
46
|
+
judge: newBucket(),
|
|
47
|
+
reactions: newBucket(),
|
|
48
|
+
other: newBucket(),
|
|
49
|
+
};
|
|
50
|
+
const trackUsage = (result, site = 'other') => {
|
|
51
|
+
if (!result?.usage)
|
|
52
|
+
return;
|
|
53
|
+
const tokensThisCall = result.usage.totalTokens ?? 0;
|
|
54
|
+
const callCacheRead = result.usage.cacheReadTokens ?? 0;
|
|
55
|
+
const callCacheCreate = result.usage.cacheCreationTokens ?? 0;
|
|
56
|
+
let costThisCall;
|
|
57
|
+
if (typeof result.usage.costUSD === 'number') {
|
|
58
|
+
costThisCall = result.usage.costUSD;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const input = result.usage.promptTokens ?? 0;
|
|
62
|
+
const output = result.usage.completionTokens ?? 0;
|
|
63
|
+
costThisCall =
|
|
64
|
+
(input * defaultPricing.input / 1_000_000)
|
|
65
|
+
+ (output * defaultPricing.output / 1_000_000)
|
|
66
|
+
+ (callCacheRead * defaultPricing.input * 0.10 / 1_000_000)
|
|
67
|
+
+ (callCacheCreate * defaultPricing.input * 1.25 / 1_000_000);
|
|
68
|
+
}
|
|
69
|
+
totalTokens += tokensThisCall;
|
|
70
|
+
totalCostUSD += costThisCall;
|
|
71
|
+
llmCalls++;
|
|
72
|
+
cacheReadTokens += callCacheRead;
|
|
73
|
+
cacheCreationTokens += callCacheCreate;
|
|
74
|
+
const bucket = costBySite[site];
|
|
75
|
+
bucket.totalTokens += tokensThisCall;
|
|
76
|
+
bucket.totalCostUSD += costThisCall;
|
|
77
|
+
bucket.calls++;
|
|
78
|
+
bucket.cacheReadTokens += callCacheRead;
|
|
79
|
+
bucket.cacheCreationTokens += callCacheCreate;
|
|
80
|
+
};
|
|
81
|
+
const buildCostPayload = () => {
|
|
82
|
+
let runCacheSavingsUSD = 0;
|
|
83
|
+
const breakdown = {};
|
|
84
|
+
for (const [k, v] of Object.entries(costBySite)) {
|
|
85
|
+
if (v.calls > 0) {
|
|
86
|
+
const sitePricing = priceForSite(k);
|
|
87
|
+
const siteSavings = ((v.cacheReadTokens * 0.90) - (v.cacheCreationTokens * 0.25))
|
|
88
|
+
* sitePricing.input / 1_000_000;
|
|
89
|
+
runCacheSavingsUSD += siteSavings;
|
|
90
|
+
breakdown[k] = {
|
|
91
|
+
totalTokens: v.totalTokens,
|
|
92
|
+
totalCostUSD: Math.round(v.totalCostUSD * 10000) / 10000,
|
|
93
|
+
calls: v.calls,
|
|
94
|
+
...(v.cacheReadTokens > 0 ? { cacheReadTokens: v.cacheReadTokens } : {}),
|
|
95
|
+
...(v.cacheCreationTokens > 0 ? { cacheCreationTokens: v.cacheCreationTokens } : {}),
|
|
96
|
+
...(Math.abs(siteSavings) >= 0.0001
|
|
97
|
+
? { cacheSavingsUSD: Math.round(siteSavings * 10000) / 10000 }
|
|
98
|
+
: {}),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
totalTokens,
|
|
104
|
+
totalCostUSD: Math.round(totalCostUSD * 10000) / 10000,
|
|
105
|
+
llmCalls,
|
|
106
|
+
cacheReadTokens,
|
|
107
|
+
cacheCreationTokens,
|
|
108
|
+
cacheSavingsUSD: Math.round(runCacheSavingsUSD * 10000) / 10000,
|
|
109
|
+
breakdown,
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
const finalCost = () => ({
|
|
113
|
+
totalTokens,
|
|
114
|
+
totalCostUSD: Math.round(totalCostUSD * 10000) / 10000,
|
|
115
|
+
llmCalls,
|
|
116
|
+
});
|
|
117
|
+
return { trackUsage, buildCostPayload, finalCost };
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=cost-tracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cost-tracker.js","sourceRoot":"","sources":["../../../src/runtime/cost-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,EACL,iBAAiB,EACjB,iBAAiB,GAElB,MAAM,cAAc,CAAC;AAwDtB;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAkC;IAClE,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,IAAI,YAAY,GAAG,CAAC,CAAC;IACrB,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,eAAe,GAAG,CAAC,CAAC;IACxB,IAAI,mBAAmB,GAAG,CAAC,CAAC;IAE5B,MAAM,cAAc,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAC;IACtD,MAAM,YAAY,GAAG,iBAAiB,CAAC,WAAW,CAAC,CAAC;IAEpD,MAAM,SAAS,GAAG,GAAe,EAAE,CAAC,CAAC;QACnC,WAAW,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,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CAAC,CAAC,eAAe,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBACxE,GAAG,CAAC,CAAC,CAAC,mBAAmB,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,mBAAmB,EAAE,CAAC,CAAC,mBAAmB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;oBACpF,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,MAAM;wBACjC,CAAC,CAAC,EAAE,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,GAAG,KAAK,CAAC,GAAG,KAAK,EAAE;wBAC9D,CAAC,CAAC,EAAE,CAAC;iBACR,CAAC;YACJ,CAAC;QACH,CAAC;QACD,OAAO;YACL,WAAW;YACX,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,GAAG,KAAK;YACtD,QAAQ;YACR,eAAe;YACf,mBAAmB;YACnB,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,kBAAkB,GAAG,KAAK,CAAC,GAAG,KAAK;YAC/D,SAAS;SACV,CAAC;IACJ,CAAC,CAAC;IAEF,MAAM,SAAS,GAA6B,GAAG,EAAE,CAAC,CAAC;QACjD,WAAW;QACX,YAAY,EAAE,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,KAAK,CAAC,GAAG,KAAK;QACtD,QAAQ;KACT,CAAC,CAAC;IAEH,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,CAAC;AACrD,CAAC"}
|
|
@@ -102,48 +102,9 @@ export declare function runSimulation(leader: LeaderConfig, keyPersonnel: KeyPer
|
|
|
102
102
|
eventIndex: number;
|
|
103
103
|
})[];
|
|
104
104
|
/** Canonical deduplicated forged toolbox (parity with dashboard ToolboxSection). */
|
|
105
|
-
forgedToolbox:
|
|
106
|
-
name: string;
|
|
107
|
-
description: string;
|
|
108
|
-
mode: string;
|
|
109
|
-
firstForgedTurn: number;
|
|
110
|
-
firstForgedDepartment: string;
|
|
111
|
-
firstForgedEventTitle: string;
|
|
112
|
-
departments: string[];
|
|
113
|
-
/** Total uses minus the original forge. */
|
|
114
|
-
reuseCount: number;
|
|
115
|
-
/** Total uses including the original forge. */
|
|
116
|
-
totalUses: number;
|
|
117
|
-
/** How many of those uses were re-forge attempts. */
|
|
118
|
-
reforgeCount: number;
|
|
119
|
-
/** Re-forge attempts that the judge rejected. */
|
|
120
|
-
rejectedReforges: number;
|
|
121
|
-
approved: boolean;
|
|
122
|
-
confidence: number;
|
|
123
|
-
inputSchema: unknown;
|
|
124
|
-
outputSchema: unknown;
|
|
125
|
-
sampleOutput: unknown;
|
|
126
|
-
/** Full audit trail: every invocation with turn, year, dept, output. */
|
|
127
|
-
history: Array<{
|
|
128
|
-
turn: number;
|
|
129
|
-
year: number;
|
|
130
|
-
eventIndex: number;
|
|
131
|
-
eventTitle: string;
|
|
132
|
-
department: string;
|
|
133
|
-
output: string | null;
|
|
134
|
-
isReforge: boolean;
|
|
135
|
-
rejected: boolean;
|
|
136
|
-
confidence?: number;
|
|
137
|
-
}>;
|
|
138
|
-
}[];
|
|
105
|
+
forgedToolbox: import("./tool-ledger.js").ForgedToolboxEntry[];
|
|
139
106
|
/** Canonical deduplicated citation catalog (parity with References). */
|
|
140
|
-
citationCatalog:
|
|
141
|
-
departments: string[];
|
|
142
|
-
turns: number[];
|
|
143
|
-
text: string;
|
|
144
|
-
url: string;
|
|
145
|
-
doi?: string;
|
|
146
|
-
}[];
|
|
107
|
+
citationCatalog: import("./citations-catalog.js").CitationCatalogEntry[];
|
|
147
108
|
/** Per-turn agent reactions with mood + quote + memory snippets. */
|
|
148
109
|
agentReactions: {
|
|
149
110
|
turn: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orchestrator.d.ts","sourceRoot":"","sources":["../../../src/runtime/orchestrator.ts"],"names":[],"mappings":"AAI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