@nylorun/agents 0.14.0-beta → 0.16.0-beta

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.
@@ -1,100 +0,0 @@
1
- /** The definitions an application serves Actions for (`createActionHandler`). */
2
- import { AgentManifestSchema } from "@nylorun/core/contracts";
3
- import { agentFrom, delegateOf, hashManifest, isFlowDelegate, isBuiltWorkflow, } from "@nylorun/core/define";
4
- import { assertNoMiddlewareClosures } from "./client.js";
5
- import { env } from "./http.js";
6
- /** The implementation version to register: the option, `NYLORUN_IMPLEMENTATION_VERSION`, or `dev`. */
7
- export function implementationVersionOf(options) {
8
- return (options.implementationVersion ??
9
- env("NYLORUN_IMPLEMENTATION_VERSION") ??
10
- "dev");
11
- }
12
- /** Agents embedded in a v2 workflow: served here, but saved only as part of the workflow. */
13
- const embeddedAgents = new WeakMap();
14
- /** The ids among `agents` that are saved only as part of another definition. */
15
- export function embeddedIn(agents) {
16
- return embeddedAgents.get(agents) ?? new Set();
17
- }
18
- /**
19
- * Every definition `sources` serves, by id: the agents and workflows passed in, the agents
20
- * a v2 workflow embeds, and flow agents used as tools. `owner` names the caller in errors.
21
- */
22
- export function buildAgents(sources, owner) {
23
- const agents = new Map();
24
- const embedded = new Set();
25
- /**
26
- * Agents embedded in another definition (a v2 flow's agents, a flow agent used as a
27
- * tool) may be reached from more than one place; the same definition is served once.
28
- */
29
- const sameAsServed = (id, manifest) => {
30
- const served = agents.get(id);
31
- if (!served)
32
- return false;
33
- if (hashManifest(served.manifest) === hashManifest(manifest))
34
- return true;
35
- throw new Error(`Duplicate connected agent ${id}`);
36
- };
37
- const addAgent = (built, shared = false) => {
38
- assertNoMiddlewareClosures(built);
39
- AgentManifestSchema.parse(built.manifest);
40
- if (shared ? sameAsServed(built.id, built.manifest) : agents.has(built.id)) {
41
- if (!shared)
42
- throw new Error(`Duplicate connected agent ${built.id}`);
43
- return;
44
- }
45
- agents.set(built.id, built);
46
- for (const tool of built.getBinding().tools) {
47
- const delegate = delegateOf(tool);
48
- if (!delegate || !isFlowDelegate(delegate))
49
- continue;
50
- // A flow agent used as a tool runs in its own linked session: serve its code too.
51
- if (!delegate.workflow)
52
- throw new Error(`Flow agent '${delegate.manifest.id}' is used as a tool by '${built.id}' but has no local implementation`);
53
- addWorkflow(delegate.workflow, true);
54
- embedded.add(delegate.workflow.id);
55
- }
56
- };
57
- const addWorkflow = (workflow, shared = false) => {
58
- if (shared ? sameAsServed(workflow.id, workflow.manifest) : agents.has(workflow.id)) {
59
- if (!shared)
60
- throw new Error(`Duplicate connected agent ${workflow.id}`);
61
- return;
62
- }
63
- agents.set(workflow.id, workflow);
64
- const v2 = workflow.manifest.workflowSchemaVersion === 2;
65
- for (const binding of Object.values(workflow.getBinding().agents)) {
66
- addAgent(agentFrom(binding.manifest, binding.implementations), v2);
67
- if (v2)
68
- embedded.add(binding.manifest.id);
69
- }
70
- };
71
- for (const source of sources) {
72
- if (isBuiltWorkflow(source)) {
73
- addWorkflow(source);
74
- continue;
75
- }
76
- const built = source.build?.() ?? source;
77
- if (isBuiltWorkflow(built)) {
78
- addWorkflow(built);
79
- continue;
80
- }
81
- addAgent(built);
82
- }
83
- if (!agents.size)
84
- throw new Error(`${owner} requires at least one agent`);
85
- // A definition passed in directly is saved on its own, even if another one embeds it.
86
- for (const source of sources)
87
- embedded.delete(source.id);
88
- embeddedAgents.set(agents, embedded);
89
- return agents;
90
- }
91
- /**
92
- * The manifest hash served for a workflow's flow actions. A run is pinned to one workflow
93
- * manifest, and its stage keys may shift between deploys, so flow actions for another hash
94
- * are left for whoever serves it.
95
- */
96
- export function flowManifestHash(agent) {
97
- return isBuiltWorkflow(agent) && agent.manifest.workflowSchemaVersion === 2
98
- ? hashManifest(agent.manifest)
99
- : undefined;
100
- }