@swirls/deploy 0.0.0-stage → 0.2.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/README.md +150 -2
- package/dist/index.d.ts +4697 -0
- package/dist/index.js +23035 -0
- package/package.json +45 -4
package/README.md
CHANGED
|
@@ -1,3 +1,151 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @swirls/deploy
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lowers `.swirls` source files into a deployment definition: the JSON document a runtime stores and runs, described by `@swirls/definition`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { lowerSwirlsSources, swirlsHost } from '@swirls/deploy'
|
|
7
|
+
|
|
8
|
+
const result = lowerSwirlsSources(
|
|
9
|
+
{
|
|
10
|
+
'intake.swirls': source,
|
|
11
|
+
'flows/normalize.swirls': other,
|
|
12
|
+
'flows/lower-case.ts.swirls': code,
|
|
13
|
+
},
|
|
14
|
+
{ host: swirlsHost },
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
if (!result.ok) {
|
|
18
|
+
for (const d of result.diagnostics) {
|
|
19
|
+
console.error(`${d.path ?? ''}:${d.range?.start.line ?? 0}: ${d.message}`)
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
store(result.definition, result.sourceFiles, result.skillFiles)
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
`lowerSwirlsSources` reads nothing from disk or from the environment. The files passed in are the whole project, keyed by posix path relative to the project root. A problem in the sources comes back as a diagnostic and is not thrown, so every problem in every file is reported at once.
|
|
27
|
+
|
|
28
|
+
## `lowerSwirlsSources(files, options?)`
|
|
29
|
+
|
|
30
|
+
| Argument | Type | Meaning |
|
|
31
|
+
| --------------- | ------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
|
32
|
+
| `files` | `Map<string, string>` or `Record<string, string>` | Path to text. Include `.swirls` files, the `.ts.swirls` files they reference, and `.agents/skills/<name>/` files of skills in use. |
|
|
33
|
+
| `options.host` | `LanguageHost` | The runtime's provider catalog, `@openui` checker and harness catalog. Without it those checks are skipped. |
|
|
34
|
+
| `options.newId` | `() => string` | Makes the id of every resource and node. Defaults to a UUIDv7. Pass a counter for reproducible output. |
|
|
35
|
+
|
|
36
|
+
It returns a `LowerResult`:
|
|
37
|
+
|
|
38
|
+
| Field | Meaning |
|
|
39
|
+
| ------------------- | ------------------------------------------------------------------------------------------------------- |
|
|
40
|
+
| `ok` | True when a definition was built. Warnings do not make it false. |
|
|
41
|
+
| `definition` | The definition as a JSON document, or `null`. It has passed `parseDeploymentDefinition`. |
|
|
42
|
+
| `diagnostics` | `{ severity, stage, message, path?, range?, related? }` for every error and warning. |
|
|
43
|
+
| `sourceFiles` | Each `.swirls` file with its size and SHA-256, sorted by path. |
|
|
44
|
+
| `sourceFingerprint` | A stable hash of `sourceFiles`. |
|
|
45
|
+
| `skillFiles` | The files of each skill an agent uses, base64 encoded. |
|
|
46
|
+
| `ast` | The merged syntax tree, or `null`. `buildDeploymentSourceManifest` takes it to map resources to source. |
|
|
47
|
+
|
|
48
|
+
A diagnostic's `stage` says which step reported it: `mount` for an input that cannot be used (an unusable path), `parse` and `validate` for one file, `merge` for combining files (a name declared twice, at each declaration with `related` naming the others, or files declaring different `version`s), `lower` for building the definition, and `definition` for the built document failing its schema.
|
|
49
|
+
|
|
50
|
+
`lowerSwirlsSources` is `lowerSwirlsMounts` over one mount, with the outputs in this shape.
|
|
51
|
+
|
|
52
|
+
## Several directories as one program
|
|
53
|
+
|
|
54
|
+
`lowerSwirlsMounts(mounts, options?)` lowers several directories, called mounts, into one definition. Each mount is `{ id, files, skillsRoot? }`:
|
|
55
|
+
|
|
56
|
+
| Field | Meaning |
|
|
57
|
+
| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
58
|
+
| `id` | Chosen by the host, unique among the mounts of one call, at least one character. Any string works: it is never used as a path. It names the mount in every output and never appears in the definition. |
|
|
59
|
+
| `files` | `Map` or record of mount-relative posix path to text or bytes (`Uint8Array`). `.swirls` and `.ts.swirls` files and operation scripts must be UTF-8; skill files are kept byte for byte, so they may be binary. |
|
|
60
|
+
| `skillsRoot` | The mount-relative directory holding the mount's skills, `<skillsRoot>/<skill>/SKILL.md`. Defaults to `.agents/skills` (`MOUNT_SKILLS_ROOT`). |
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { lowerSwirlsMounts, swirlsHost } from '@swirls/deploy'
|
|
64
|
+
|
|
65
|
+
const result = lowerSwirlsMounts(
|
|
66
|
+
[
|
|
67
|
+
{ id: 'intake', files: { 'intake.swirls': intake } },
|
|
68
|
+
{ id: 'flows', files: { 'greet.swirls': greet, '.agents/skills/kit/SKILL.md': kit } },
|
|
69
|
+
],
|
|
70
|
+
{ host: swirlsHost },
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
for (const d of result.diagnostics) {
|
|
74
|
+
console.error(`${d.mount ?? ''}:${d.path ?? ''}:${d.range?.start.line ?? 0}: ${d.message}`)
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
What holds across mounts:
|
|
79
|
+
|
|
80
|
+
- **One namespace.** A block may name a block of another mount by its bare name. A name is unique per kind across every mount (a form and a workflow may share one).
|
|
81
|
+
- **Confinement.** A file may read only files of its own mount: a `@ts "…"` reference or an operation's `@sh "…"` / `@ps "…"` script that resolves outside the mount is a `validate` error at the referencing file, `"<ref>" is outside this directory`.
|
|
82
|
+
- **Every duplicate, at every site.** A name declared more than once gives one `merge` error per declaration, at its mount, path and range, with `related` listing every other declaration.
|
|
83
|
+
- **Version.** Every file that declares `version:` must declare the same value; otherwise each such file gets a `merge` error.
|
|
84
|
+
- **Skills.** A skill is read from the skills root of the mount that declares its `skill` block, which need not be the mount of the agent that uses it. Two mounts may hold the same skill directory name under different `skill` blocks.
|
|
85
|
+
- **Inputs.** An empty or repeated mount id, a file path that is absolute, empty or holds `.`, `..` or a backslash, and a `skillsRoot` outside the mount are `mount` errors. Nothing in the inputs makes the call throw.
|
|
86
|
+
|
|
87
|
+
It returns a `LowerMountsResult`: `ok`, `definition` and `ast` as for `lowerSwirlsSources`; `diagnostics`, each with `mount` when it belongs to one and with mount-relative `path`s; `sourceFiles` (each with `mount`, sorted by mount id then path); `skillFiles` (each bundle with the `mount` it was read from, sorted by block name); `manifest` (version 2, below); `mounts` (each mount's `sources` and `skills` fingerprint, sorted by id) and `fingerprint`.
|
|
88
|
+
|
|
89
|
+
### Ids by site
|
|
90
|
+
|
|
91
|
+
`options.newId(site, mount)` makes every id. `site` says what the id is for and is unique within one call; `mount` is the mount that declares it. Without it every id is a UUIDv7 drawn in call order. A host that derives ids from the site (a UUIDv8 of a hash of the site and something of its own, say) gets identical ids for identical sources, and the ids of one mount stay the same when another mount changes. Every id must be a UUID. The sites are a stable contract:
|
|
92
|
+
|
|
93
|
+
| What | Site |
|
|
94
|
+
| ----------------------------- | --------------------------------------------- |
|
|
95
|
+
| A named block of kind `k` | `<k>:<name>`, `k` as in the manifest's `kind` |
|
|
96
|
+
| A workflow's node | `workflow:<workflow>/node:<node>` |
|
|
97
|
+
| A workflow's edge | `workflow:<workflow>/edge:<source>-><target>` |
|
|
98
|
+
| A synthetic subgraph workflow | `workflow:<its synthesized name>` |
|
|
99
|
+
| A database's connection | `database:<database>/connection:<connection>` |
|
|
100
|
+
|
|
101
|
+
A site that would repeat within one call (two edges between one pair of nodes, say) gets `#2`, `#3`, … in call order. The mount of a synthetic workflow is its parent's.
|
|
102
|
+
|
|
103
|
+
### Manifest v2
|
|
104
|
+
|
|
105
|
+
`manifest` places every named block: `{ version: 2, mounts, files, resources, nodes, fingerprint }`, where every file, resource and node carries its `mount`. Resources cover every named kind, including `computer`, `query`, `operation`, `role`, `mock` and `playbook`; a role has no `id`. Policies have no name and are not listed, and neither are the nodes of synthetic subgraph workflows. `deploymentSourceManifestV2Schema` and `parseDeploymentSourceManifestV2` check one. Version 1 (`buildDeploymentSourceManifest`) is unchanged.
|
|
106
|
+
|
|
107
|
+
### Fingerprints
|
|
108
|
+
|
|
109
|
+
A mount's `sources` is the SHA-256 of its `.swirls` and `.ts.swirls` files (path and SHA-256 of each, sorted by path: the formula of `computeDeploymentSourceFingerprint`). Its `skills` is the same over every file under its skills root, whether or not an agent uses it. `fingerprint` hashes every mount's id, `sources` and `skills`, sorted by id: it does not change with the order mounts are given in, and it does change when a file moves from one mount to another.
|
|
110
|
+
|
|
111
|
+
## The steps, for a host that reads a directory
|
|
112
|
+
|
|
113
|
+
`lowerSwirlsSources` is these steps over an in-memory file system. They are exported for a caller that wants to read a real directory or stop between them. The first two throw on a problem in the sources.
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import fs from 'node:fs'
|
|
117
|
+
import {
|
|
118
|
+
buildDeploymentDefinitionPayload,
|
|
119
|
+
collectDeploymentSkillFiles,
|
|
120
|
+
collectDeploymentSourceFiles,
|
|
121
|
+
collectReferencedSkillBlocksFromAst,
|
|
122
|
+
computeDeploymentSourceFingerprint,
|
|
123
|
+
createNodeDeployBundleFs,
|
|
124
|
+
mergeProjectSwirlsFiles,
|
|
125
|
+
} from '@swirls/deploy'
|
|
126
|
+
|
|
127
|
+
const files = createNodeDeployBundleFs(fs)
|
|
128
|
+
const merged = mergeProjectSwirlsFiles(files, dir, { host })
|
|
129
|
+
const { definition, parseError } = buildDeploymentDefinitionPayload(merged, { newId })
|
|
130
|
+
const sourceFiles = collectDeploymentSourceFiles(files, dir)
|
|
131
|
+
const skillFiles = collectDeploymentSkillFiles(
|
|
132
|
+
files,
|
|
133
|
+
dir,
|
|
134
|
+
collectReferencedSkillBlocksFromAst(merged),
|
|
135
|
+
)
|
|
136
|
+
const name = computeDeploymentSourceFingerprint(sourceFiles).slice(0, 12)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Also exported: `createMemoryRepoFs` (a file system over a path-to-text map), `computeDeploymentDefinitionFingerprint` (a hash of a definition that ignores ids and ordering), `computeDefinitionHash`, `buildDeploymentSourceManifest`, `compileComputerRecipe`, `compileOperationBlock`, `resolveSkillTree` and `classifySkillBundle`.
|
|
140
|
+
|
|
141
|
+
## What is not here
|
|
142
|
+
|
|
143
|
+
Storing a deployment is the runtime's job. This package does not upload skill files or sources anywhere, and it does not build computer images: `compileComputerRecipe` only works out the recipe and its hash.
|
|
144
|
+
|
|
145
|
+
## Tests
|
|
146
|
+
|
|
147
|
+
`bun test` runs the unit tests and a golden test that lowers every recipe in the repository's [`cookbook`](../../cookbook) and compares the result with the output recorded under `src/__golden__/cookbook`.
|
|
148
|
+
|
|
149
|
+
## Build
|
|
150
|
+
|
|
151
|
+
`bun run build` writes `dist/index.js` and `dist/index.d.ts`. `@swirls/language` and `@swirls/definition` are compiled in, so the package depends only on `zod`, `ajv` and `pathe`. It imports `node:crypto` and `node:fs`, so it runs on Node 20 or later, Bun, and Workers with Node compatibility turned on.
|