@fungi.computer/knapsack 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/LICENSE.codex +201 -0
- package/README.md +140 -2
- package/dist/files-patch.d.ts +30 -0
- package/dist/files-patch.js +22 -0
- package/dist/files-read-codec.d.ts +10 -0
- package/dist/files-read-codec.js +26 -0
- package/dist/files-read.d.ts +25 -0
- package/dist/files-read.js +43 -0
- package/dist/files.d.ts +170 -0
- package/dist/files.js +514 -0
- package/dist/index.d.ts +70 -0
- package/dist/index.js +178 -0
- package/dist/mcp.d.ts +54 -0
- package/dist/mcp.js +237 -0
- package/dist/patch-grammar.d.ts +1 -0
- package/dist/patch-grammar.js +3 -0
- package/dist/patch.d.ts +27 -0
- package/dist/patch.js +56 -0
- package/docs/api/README.md +17 -0
- package/docs/api/files-patch.md +72 -0
- package/docs/api/files-read-codec.md +74 -0
- package/docs/api/files-read.md +94 -0
- package/docs/api/index.md +733 -0
- package/docs/api/patch.md +84 -0
- package/package.json +84 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jamstack Consulting LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/LICENSE.codex
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
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
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2025 OpenAI
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,141 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fungi.computer/knapsack
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Knapsack collects Agent tool connections. A connection discovers one external
|
|
4
|
+
capability provider and projects its advertised operations into a Mycelium
|
|
5
|
+
`CapabilitySource`.
|
|
6
|
+
|
|
7
|
+
The first connection is `Knapsack.computer`. A Computer provider continues to
|
|
8
|
+
own its operation implementations, schemas, storage, and execution. Knapsack
|
|
9
|
+
parses the provider's advertisement and forwards generic calls; Mycelium owns
|
|
10
|
+
the resulting capability lifecycle and execute door.
|
|
11
|
+
|
|
12
|
+
`Knapsack.computers` captures a host's Computer list. Computers are described
|
|
13
|
+
concurrently under a timeout; one that rejects, hangs, or advertises an unusable
|
|
14
|
+
schema is dropped and reported through `onUnavailable`, while the remaining
|
|
15
|
+
Computers stay available. There is no implicit shared Files module. A connection
|
|
16
|
+
may supply its own `files` port and `namespace: ["computers"]`; its advertised
|
|
17
|
+
commands and nested `files.*` then share that Computer’s authority id under
|
|
18
|
+
`env.computers.<name>`. Failed acquisition removes that Computer’s Files with
|
|
19
|
+
its commands.
|
|
20
|
+
|
|
21
|
+
`Knapsack.files` projects one bounded Files port as the `files` module and, when
|
|
22
|
+
the host supplies a patch engine, the direct `apply_patch` tool. Both decode
|
|
23
|
+
through the same Zod schemas.
|
|
24
|
+
|
|
25
|
+
`Knapsack.mcpServers` connects a host-owned list of remote MCP servers with the
|
|
26
|
+
maintained MCP SDK client over Streamable HTTP. Each server becomes the module
|
|
27
|
+
`env.mcp.<name>`, one operation per advertised tool. A tool name maps onto the
|
|
28
|
+
guest identifier grammar (`create-note` becomes `create_note`). Its input schema
|
|
29
|
+
decodes through Forage's JSON Schema subset; a schema outside that subset still
|
|
30
|
+
accepts a JSON object, and the server validates it. A tool replays only when the
|
|
31
|
+
server marks it `readOnlyHint`. A call returns the tool's `structuredContent`,
|
|
32
|
+
or `{ isError, content }` when there is none or the tool failed. The host
|
|
33
|
+
supplies the `fetch` that carries every request and any headers, such as a
|
|
34
|
+
bearer token; redirects are refused, and headers never reach the snapshot, the
|
|
35
|
+
catalog or a result. Servers connect concurrently under a timeout. One that
|
|
36
|
+
rejects, refuses its credential, times out or advertises an unusable list is
|
|
37
|
+
dropped and reported through `onUnavailable`, while the others stay available.
|
|
38
|
+
|
|
39
|
+
Knapsack does not own model loops, tool execution policy, Computer storage, or
|
|
40
|
+
provider-specific operations.
|
|
41
|
+
|
|
42
|
+
`@fungi.computer/knapsack/files-patch` exposes the same patch input/output
|
|
43
|
+
contracts without loading tool connections or the agent runtime. Browser and
|
|
44
|
+
transport consumers use this subpath.
|
|
45
|
+
|
|
46
|
+
## Set up
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
npm install @fungi.computer/knapsack
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
The package is an ESM public candidate, not a claim of npm publication. It
|
|
53
|
+
depends on Cairn, Forage, Mule, Mycelium, the MCP SDK client, Effect, Zod and
|
|
54
|
+
`diff`. Its explicit file list contains compiled JavaScript and declarations,
|
|
55
|
+
the API reference, this README and the license texts. It has no install scripts.
|
|
56
|
+
|
|
57
|
+
## Quick start: validate a read request
|
|
58
|
+
|
|
59
|
+
This example uses the same public decoder as the packed-consumer proof in
|
|
60
|
+
[test/stranger/runtime.mjs](test/stranger/runtime.mjs). It validates a request;
|
|
61
|
+
it does not grant file access or perform I/O.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { readInputSchema } from "@fungi.computer/knapsack";
|
|
65
|
+
|
|
66
|
+
export const request = readInputSchema.parse({
|
|
67
|
+
path: "notes.md",
|
|
68
|
+
offset: 0,
|
|
69
|
+
limit: 4,
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Core concepts
|
|
74
|
+
|
|
75
|
+
The host supplies an already admitted Computer or Files port. Knapsack does not
|
|
76
|
+
mint credentials or choose a Team, member, Computer, path root or permission.
|
|
77
|
+
`Knapsack.computer({ id, name, computer })` captures the Computer's advertised
|
|
78
|
+
operations. `Knapsack.computers` subscribes to a host-owned connection list and
|
|
79
|
+
invalidates its captured modules when that list changes.
|
|
80
|
+
|
|
81
|
+
Computer calls preserve Mycelium's invocation context, including its execution
|
|
82
|
+
identity, abort signal and deadline. Advertised Computer operations are marked
|
|
83
|
+
unsafe to replay because their implementation can change external state. The
|
|
84
|
+
host's provider remains responsible for admission on each call.
|
|
85
|
+
|
|
86
|
+
`Knapsack.files({ port })` builds a Files module and direct tools from one port.
|
|
87
|
+
Text edits use `port.replace` with the expected original content; the host must
|
|
88
|
+
replace atomically or refuse a conflict. A native patch operation is available
|
|
89
|
+
only when that port supplies a patch engine. Paths are validated, not confined
|
|
90
|
+
by Knapsack: the captured host port enforces its own root and authority.
|
|
91
|
+
|
|
92
|
+
## API reference
|
|
93
|
+
|
|
94
|
+
The [generated reference](docs/api/README.md) covers every public entry:
|
|
95
|
+
|
|
96
|
+
| Entry | Use |
|
|
97
|
+
| ------------------- | ------------------------------------------------------------------------------------------- |
|
|
98
|
+
| Root | Computer sources, Files connections, edit operations and their codecs. |
|
|
99
|
+
| `/files-patch` | Patch input/output codecs without the tool runtime. |
|
|
100
|
+
| `/files-read` | Bounded text read schemas and shared path validation. |
|
|
101
|
+
| `/patch` | Read Codex patch operations for presentation; the native engine validates and applies them. |
|
|
102
|
+
| `/files-read-codec` | Encode read requests and adapt a framed host transport to the Files read port. |
|
|
103
|
+
|
|
104
|
+
## Examples and checks
|
|
105
|
+
|
|
106
|
+
[Computer tests](test/computer.test.ts) exercise acquisition, invalidation,
|
|
107
|
+
provider failures and invocation context. [Files tests](test/files.test.ts)
|
|
108
|
+
exercise bounded reads and edits.
|
|
109
|
+
[Read codec tests](test/files-read-codec.test.ts) exercise the framed transport.
|
|
110
|
+
The packed-consumer runtime and typecheck fixtures import only the five public
|
|
111
|
+
entries from an isolated tarball installation.
|
|
112
|
+
|
|
113
|
+
From a configured repository checkout:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
pnpm turbo:run run build --filter=@fungi.computer/knapsack
|
|
117
|
+
pnpm --dir packages/knapsack run test
|
|
118
|
+
pnpm --dir packages/knapsack run docs
|
|
119
|
+
pnpm --dir packages/knapsack run stranger
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Limits and errors
|
|
123
|
+
|
|
124
|
+
A Computer advertises at most 256 capabilities. Each advertisement read has a
|
|
125
|
+
10-second default timeout; list acquisition describes at most eight Computers
|
|
126
|
+
concurrently. An unavailable Computer is reported through `onUnavailable` and
|
|
127
|
+
omitted without removing healthy ones. Cancelling acquisition rejects it.
|
|
128
|
+
|
|
129
|
+
Files reads and patch inputs are bounded to 128 KiB of UTF-8 text. Listing and
|
|
130
|
+
search results contain at most 256 entries and expose truncation. Schemas reject
|
|
131
|
+
invalid requests and results. `FilesEditError` distinguishes conflicts, missing
|
|
132
|
+
or ambiguous matches, oversized files and partial-character edits.
|
|
133
|
+
|
|
134
|
+
## Contribute and license
|
|
135
|
+
|
|
136
|
+
Knapsack's package license is [MIT](LICENSE). The retained Codex patch grammar
|
|
137
|
+
in `src/patch-grammar.ts` carries its existing
|
|
138
|
+
[Apache-2.0 license](LICENSE.codex), which is included in the tarball. Keep host
|
|
139
|
+
authority separate from connection projection and run the package and
|
|
140
|
+
packed-consumer checks before proposing a change. Publication is a separate
|
|
141
|
+
release decision.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/** Browser-safe patch contracts shared by native Files tools and Computer HTTP. */
|
|
3
|
+
export declare const patchInputSchema: z.ZodObject<{
|
|
4
|
+
patch: z.ZodString;
|
|
5
|
+
}, z.core.$strict>;
|
|
6
|
+
/** Native patch completion, cancellation or failure, including an optional mismatch. */
|
|
7
|
+
export declare const patchOutputSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
8
|
+
status: z.ZodEnum<{
|
|
9
|
+
completed: "completed";
|
|
10
|
+
failed: "failed";
|
|
11
|
+
cancelled: "cancelled";
|
|
12
|
+
}>;
|
|
13
|
+
exitCode: z.ZodNumber;
|
|
14
|
+
stdout: z.ZodString;
|
|
15
|
+
stderr: z.ZodString;
|
|
16
|
+
truncated: z.ZodBoolean;
|
|
17
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
18
|
+
status: z.ZodEnum<{
|
|
19
|
+
completed: "completed";
|
|
20
|
+
failed: "failed";
|
|
21
|
+
cancelled: "cancelled";
|
|
22
|
+
}>;
|
|
23
|
+
exitCode: z.ZodNumber;
|
|
24
|
+
stdout: z.ZodString;
|
|
25
|
+
stderr: z.ZodString;
|
|
26
|
+
truncated: z.ZodBoolean;
|
|
27
|
+
error: z.ZodObject<{
|
|
28
|
+
kind: z.ZodLiteral<"mismatch">;
|
|
29
|
+
}, z.core.$strict>;
|
|
30
|
+
}, z.core.$strict>]>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { boundedTextSchema, maximumReadBytes } from "./files-read.js";
|
|
3
|
+
/** Browser-safe patch contracts shared by native Files tools and Computer HTTP. */
|
|
4
|
+
export const patchInputSchema = z.strictObject({
|
|
5
|
+
patch: boundedTextSchema(1).meta({
|
|
6
|
+
description: `Codex patch text, at most ${String(maximumReadBytes)} UTF-8 bytes.`,
|
|
7
|
+
}),
|
|
8
|
+
});
|
|
9
|
+
const patchResultSchema = z.strictObject({
|
|
10
|
+
status: z.enum(["completed", "failed", "cancelled"]),
|
|
11
|
+
exitCode: z.number().int(),
|
|
12
|
+
stdout: z.string(),
|
|
13
|
+
stderr: z.string(),
|
|
14
|
+
truncated: z.boolean(),
|
|
15
|
+
});
|
|
16
|
+
/** Native patch completion, cancellation or failure, including an optional mismatch. */
|
|
17
|
+
export const patchOutputSchema = z.union([
|
|
18
|
+
patchResultSchema,
|
|
19
|
+
patchResultSchema.extend({
|
|
20
|
+
error: z.strictObject({ kind: z.literal("mismatch") }),
|
|
21
|
+
}),
|
|
22
|
+
]);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { InvocationContext } from "@fungi.computer/mycelium";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import type { FilesPort } from "./files.js";
|
|
4
|
+
import { readInputSchema } from "./files-read.js";
|
|
5
|
+
/** Host transport carrying a framed Files read with its invocation context. */
|
|
6
|
+
export type FilesReadTransport = (frame: string, context: InvocationContext) => Promise<string>;
|
|
7
|
+
/** Validate and encode a bounded Files read request as a JSON frame. */
|
|
8
|
+
export declare function encodeFilesReadRequest(input: z.input<typeof readInputSchema>): string;
|
|
9
|
+
/** Adapt a framed transport to the Files read port; `Knapsack.files` owns the operation. */
|
|
10
|
+
export declare function createFilesReadAdapter(transport: FilesReadTransport): Pick<FilesPort, "read">;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { readInputSchema, readOutputSchema } from "./files-read.js";
|
|
3
|
+
const readRequestSchema = z.strictObject({
|
|
4
|
+
operation: z.literal("read"),
|
|
5
|
+
input: readInputSchema,
|
|
6
|
+
});
|
|
7
|
+
const readResponseSchema = z.discriminatedUnion("status", [
|
|
8
|
+
z.object({ status: z.literal("ok"), result: readOutputSchema }),
|
|
9
|
+
z.object({ status: z.literal("error"), message: z.string().min(1) }),
|
|
10
|
+
]);
|
|
11
|
+
/** Validate and encode a bounded Files read request as a JSON frame. */
|
|
12
|
+
export function encodeFilesReadRequest(input) {
|
|
13
|
+
return JSON.stringify(readRequestSchema.parse({ operation: "read", input }));
|
|
14
|
+
}
|
|
15
|
+
function decodeFilesReadResponse(input) {
|
|
16
|
+
const parsed = readResponseSchema.parse(JSON.parse(input));
|
|
17
|
+
if (parsed.status === "error")
|
|
18
|
+
throw new Error(parsed.message);
|
|
19
|
+
return parsed.result;
|
|
20
|
+
}
|
|
21
|
+
/** Adapt a framed transport to the Files read port; `Knapsack.files` owns the operation. */
|
|
22
|
+
export function createFilesReadAdapter(transport) {
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
read: async (input, context) => decodeFilesReadResponse(await transport(encodeFilesReadRequest(input), context)),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/** Maximum UTF-8 byte length of a Files text page. */
|
|
3
|
+
export declare const maximumReadBytes: number;
|
|
4
|
+
/** Nonempty file path bounded to 4,096 characters, without null bytes. */
|
|
5
|
+
export declare const pathSchema: z.ZodString;
|
|
6
|
+
/** Validate text against a minimum character length and the Files UTF-8 byte limit. */
|
|
7
|
+
export declare function boundedTextSchema(minimumLength?: number): z.ZodString;
|
|
8
|
+
/** Canonical bounded Files read request and result, shared with display consumers. */
|
|
9
|
+
export declare const readInputSchema: z.ZodObject<{
|
|
10
|
+
path: z.ZodString;
|
|
11
|
+
offset: z.ZodDefault<z.ZodNumber>;
|
|
12
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
13
|
+
}, z.core.$strict>;
|
|
14
|
+
/** Bounded text page, continuation offset and optional hosted file identity. */
|
|
15
|
+
export declare const readOutputSchema: z.ZodObject<{
|
|
16
|
+
path: z.ZodString;
|
|
17
|
+
content: z.ZodString;
|
|
18
|
+
offset: z.ZodNumber;
|
|
19
|
+
nextOffset: z.ZodNullable<z.ZodNumber>;
|
|
20
|
+
truncated: z.ZodBoolean;
|
|
21
|
+
file: z.ZodExactOptional<z.ZodObject<{
|
|
22
|
+
computerId: z.ZodString;
|
|
23
|
+
path: z.ZodString;
|
|
24
|
+
}, z.core.$strict>>;
|
|
25
|
+
}, z.core.$strict>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/** Maximum UTF-8 byte length of a Files text page. */
|
|
3
|
+
export const maximumReadBytes = 128 * 1_024;
|
|
4
|
+
const utf8 = new TextEncoder();
|
|
5
|
+
/** Nonempty file path bounded to 4,096 characters, without null bytes. */
|
|
6
|
+
export const pathSchema = z
|
|
7
|
+
.string()
|
|
8
|
+
.min(1)
|
|
9
|
+
.max(4_096)
|
|
10
|
+
.refine((path) => !path.includes("\0"), "Path cannot contain null bytes");
|
|
11
|
+
/** Validate text against a minimum character length and the Files UTF-8 byte limit. */
|
|
12
|
+
export function boundedTextSchema(minimumLength = 0) {
|
|
13
|
+
return z
|
|
14
|
+
.string()
|
|
15
|
+
.min(minimumLength)
|
|
16
|
+
.refine((content) => utf8.encode(content).byteLength <= maximumReadBytes, `Text exceeds the ${String(maximumReadBytes)}-byte Files bound`);
|
|
17
|
+
}
|
|
18
|
+
/** Canonical bounded Files read request and result, shared with display consumers. */
|
|
19
|
+
export const readInputSchema = z.strictObject({
|
|
20
|
+
path: pathSchema,
|
|
21
|
+
offset: z.number().int().min(0).default(0),
|
|
22
|
+
limit: z
|
|
23
|
+
.number()
|
|
24
|
+
.int()
|
|
25
|
+
.min(4)
|
|
26
|
+
.max(maximumReadBytes)
|
|
27
|
+
.default(maximumReadBytes),
|
|
28
|
+
});
|
|
29
|
+
/** Bounded text page, continuation offset and optional hosted file identity. */
|
|
30
|
+
export const readOutputSchema = z.strictObject({
|
|
31
|
+
path: pathSchema,
|
|
32
|
+
content: boundedTextSchema(),
|
|
33
|
+
offset: z.number().int().min(0),
|
|
34
|
+
nextOffset: z.number().int().min(0).nullable(),
|
|
35
|
+
truncated: z.boolean(),
|
|
36
|
+
/** Captured hosted file identity; a local Files host may have no Computer identity. */
|
|
37
|
+
file: z
|
|
38
|
+
.strictObject({
|
|
39
|
+
computerId: z.string().min(1).max(256),
|
|
40
|
+
path: pathSchema.refine((path) => path.startsWith("/"), "Computer file paths must be absolute"),
|
|
41
|
+
})
|
|
42
|
+
.exactOptional(),
|
|
43
|
+
});
|