@nylorun/runtime 0.1.1-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.
- package/CHANGELOG.md +29 -0
- package/LICENSE +192 -0
- package/README.md +44 -0
- package/dist/adapters/journal.d.ts +35 -0
- package/dist/adapters/journal.js +130 -0
- package/dist/adapters/media.d.ts +40 -0
- package/dist/adapters/media.js +161 -0
- package/dist/assets.d.ts +2 -0
- package/dist/assets.js +10 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +319 -0
- package/dist/config.d.ts +10 -0
- package/dist/config.js +15 -0
- package/dist/contracts.d.ts +147 -0
- package/dist/contracts.js +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/model/auth-store.d.ts +10 -0
- package/dist/model/auth-store.js +85 -0
- package/dist/model/configure.d.ts +12 -0
- package/dist/model/configure.js +115 -0
- package/dist/model/models.d.ts +9 -0
- package/dist/model/models.js +31 -0
- package/dist/model/pi-model.d.ts +10 -0
- package/dist/model/pi-model.js +166 -0
- package/dist/model/settings.d.ts +3 -0
- package/dist/model/settings.js +35 -0
- package/dist/server/ag-ui.d.ts +8 -0
- package/dist/server/ag-ui.js +67 -0
- package/dist/server/digests.d.ts +11 -0
- package/dist/server/digests.js +39 -0
- package/dist/server/host.d.ts +14 -0
- package/dist/server/host.js +589 -0
- package/package.json +53 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.1-beta
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 54ab304: Configure the provider and model after project installation and before development
|
|
8
|
+
starts. Add --skip-config for deferred setup and require it for noninteractive
|
|
9
|
+
creation. Retain the project with recovery instructions when setup fails or is
|
|
10
|
+
cancelled, and cancel pending prompts, authentication, and child processes on
|
|
11
|
+
shutdown.
|
|
12
|
+
- 54ab304: Add `--host` and `--allowed-hosts` to `nylorun dev` and `nylorun start`, with
|
|
13
|
+
`HOST` and `ALLOWED_HOSTS` environment equivalents. Loopback binds now answer only
|
|
14
|
+
to their own address on the chosen port, so local development needs no setup on
|
|
15
|
+
any port while DNS-rebinding pages are rejected. Publish with `--host 0.0.0.0` or
|
|
16
|
+
list proxy host names to deploy on your own servers.
|
|
17
|
+
- 54ab304: Support portable Runtime protocol version 2 while retaining legacy Studio
|
|
18
|
+
manifest support. Move the Studio CLI into Runtime: use `nylorun studio
|
|
19
|
+
--agent-url <url>` instead of `nylo studio`. Applications should install Runtime
|
|
20
|
+
directly and keep Studio as a development dependency.
|
|
21
|
+
|
|
22
|
+
Retain session lists and media across development reloads, and recognize agent
|
|
23
|
+
file changes on Windows. Validate noninteractive creator startup during release
|
|
24
|
+
verification with deferred provider configuration.
|
|
25
|
+
|
|
26
|
+
Include Harness in this release so the creator does not rely on an unpublished
|
|
27
|
+
compatibility pin. Ship and verify all four packages together.
|
|
28
|
+
|
|
29
|
+
Release notes are maintained with Changesets.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
Copyright 2026 Nylo
|
|
181
|
+
|
|
182
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
183
|
+
you may not use this file except in compliance with the License.
|
|
184
|
+
You may obtain a copy of the License at
|
|
185
|
+
|
|
186
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
187
|
+
|
|
188
|
+
Unless required by applicable law or agreed to in writing, software
|
|
189
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
190
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
191
|
+
See the License for the specific language governing permissions and
|
|
192
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @nylorun/runtime
|
|
2
|
+
|
|
3
|
+
Portable agent hosting, pi-ai model providers, and the `nylorun` CLI. Runtime has no Harness dependency. Studio is loaded from the application only when requested.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { defineRuntime } from "@nylorun/runtime";
|
|
7
|
+
import { agents } from "./agent/registry.js";
|
|
8
|
+
export default defineRuntime({ agents });
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Applications install their engine and Runtime directly, with Studio as a development dependency. An agent may bind Runtime's `piModel()` through its engine's model interface. `piModel({ media })` uses the explicitly supplied media adapter to resolve opaque image references. It never fetches arbitrary media URLs.
|
|
12
|
+
|
|
13
|
+
## Commands
|
|
14
|
+
|
|
15
|
+
Run from the directory containing `nylorun.config.ts`:
|
|
16
|
+
|
|
17
|
+
- `nylorun dev [--no-studio] [--no-open] [--port 4111] [--host 127.0.0.1] [--allowed-hosts a,b]`
|
|
18
|
+
- `nylorun studio --agent-url http://127.0.0.1:4111 [--port 3000] [--no-open]`
|
|
19
|
+
- `nylorun configure`
|
|
20
|
+
- `nylorun inspect`
|
|
21
|
+
- `nylorun build`
|
|
22
|
+
- `nylorun start [--port 4111] [--host 127.0.0.1] [--allowed-hosts a,b]`
|
|
23
|
+
|
|
24
|
+
`PORT`, `HOST` and `ALLOWED_HOSTS` environment variables supply the same settings. By default the host binds to loopback and answers only to `localhost`, `127.0.0.1` and `[::1]` on the chosen port, so changing `--port` needs no other setup and Studio connects to whatever address is printed. To publish, bind with `--host 0.0.0.0` (every Host header is then served) or keep the loopback bind behind a reverse proxy and list the public names in `--allowed-hosts`; `*` accepts any Host header. Development retains existing sessions across reloads; new sessions use the latest definitions. A failed reload leaves the previous runtime active. Studio attachment does not start or own the remote runtime.
|
|
25
|
+
|
|
26
|
+
Build emits `dist/nylorun.config.js` and application assets under `dist/agent/`. Deploy `dist/`, your package manifest and lockfile, installed production dependencies, and private configuration. Production startup does not import Studio. Use `projectAsset("agent/skills/catalog")` for assets that must resolve in source and built deployments.
|
|
27
|
+
|
|
28
|
+
## Configuration and adapters
|
|
29
|
+
|
|
30
|
+
The default host uses in-memory history and text input. Add `persistence: localJsonl()` and `media: localMedia()` explicitly for local storage under `.data/sessions` and `.data/media`. Share the same media adapter with `piModel({ media })` and domain tools. Persistence preserves history across restarts; archived sessions cannot resume execution and require a new conversation.
|
|
31
|
+
|
|
32
|
+
The creator runs `nylorun configure` after installation and before development unless `--skip-config` is passed. Standalone configuration also supports scripted input. EOF fails setup; Ctrl-C/SIGTERM cancels prompts and authentication with exit status 130/143. A cancelled setup does not proceed to development.
|
|
33
|
+
|
|
34
|
+
`nylorun configure` stores provider credentials in `.env/auth.json` and selection in `config/model.json`. Agents can be imported before setup; provider authentication is resolved when the model is called. Optional integration environment variables are loaded from `.env/integrations.env`. Existing flat `.env` files must be relocated before creating the vault; never commit credentials. Image-editing and external coding integrations may use their own credentials independently.
|
|
35
|
+
|
|
36
|
+
## Portable contracts
|
|
37
|
+
|
|
38
|
+
`RuntimeAgent` supplies `id`, `name`, a matching `manifest`, and `run({ id })`. Its session supplies `input`, `stream`, `observe`, and `stop`. An input handle's `completed` promise settles after that input's execution or pause, with ordered lifecycle events and a status. Runtime uses completion events for terminal output and interactions, and observation events for optional diagnostics. Engines need not emit model/tool diagnostics or middleware metadata. Runtime records input before execution and flushes persistence before returning an HTTP reply. `stop()` releases session resources; optional agent `close()` runs after session shutdown. Shutdown is idempotent.
|
|
39
|
+
|
|
40
|
+
`RuntimeModelAdapter` accepts ordered prompt items, tools, model controls, opaque media references, and an abort signal, returning portable text/reasoning/tool-call outputs. It has no engine import. Integration tests in the creator check assignment in both directions with Harness without casts. Runtime's own tests use an independent engine.
|
|
41
|
+
|
|
42
|
+
Discovery and manifest documents use protocol version 2. The agent's manifest is under `manifest`; no engine-branded envelope is required. Existing agent-scoped HTTP endpoint paths remain unchanged. Studio also understands legacy version-1 `harness.manifest` documents.
|
|
43
|
+
|
|
44
|
+
Repository development: [contributing](../CONTRIBUTING.md). Package publication: [releasing](../RELEASING.md).
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type CanonicalEvent = Readonly<{
|
|
2
|
+
session: string;
|
|
3
|
+
seq: number;
|
|
4
|
+
ts: string;
|
|
5
|
+
type: string;
|
|
6
|
+
payload: Record<string, unknown>;
|
|
7
|
+
}>;
|
|
8
|
+
export type SessionSummary = Readonly<{
|
|
9
|
+
session: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
status: "idle" | "waiting";
|
|
12
|
+
startedAt: number;
|
|
13
|
+
endedAt?: number;
|
|
14
|
+
}>;
|
|
15
|
+
/** Explicit local-only JSONL persistence. Raw provider payloads never enter this service. */
|
|
16
|
+
export declare class JsonlJournal {
|
|
17
|
+
private readonly root;
|
|
18
|
+
private readonly secrets;
|
|
19
|
+
constructor(root: string, secrets: readonly string[]);
|
|
20
|
+
append(agent: string, event: CanonicalEvent): Promise<void>;
|
|
21
|
+
events(agent: string, session: string): Promise<readonly CanonicalEvent[]>;
|
|
22
|
+
list(agent: string): Promise<readonly SessionSummary[]>;
|
|
23
|
+
private file;
|
|
24
|
+
}
|
|
25
|
+
export declare function scrub(value: unknown, secrets: readonly string[]): unknown;
|
|
26
|
+
export interface RuntimePersistence {
|
|
27
|
+
append(agent: string, event: CanonicalEvent): Promise<void>;
|
|
28
|
+
events(agent: string, session: string): Promise<readonly CanonicalEvent[]>;
|
|
29
|
+
list(agent: string): Promise<readonly SessionSummary[]>;
|
|
30
|
+
}
|
|
31
|
+
export declare function localJsonl(options?: {
|
|
32
|
+
root?: string;
|
|
33
|
+
secrets?: readonly string[];
|
|
34
|
+
}): RuntimePersistence;
|
|
35
|
+
export declare function memoryHistory(): RuntimePersistence;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { appendFile, mkdir, readFile, readdir } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
/** Explicit local-only JSONL persistence. Raw provider payloads never enter this service. */
|
|
4
|
+
export class JsonlJournal {
|
|
5
|
+
root;
|
|
6
|
+
secrets;
|
|
7
|
+
constructor(root, secrets) {
|
|
8
|
+
this.root = root;
|
|
9
|
+
this.secrets = secrets;
|
|
10
|
+
}
|
|
11
|
+
async append(agent, event) {
|
|
12
|
+
const file = this.file(agent, event.session);
|
|
13
|
+
await mkdir(join(this.root, agent, event.session), { recursive: true });
|
|
14
|
+
await appendFile(file, `${JSON.stringify(scrub(event, this.secrets))}\n`);
|
|
15
|
+
}
|
|
16
|
+
async events(agent, session) {
|
|
17
|
+
try {
|
|
18
|
+
return Object.freeze((await readFile(this.file(agent, session), "utf8"))
|
|
19
|
+
.split("\n")
|
|
20
|
+
.filter(Boolean)
|
|
21
|
+
.flatMap((line) => {
|
|
22
|
+
try {
|
|
23
|
+
return [JSON.parse(line)];
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return Object.freeze([]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async list(agent) {
|
|
35
|
+
try {
|
|
36
|
+
const ids = await readdir(join(this.root, agent));
|
|
37
|
+
const summaries = await Promise.all(ids.map(async (session) => {
|
|
38
|
+
const events = await this.events(agent, session);
|
|
39
|
+
const final = events.findLast((event) => event.type === "final");
|
|
40
|
+
const title = sessionTitle(events);
|
|
41
|
+
return {
|
|
42
|
+
session,
|
|
43
|
+
...(title === undefined ? {} : { title }),
|
|
44
|
+
status: sessionStatus(events),
|
|
45
|
+
startedAt: events[0] ? Date.parse(events[0].ts) : 0,
|
|
46
|
+
...(final === undefined ? {} : { endedAt: Date.parse(final.ts) }),
|
|
47
|
+
};
|
|
48
|
+
}));
|
|
49
|
+
return Object.freeze(summaries.sort((a, b) => b.startedAt - a.startedAt));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return Object.freeze([]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
file(agent, session) {
|
|
56
|
+
return join(this.root, safe(agent), safe(session), "events.jsonl");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function sessionTitle(events) {
|
|
60
|
+
for (const event of events) {
|
|
61
|
+
if (event.type !== "session.run.started" ||
|
|
62
|
+
typeof event.payload.input !== "string")
|
|
63
|
+
continue;
|
|
64
|
+
const title = event.payload.input.replace(/\s+/gu, " ").trim();
|
|
65
|
+
if (title)
|
|
66
|
+
return title;
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
function sessionStatus(events) {
|
|
71
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
72
|
+
const event = events[index];
|
|
73
|
+
if (event.type === "final")
|
|
74
|
+
return "idle";
|
|
75
|
+
if (event.type === "interaction.required")
|
|
76
|
+
return "waiting";
|
|
77
|
+
if (event.type === "session.run.started" &&
|
|
78
|
+
typeof event.payload.approved === "boolean")
|
|
79
|
+
return "idle";
|
|
80
|
+
}
|
|
81
|
+
return "idle";
|
|
82
|
+
}
|
|
83
|
+
function safe(value) {
|
|
84
|
+
if (value === "." || value === ".." || !/^[a-zA-Z0-9._-]+$/u.test(value)) {
|
|
85
|
+
throw new Error("Refusing a path-shaped ID.");
|
|
86
|
+
}
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
export function scrub(value, secrets) {
|
|
90
|
+
if (typeof value === "string")
|
|
91
|
+
return secrets.reduce((text, secret) => secret.length >= 8 ? text.split(secret).join("[redacted]") : text, value.replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/giu, "[inline image data redacted]"));
|
|
92
|
+
if (Array.isArray(value))
|
|
93
|
+
return value.map((item) => scrub(item, secrets));
|
|
94
|
+
if (value && typeof value === "object")
|
|
95
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [
|
|
96
|
+
key,
|
|
97
|
+
/^(authorization|api[_-]?key|(?:access|refresh|id|auth)[_-]?token|token|secret|password|cookie|credentials?)$/iu.test(key)
|
|
98
|
+
? "[redacted]"
|
|
99
|
+
: scrub(item, secrets),
|
|
100
|
+
]));
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
export function localJsonl(options = {}) {
|
|
104
|
+
return new JsonlJournal(options.root ?? join(process.cwd(), ".data", "sessions"), options.secrets ?? []);
|
|
105
|
+
}
|
|
106
|
+
export function memoryHistory() {
|
|
107
|
+
const agents = new Map();
|
|
108
|
+
return {
|
|
109
|
+
async append(agent, event) {
|
|
110
|
+
const sessions = agents.get(agent) ?? new Map();
|
|
111
|
+
agents.set(agent, sessions);
|
|
112
|
+
const events = sessions.get(event.session) ?? [];
|
|
113
|
+
events.push(event);
|
|
114
|
+
sessions.set(event.session, events);
|
|
115
|
+
},
|
|
116
|
+
async events(agent, session) {
|
|
117
|
+
return agents.get(agent)?.get(session) ?? [];
|
|
118
|
+
},
|
|
119
|
+
async list(agent) {
|
|
120
|
+
return [...(agents.get(agent) ?? [])]
|
|
121
|
+
.map(([session, events]) => ({
|
|
122
|
+
session,
|
|
123
|
+
title: sessionTitle(events),
|
|
124
|
+
status: sessionStatus(events),
|
|
125
|
+
startedAt: Date.parse(events[0].ts),
|
|
126
|
+
}))
|
|
127
|
+
.sort((a, b) => b.startedAt - a.startedAt);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export declare const IMAGE_MEDIA_TYPES: readonly string[];
|
|
2
|
+
export declare const MAX_IMAGE_BYTES: number;
|
|
3
|
+
export interface MediaAsset {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly mediaType: string;
|
|
6
|
+
readonly bytes: number;
|
|
7
|
+
readonly kind: "input" | "generated";
|
|
8
|
+
}
|
|
9
|
+
/** Opaque reference retained by Harness and resolved by the configured media adapter. */
|
|
10
|
+
export interface MediaReference {
|
|
11
|
+
readonly agentId: string;
|
|
12
|
+
readonly assetId: string;
|
|
13
|
+
}
|
|
14
|
+
/** Explicit local asset store; agents receive only opaque references. */
|
|
15
|
+
export declare class MediaStore {
|
|
16
|
+
private readonly root;
|
|
17
|
+
constructor(root: string);
|
|
18
|
+
saveInput(agentId: string, sessionId: string, mediaType: string, base64: string): Promise<MediaAsset>;
|
|
19
|
+
saveGenerated(agentId: string, sessionId: string, mediaType: string, bytes: Uint8Array): Promise<MediaAsset>;
|
|
20
|
+
dataUrl(reference: MediaReference, sessionId: string): Promise<{
|
|
21
|
+
readonly asset: MediaAsset;
|
|
22
|
+
readonly url: string;
|
|
23
|
+
} | undefined>;
|
|
24
|
+
latestInput(agentId: string, sessionId: string): Promise<MediaAsset | undefined>;
|
|
25
|
+
read(agentId: string, sessionId: string, assetId: string): Promise<{
|
|
26
|
+
asset: MediaAsset;
|
|
27
|
+
bytes: Uint8Array;
|
|
28
|
+
} | undefined>;
|
|
29
|
+
private save;
|
|
30
|
+
private readMetadata;
|
|
31
|
+
private directory;
|
|
32
|
+
}
|
|
33
|
+
/** Decode one canonical base64 image before it is persisted or sent to a provider. */
|
|
34
|
+
export declare function decodeImageBase64(mediaType: string, base64: string): Uint8Array;
|
|
35
|
+
/** Verify the asserted media type and lightweight file signature for a supported image. */
|
|
36
|
+
export declare function validateImageBytes(mediaType: string, bytes: Uint8Array): void;
|
|
37
|
+
export declare function localMedia(options?: {
|
|
38
|
+
root?: string;
|
|
39
|
+
}): MediaStore;
|
|
40
|
+
export type RuntimeMedia = Pick<MediaStore, "saveInput" | "saveGenerated" | "dataUrl" | "latestInput" | "read">;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
export const IMAGE_MEDIA_TYPES = Object.freeze([
|
|
5
|
+
"image/jpeg",
|
|
6
|
+
"image/png",
|
|
7
|
+
"image/webp",
|
|
8
|
+
]);
|
|
9
|
+
export const MAX_IMAGE_BYTES = 8 * 1024 * 1024;
|
|
10
|
+
/** Explicit local asset store; agents receive only opaque references. */
|
|
11
|
+
export class MediaStore {
|
|
12
|
+
root;
|
|
13
|
+
constructor(root) {
|
|
14
|
+
this.root = root;
|
|
15
|
+
}
|
|
16
|
+
async saveInput(agentId, sessionId, mediaType, base64) {
|
|
17
|
+
const bytes = decodeImageBase64(mediaType, base64);
|
|
18
|
+
return this.save(agentId, sessionId, "input", mediaType, bytes);
|
|
19
|
+
}
|
|
20
|
+
async saveGenerated(agentId, sessionId, mediaType, bytes) {
|
|
21
|
+
validateImageBytes(mediaType, bytes);
|
|
22
|
+
return this.save(agentId, sessionId, "generated", mediaType, bytes);
|
|
23
|
+
}
|
|
24
|
+
async dataUrl(reference, sessionId) {
|
|
25
|
+
const stored = await this.read(reference.agentId, sessionId, reference.assetId);
|
|
26
|
+
if (!stored)
|
|
27
|
+
return undefined;
|
|
28
|
+
return Object.freeze({
|
|
29
|
+
asset: stored.asset,
|
|
30
|
+
url: `data:${stored.asset.mediaType};base64,${Buffer.from(stored.bytes).toString("base64")}`,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async latestInput(agentId, sessionId) {
|
|
34
|
+
const directory = this.directory(agentId, sessionId);
|
|
35
|
+
try {
|
|
36
|
+
const metadata = await Promise.all((await readdir(directory))
|
|
37
|
+
.filter((name) => name.endsWith(".json"))
|
|
38
|
+
.map(async (name) => this.readMetadata(directory, name)));
|
|
39
|
+
return metadata
|
|
40
|
+
.filter((entry) => entry?.kind === "input")
|
|
41
|
+
.sort((left, right) => right.createdAt - left.createdAt)[0];
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async read(agentId, sessionId, assetId) {
|
|
48
|
+
if (!safe(agentId) || !safe(sessionId) || !safe(assetId))
|
|
49
|
+
return undefined;
|
|
50
|
+
const directory = this.directory(agentId, sessionId);
|
|
51
|
+
const metadata = await this.readMetadata(directory, `${assetId}.json`);
|
|
52
|
+
if (!metadata)
|
|
53
|
+
return undefined;
|
|
54
|
+
try {
|
|
55
|
+
return {
|
|
56
|
+
asset: metadata,
|
|
57
|
+
bytes: await readFile(join(directory, `${assetId}.bin`)),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async save(agentId, sessionId, kind, mediaType, bytes) {
|
|
65
|
+
if (!safe(agentId) || !safe(sessionId))
|
|
66
|
+
throw new Error("Invalid media asset scope.");
|
|
67
|
+
const directory = this.directory(agentId, sessionId);
|
|
68
|
+
const asset = {
|
|
69
|
+
id: randomUUID(),
|
|
70
|
+
mediaType,
|
|
71
|
+
bytes: bytes.byteLength,
|
|
72
|
+
kind,
|
|
73
|
+
createdAt: Date.now(),
|
|
74
|
+
};
|
|
75
|
+
await mkdir(directory, { recursive: true });
|
|
76
|
+
await writeFile(join(directory, `${asset.id}.bin`), bytes);
|
|
77
|
+
await writeFile(join(directory, `${asset.id}.json`), JSON.stringify(asset));
|
|
78
|
+
return omitCreatedAt(asset);
|
|
79
|
+
}
|
|
80
|
+
async readMetadata(directory, name) {
|
|
81
|
+
try {
|
|
82
|
+
const value = JSON.parse(await readFile(join(directory, name), "utf8"));
|
|
83
|
+
return validAsset(value) ? value : undefined;
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
directory(agentId, sessionId) {
|
|
90
|
+
return join(this.root, agentId, sessionId);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** Decode one canonical base64 image before it is persisted or sent to a provider. */
|
|
94
|
+
export function decodeImageBase64(mediaType, base64) {
|
|
95
|
+
if (!IMAGE_MEDIA_TYPES.includes(mediaType))
|
|
96
|
+
throw new Error("Only JPEG, PNG, and WebP images are supported.");
|
|
97
|
+
if (typeof base64 !== "string" || base64 === "")
|
|
98
|
+
throw new Error("Image data is required.");
|
|
99
|
+
if (base64.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/u.test(base64))
|
|
100
|
+
throw new Error("Image data must be canonical base64.");
|
|
101
|
+
const bytes = Buffer.from(base64, "base64");
|
|
102
|
+
if (bytes.toString("base64") !== base64)
|
|
103
|
+
throw new Error("Image data must be canonical base64.");
|
|
104
|
+
validateImageBytes(mediaType, bytes);
|
|
105
|
+
return bytes;
|
|
106
|
+
}
|
|
107
|
+
/** Verify the asserted media type and lightweight file signature for a supported image. */
|
|
108
|
+
export function validateImageBytes(mediaType, bytes) {
|
|
109
|
+
if (!IMAGE_MEDIA_TYPES.includes(mediaType))
|
|
110
|
+
throw new Error("Only JPEG, PNG, and WebP images are supported.");
|
|
111
|
+
if (bytes.byteLength === 0 || bytes.byteLength > MAX_IMAGE_BYTES)
|
|
112
|
+
throw new Error("Images must be no larger than 8 MiB.");
|
|
113
|
+
const signature = mediaType === "image/jpeg"
|
|
114
|
+
? bytes.byteLength >= 3 &&
|
|
115
|
+
bytes[0] === 0xff &&
|
|
116
|
+
bytes[1] === 0xd8 &&
|
|
117
|
+
bytes[2] === 0xff
|
|
118
|
+
: mediaType === "image/png"
|
|
119
|
+
? bytes.byteLength >= 8 &&
|
|
120
|
+
bytes[0] === 0x89 &&
|
|
121
|
+
bytes[1] === 0x50 &&
|
|
122
|
+
bytes[2] === 0x4e &&
|
|
123
|
+
bytes[3] === 0x47 &&
|
|
124
|
+
bytes[4] === 0x0d &&
|
|
125
|
+
bytes[5] === 0x0a &&
|
|
126
|
+
bytes[6] === 0x1a &&
|
|
127
|
+
bytes[7] === 0x0a
|
|
128
|
+
: bytes.byteLength >= 12 &&
|
|
129
|
+
bytes[0] === 0x52 &&
|
|
130
|
+
bytes[1] === 0x49 &&
|
|
131
|
+
bytes[2] === 0x46 &&
|
|
132
|
+
bytes[3] === 0x46 &&
|
|
133
|
+
bytes[8] === 0x57 &&
|
|
134
|
+
bytes[9] === 0x45 &&
|
|
135
|
+
bytes[10] === 0x42 &&
|
|
136
|
+
bytes[11] === 0x50;
|
|
137
|
+
if (!signature)
|
|
138
|
+
throw new Error(`Image bytes do not match ${mediaType}.`);
|
|
139
|
+
}
|
|
140
|
+
function validAsset(value) {
|
|
141
|
+
if (!value || typeof value !== "object")
|
|
142
|
+
return false;
|
|
143
|
+
const asset = value;
|
|
144
|
+
return (typeof asset.id === "string" &&
|
|
145
|
+
safe(asset.id) &&
|
|
146
|
+
typeof asset.mediaType === "string" &&
|
|
147
|
+
IMAGE_MEDIA_TYPES.includes(asset.mediaType) &&
|
|
148
|
+
typeof asset.bytes === "number" &&
|
|
149
|
+
(asset.kind === "input" || asset.kind === "generated") &&
|
|
150
|
+
typeof asset.createdAt === "number");
|
|
151
|
+
}
|
|
152
|
+
function omitCreatedAt(asset) {
|
|
153
|
+
const { createdAt: _createdAt, ...result } = asset;
|
|
154
|
+
return Object.freeze(result);
|
|
155
|
+
}
|
|
156
|
+
function safe(value) {
|
|
157
|
+
return value !== "." && value !== ".." && /^[a-zA-Z0-9._-]+$/u.test(value);
|
|
158
|
+
}
|
|
159
|
+
export function localMedia(options = {}) {
|
|
160
|
+
return new MediaStore(options.root ?? join(process.cwd(), ".data", "media"));
|
|
161
|
+
}
|
package/dist/assets.d.ts
ADDED
package/dist/assets.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { resolve, relative } from "node:path";
|
|
3
|
+
/** Resolve authored catalogs and subprocess assets in source or a built application. */
|
|
4
|
+
export function projectAsset(path) {
|
|
5
|
+
const root = process.cwd();
|
|
6
|
+
const source = resolve(root, path);
|
|
7
|
+
if (relative(root, source).startsWith(".."))
|
|
8
|
+
throw new Error("Asset must be inside the project.");
|
|
9
|
+
return existsSync(source) ? source : resolve(root, "dist", path);
|
|
10
|
+
}
|
package/dist/cli.d.ts
ADDED