@@ -34,186 +34,10 @@ import YAML from 'yaml';
3434import { agent as createAgent } from './agent.js' ;
3535import { agency as createAgency } from './agency.js' ;
3636import type { BaseAgentConfig , AgencyOptions , AgencyStrategy , Agent } from './types.js' ;
37-
38- // ============================================================================
39- // EXPORT CONFIG TYPE
40- // ============================================================================
41-
42- /**
43- * Portable agent configuration envelope.
44- *
45- * Wraps a `BaseAgentConfig` with version metadata, export timestamp,
46- * and type discriminator so import logic can reconstruct the correct agent
47- * variant (single agent vs. multi-agent agency).
48- */
49- export interface AgentExportConfig {
50- /** Schema version for forward-compatible deserialization. */
51- version : '1.0.0' ;
52-
53- /** ISO 8601 timestamp of when the export was created. */
54- exportedAt : string ;
55-
56- /**
57- * Discriminator: `'agent'` for a single-agent export, `'agency'` for
58- * a multi-agent export that includes a sub-agent roster.
59- */
60- type : 'agent' | 'agency' ;
61-
62- /** The full agent configuration. */
63- config : BaseAgentConfig ;
64-
65- // -- Agency-specific fields (present when `type === 'agency'`) --
66-
67- /** Sub-agent roster keyed by agent name. Present for agency exports. */
68- agents ?: Record < string , BaseAgentConfig > ;
69-
70- /** Orchestration strategy. Present for agency exports. */
71- strategy ?: AgencyStrategy ;
72-
73- /** Whether runtime strategy adaptation is enabled. */
74- adaptive ?: boolean ;
75-
76- /** Maximum orchestration rounds for iterative strategies. */
77- maxRounds ?: number ;
78-
79- // -- Optional metadata --
80-
81- /** Human-readable metadata about the export (name, author, tags, etc.). */
82- metadata ?: {
83- /** Display name for the exported agent. */
84- name ?: string ;
85- /** Free-text description of what this agent does. */
86- description ?: string ;
87- /** Author identifier (person or system). */
88- author ?: string ;
89- /** Searchable tags for categorization. */
90- tags ?: string [ ] ;
91- } ;
92- }
93-
94- // ============================================================================
95- // EXPORT FUNCTIONS
96- // ============================================================================
97-
98- /**
99- * Extracts the stored configuration from an Agent instance.
100- *
101- * The agent's config is captured at creation time by the `agent()` and
102- * `agency()` factories and attached as a non-enumerable `__config` property.
103- * If the property is absent (e.g. the agent was created by external code),
104- * we return an empty config.
105- *
106- * @param agentInstance - The agent to extract config from.
107- * @returns The stored BaseAgentConfig or an empty object.
108- */
109- function extractConfig ( agentInstance : Agent ) : BaseAgentConfig {
110- // The __config property is set by our patched agent/agency factories
111- const config = ( agentInstance as unknown as Record < string , unknown > ) . __config ;
112- if ( config && typeof config === 'object' ) {
113- return config as BaseAgentConfig ;
114- }
115- // Fallback: no stored config — return empty
116- return { } ;
117- }
118-
119- /**
120- * Extracts agency-specific fields from an Agent instance that was created
121- * by the `agency()` factory.
122- *
123- * @param agentInstance - The agent/agency to extract from.
124- * @returns Agency fields if present, or undefined for plain agents.
125- */
126- function extractAgencyFields ( agentInstance : Agent ) :
127- | {
128- agents ?: Record < string , BaseAgentConfig > ;
129- strategy ?: AgencyStrategy ;
130- adaptive ?: boolean ;
131- maxRounds ?: number ;
132- }
133- | undefined {
134- const raw = ( agentInstance as unknown as Record < string , unknown > ) . __agencyConfig ;
135- if ( raw && typeof raw === 'object' ) {
136- return raw as {
137- agents ?: Record < string , BaseAgentConfig > ;
138- strategy ?: AgencyStrategy ;
139- adaptive ?: boolean ;
140- maxRounds ?: number ;
141- } ;
142- }
143- return undefined ;
144- }
145-
146- /**
147- * Exports an agent's configuration as a portable {@link AgentExportConfig} object.
148- *
149- * Captures the full `BaseAgentConfig` including model, instructions,
150- * personality, tools, guardrails, memory, RAG, voice, channels, and all
151- * other configuration surfaces. For agency instances, the sub-agent roster,
152- * strategy, and round limits are also included.
153- *
154- * @param agentInstance - The agent (or agency) instance to export.
155- * @param metadata - Optional human-readable metadata to attach to the export.
156- * @returns A portable config object that can be serialized to JSON or YAML.
157- *
158- * @example
159- * ```ts
160- * const config = exportAgentConfig(myAgent, {
161- * name: 'Research Assistant',
162- * author: 'team-alpha',
163- * tags: ['research', 'summarization'],
164- * });
165- * ```
166- */
167- export function exportAgentConfig (
168- agentInstance : Agent ,
169- metadata ?: AgentExportConfig [ 'metadata' ]
170- ) : AgentExportConfig {
171- const config = extractConfig ( agentInstance ) ;
172- const agencyFields = extractAgencyFields ( agentInstance ) ;
173-
174- const isAgency = ! ! agencyFields ?. agents ;
175-
176- const exportConfig : AgentExportConfig = {
177- version : '1.0.0' ,
178- exportedAt : new Date ( ) . toISOString ( ) ,
179- type : isAgency ? 'agency' : 'agent' ,
180- config,
181- } ;
182-
183- // Attach agency-specific fields when present
184- if ( isAgency && agencyFields ) {
185- exportConfig . agents = agencyFields . agents ;
186- exportConfig . strategy = agencyFields . strategy ;
187- exportConfig . adaptive = agencyFields . adaptive ;
188- exportConfig . maxRounds = agencyFields . maxRounds ;
189- }
190-
191- if ( metadata ) {
192- exportConfig . metadata = metadata ;
193- }
194-
195- return exportConfig ;
196- }
197-
198- /**
199- * Exports an agent's configuration as a pretty-printed JSON string.
200- *
201- * @param agentInstance - The agent (or agency) instance to export.
202- * @param metadata - Optional human-readable metadata to attach.
203- * @returns JSON string with 2-space indentation.
204- *
205- * @example
206- * ```ts
207- * const json = exportAgentConfigJSON(myAgent);
208- * fs.writeFileSync('agent.json', json);
209- * ```
210- */
211- export function exportAgentConfigJSON (
212- agentInstance : Agent ,
213- metadata ?: AgentExportConfig [ 'metadata' ]
214- ) : string {
215- return JSON . stringify ( exportAgentConfig ( agentInstance , metadata ) , null , 2 ) ;
216- }
37+ import { exportAgentConfig , exportAgentConfigJSON } from './agentExportCore.js' ;
38+ export { exportAgentConfig , exportAgentConfigJSON } ;
39+ export type { AgentExportConfig } from './agentExportCore.js' ;
40+ import type { AgentExportConfig } from './agentExportCore.js' ;
21741
21842/**
21943 * Exports an agent's configuration as a YAML string.
0 commit comments