@amalgm/agents 0.2.3-preview.34176069706 → 0.2.3-preview.34546331677

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.
@@ -0,0 +1,12 @@
1
+ import type { AgentBundle, BundleHeadType } from './types.js';
2
+ export interface CopyBundleOptions {
3
+ /** Fresh identity for one construct; the default is random. */
4
+ freshId?: (type: BundleHeadType) => string;
5
+ now?: () => string;
6
+ }
7
+ /**
8
+ * A copy is the same content under fresh identities: no construct of the
9
+ * source is named anywhere in it, and its graph is rebuilt around the new
10
+ * names. It is how a published copy cuts lineage.
11
+ */
12
+ export declare function copyAgentBundle(source: AgentBundle, options?: CopyBundleOptions): AgentBundle;
@@ -0,0 +1,142 @@
1
+ import crypto from 'node:crypto';
2
+ import { buildBundleGraph } from './graph.js';
3
+ import { actionEntryId, agentEntryId, appEntryId, automationEntryId, toolEntryId } from './graph-records.js';
4
+ import { bundleId, clean, clone, object } from './util.js';
5
+ /**
6
+ * The identity map of one copy: every construct of the source bundle to its
7
+ * fresh identity. Tool action ids follow their tool: `<tool>.<action>`.
8
+ */
9
+ class IdentityMap {
10
+ #ids = new Map();
11
+ #fresh;
12
+ constructor(fresh) {
13
+ this.#fresh = fresh;
14
+ }
15
+ assign(type, id) {
16
+ if (!id)
17
+ throw new Error(`bundle ${type} entries must include ids`);
18
+ const next = this.#fresh(type);
19
+ this.#ids.set(id, next);
20
+ return next;
21
+ }
22
+ assignAction(id, toolId) {
23
+ const tool = this.#ids.get(toolId);
24
+ if (!tool)
25
+ throw new Error(`tool action ${id} references missing tool: ${toolId}`);
26
+ const separator = id.lastIndexOf('.');
27
+ const next = `${tool}${separator >= 0 ? id.slice(separator) : `.${id}`}`;
28
+ this.#ids.set(id, next);
29
+ return next;
30
+ }
31
+ of(id) {
32
+ return this.#ids.get(id) ?? id;
33
+ }
34
+ strings(value) {
35
+ return Array.isArray(value) ? value.filter((item) => typeof item === 'string').map((item) => this.of(item)) : [];
36
+ }
37
+ /** Rewrite every `actionId` in a compiled plan; keys are the plan's own. */
38
+ plan(value) {
39
+ if (Array.isArray(value))
40
+ return value.map((item) => this.plan(item));
41
+ const record = object(value);
42
+ if (!record)
43
+ return value;
44
+ return Object.fromEntries(Object.entries(record).map(([key, child]) => [
45
+ key,
46
+ (key === 'actionId' || key === 'action_id') && typeof child === 'string' ? this.of(child) : this.plan(child),
47
+ ]));
48
+ }
49
+ /** `automation:<id>:field` bindings and `tools.<id>.path` secret paths; ids may contain dots. */
50
+ requirement(value) {
51
+ const binding = value.match(/^(automation|tool|toolAction):([^:]+):(.*)$/);
52
+ if (binding)
53
+ return `${binding[1]}:${this.of(binding[2])}:${binding[3]}`;
54
+ const secret = value.match(/^(tools|toolActions)\.(.*)$/);
55
+ if (!secret)
56
+ return value;
57
+ for (const [id, next] of this.#ids) {
58
+ if (secret[2].startsWith(`${id}.`))
59
+ return `${secret[1]}.${next}${secret[2].slice(id.length)}`;
60
+ }
61
+ return value;
62
+ }
63
+ }
64
+ function defaultFreshId(type) {
65
+ const id = crypto.randomUUID();
66
+ return type === 'tool' ? `tool-${id.replace(/-/g, '').slice(0, 16)}` : `${type}-${id}`;
67
+ }
68
+ function isSystemTool(tool) {
69
+ return tool.origin === 'system';
70
+ }
71
+ /**
72
+ * A copy is the same content under fresh identities: no construct of the
73
+ * source is named anywhere in it, and its graph is rebuilt around the new
74
+ * names. It is how a published copy cuts lineage.
75
+ */
76
+ export function copyAgentBundle(source, options = {}) {
77
+ const bundle = clone(source);
78
+ const ids = new IdentityMap(options.freshId ?? defaultFreshId);
79
+ for (const entry of bundle.agents)
80
+ ids.assign('agent', agentEntryId(entry));
81
+ for (const entry of bundle.automations)
82
+ ids.assign('automation', automationEntryId(entry));
83
+ for (const entry of bundle.apps)
84
+ ids.assign('app', appEntryId(entry));
85
+ for (const tool of bundle.tools)
86
+ if (!isSystemTool(tool))
87
+ ids.assign('tool', toolEntryId(tool));
88
+ for (const action of bundle.toolActions) {
89
+ const toolId = clean(action.toolId);
90
+ const tool = bundle.tools.find((candidate) => toolEntryId(candidate) === toolId);
91
+ if (tool && !isSystemTool(tool))
92
+ ids.assignAction(actionEntryId(action), toolId);
93
+ }
94
+ for (const entry of bundle.agents) {
95
+ entry.sourceAgentId = ids.of(entry.sourceAgentId);
96
+ entry.config = {
97
+ ...entry.config,
98
+ ...(entry.config.agentId ? { agentId: entry.sourceAgentId } : {}),
99
+ loadout: { toolIds: ids.strings(entry.config.loadout.toolIds) },
100
+ subagents: entry.config.subagents.map((subagent) => ({ ...subagent, agentId: ids.of(subagent.agentId) })),
101
+ };
102
+ }
103
+ for (const entry of bundle.automations) {
104
+ entry.sourceAutomationId = ids.of(clean(entry.sourceAutomationId));
105
+ entry.toolIds = ids.strings(entry.toolIds);
106
+ if (Array.isArray(entry.workflows)) {
107
+ entry.workflows = entry.workflows.map((workflow) => {
108
+ const record = object(workflow);
109
+ return record ? { ...record, compiled: ids.plan(record.compiled ?? null) } : workflow;
110
+ });
111
+ }
112
+ }
113
+ for (const entry of bundle.apps) {
114
+ entry.sourceAppId = ids.of(clean(entry.sourceAppId));
115
+ entry.toolIds = ids.strings(entry.toolIds);
116
+ const manifest = object(entry.manifest);
117
+ if (manifest && Array.isArray(manifest.toolIds))
118
+ entry.manifest = { ...manifest, toolIds: ids.strings(manifest.toolIds) };
119
+ }
120
+ for (const tool of bundle.tools) {
121
+ tool.id = ids.of(toolEntryId(tool));
122
+ if (clean(tool.appId))
123
+ tool.appId = ids.of(clean(tool.appId));
124
+ }
125
+ for (const action of bundle.toolActions) {
126
+ action.id = ids.of(actionEntryId(action));
127
+ action.toolId = ids.of(clean(action.toolId));
128
+ if (clean(action.appId))
129
+ action.appId = ids.of(clean(action.appId));
130
+ }
131
+ bundle.heads = bundle.heads.map((head) => ({ ...head, id: ids.of(head.id) }));
132
+ bundle.rootAgentId = bundle.rootAgentId ? ids.of(bundle.rootAgentId) : null;
133
+ bundle.requires = {
134
+ ...bundle.requires,
135
+ bindings: bundle.requires.bindings.map((value) => ids.requirement(value)),
136
+ secrets: bundle.requires.secrets.map((value) => ids.requirement(value)),
137
+ };
138
+ bundle.bundleId = bundleId();
139
+ bundle.createdAt = options.now?.() || new Date().toISOString();
140
+ Object.assign(bundle, buildBundleGraph(bundle));
141
+ return bundle;
142
+ }
@@ -1,6 +1,8 @@
1
+ import { copyAgentBundle } from '../bundles/copy.js';
1
2
  import { createAgentBundle } from '../bundles/create.js';
2
3
  import { installAgentBundle } from '../bundles/install.js';
3
4
  import { BUNDLE_KIND, BUNDLE_SCHEMA_VERSION } from '../bundles/util.js';
5
+ import { validateAgentBundle } from '../bundles/validate.js';
4
6
  import { scanInstalledSkills } from '../skills/scanner.js';
5
7
  function ids(...values) {
6
8
  const result = [];
@@ -44,6 +46,14 @@ export async function routeAgentBundles(context) {
44
46
  context.json(200, { ok: true, ...result });
45
47
  return true;
46
48
  }
49
+ if (action === 'copy') {
50
+ if (!body.bundle) {
51
+ context.json(400, { error: 'bundle is required' });
52
+ return true;
53
+ }
54
+ context.json(200, { ok: true, bundle: copyAgentBundle(validateAgentBundle(body.bundle)) });
55
+ return true;
56
+ }
47
57
  if (action === 'install') {
48
58
  if (!body.bundle) {
49
59
  context.json(400, { error: 'bundle is required' });
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ export { configFromAgent, listAgentConfigs, updateAgentConfig } from './config/s
11
11
  export { importAgentConfig } from './config/import.js';
12
12
  export { importNativeConfig } from './config/native.js';
13
13
  export type * from './config/types.js';
14
+ export { copyAgentBundle } from './bundles/copy.js';
15
+ export type { CopyBundleOptions } from './bundles/copy.js';
14
16
  export { createAgentBundle } from './bundles/create.js';
15
17
  export type { CreateBundleOptions } from './bundles/create.js';
16
18
  export { installAgentBundle } from './bundles/install.js';
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ export { normalizeAgentConfig, stableConfigId } from './config/schema.js';
7
7
  export { configFromAgent, listAgentConfigs, updateAgentConfig } from './config/store.js';
8
8
  export { importAgentConfig } from './config/import.js';
9
9
  export { importNativeConfig } from './config/native.js';
10
+ export { copyAgentBundle } from './bundles/copy.js';
10
11
  export { createAgentBundle } from './bundles/create.js';
11
12
  export { installAgentBundle } from './bundles/install.js';
12
13
  export { buildBundleGraph } from './bundles/graph.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amalgm/agents",
3
- "version": "0.2.3-preview.34176069706",
3
+ "version": "0.2.3-preview.34546331677",
4
4
  "description": "Local-first agent identities, installations, and immutable revisions.",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -58,7 +58,7 @@
58
58
  "node": ">=24"
59
59
  },
60
60
  "dependencies": {
61
- "@amalgm/core": "0.4.8-preview.34168975242",
61
+ "@amalgm/core": "0.5.1-preview.34403852460",
62
62
  "@types/better-sqlite3": "^7.6.13",
63
63
  "@types/node": "^20.19.43",
64
64
  "better-sqlite3": "^12.6.2"
@@ -89,11 +89,11 @@
89
89
  "package.json"
90
90
  ]
91
91
  },
92
- "gitHead": "195724cf8a10bf834740ff0b4b9855fd69138e00",
92
+ "gitHead": "8f86bd5edf730c9e06744ec94371b1eabb91acf3",
93
93
  "amalgmSource": {
94
94
  "repository": "amalgm-inc/amalgm-agents",
95
95
  "branch": "preview",
96
- "commit": "195724cf8a10bf834740ff0b4b9855fd69138e00",
97
- "occurrence": "34176069706"
96
+ "commit": "8f86bd5edf730c9e06744ec94371b1eabb91acf3",
97
+ "occurrence": "34546331677"
98
98
  }
99
99
  }