pi-codex-tools 0.1.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/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ This project follows the spirit of [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and uses semantic versioning.
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-08-03
10
+
11
+ ### Added
12
+
13
+ - Initial `pi-codex-tools` extension.
14
+ - Codex-compatible raw `apply_patch` grammar tooling and capability-based model activation.
15
+ - Supported models replace Pi's `edit` and `write` tools with `apply_patch` and restore their previous active states when switching models.
16
+
17
+ ### Fixed
18
+
19
+ - Preserve indented patch marker lines as update context instead of interpreting them as control markers.
20
+ - Anchor filesystem mutations to no-follow directory descriptors on Linux/macOS and fail closed on unsupported platforms.
21
+ - Bound target-file reads and use linear hunk matching to prevent unbounded memory and quadratic matching work.
@@ -0,0 +1,45 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ ## Our Standards
8
+
9
+ Examples of behavior that contributes to a positive environment include:
10
+
11
+ - Demonstrating empathy and kindness toward other people
12
+ - Being respectful of differing opinions, viewpoints, and experiences
13
+ - Giving and gracefully accepting constructive feedback
14
+ - Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
15
+ - Focusing on what is best for the community, rather than us as individuals
16
+
17
+ Examples of unacceptable behavior by participants include:
18
+
19
+ - Trolling, insulting or derogatory comments, and personal or political attacks
20
+ - Public or private harassment
21
+ - Publishing others' private information, such as a physical or electronic address, without explicit permission
22
+ - Other conduct which could reasonably be considered inappropriate in a professional setting
23
+
24
+ ## Enforcement Responsibilities
25
+
26
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
27
+
28
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
29
+
30
+ ## Scope
31
+
32
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
33
+
34
+ ## Enforcement
35
+
36
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jose@mocito.dev. All complaints will be reviewed and investigated promptly and fairly.
37
+
38
+ Community leaders are obligated to respect the privacy and security of the reporter of any incident.
39
+
40
+ ## Attribution
41
+
42
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
43
+
44
+ [homepage]: https://www.contributor-covenant.org
45
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
@@ -0,0 +1,35 @@
1
+ # Contributing
2
+
3
+ Thanks for your interest in contributing to `pi-codex-tools`.
4
+
5
+ ## Development setup
6
+
7
+ ```bash
8
+ npm install
9
+ npm run -w packages/pi-codex-tools check
10
+ ```
11
+
12
+ This package is source-distributed: Pi loads the TypeScript extension files directly. There is no build step for runtime use.
13
+
14
+ ## Pull request checklist
15
+
16
+ Before opening a pull request:
17
+
18
+ - Run `npm run -w packages/pi-codex-tools check`.
19
+ - Run `npm test -w packages/pi-codex-tools`.
20
+ - Run `npm audit --omit=dev`.
21
+ - Run `npm run -w packages/pi-codex-tools pack:dry-run` and confirm the package contents are intentional.
22
+ - Update `README.md` if user-visible behavior changes.
23
+ - Update `CHANGELOG.md` for notable changes.
24
+ - Keep examples and paths generic; do not commit API keys, tokens, auth headers, local settings, or provider configuration containing secrets.
25
+
26
+ ## Coding guidelines
27
+
28
+ - Keep model capability checks explicit and conservative; never switch on model names alone.
29
+ - Keep raw grammar input separate from JSON tool arguments.
30
+ - Add regression coverage for parser, path-safety, mutation, or model-switching changes.
31
+ - Preserve OpenAI Codex attribution when changing adapted parser or grammar behavior.
32
+
33
+ ## Code of conduct
34
+
35
+ This project follows the Contributor Covenant Code of Conduct.
package/LICENSE 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/NOTICE ADDED
@@ -0,0 +1,6 @@
1
+ pi-codex-tools includes code and grammar behavior derived from OpenAI Codex.
2
+
3
+ OpenAI Codex
4
+ Copyright 2025 OpenAI
5
+
6
+ This project includes code derived from OpenAI Codex and is distributed under the Apache License, Version 2.0. See LICENSE for the license text.
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # pi-codex-tools
2
+
3
+ Give grammar-capable OpenAI/Codex models the Codex `apply_patch` tool in Pi without changing Pi's normal tools for other models.
4
+
5
+ ## What it adds
6
+
7
+ - **Raw `apply_patch`** — sends Codex's Lark grammar as an OpenAI custom tool, so patches are not JSON-wrapped.
8
+ - **Capability-based activation** — requires `openai-codex-responses` or `openai-responses` plus `model.compat.supportsOpenAIGrammarTools === true`; model names alone are never enough.
9
+ - **Safe local mutation** — patches are limited to 1 MiB, target files to 64 MiB, stay under Pi's current working directory, reject symlink escapes, use descriptor-anchored no-follow operations on Linux/macOS, fail closed elsewhere, preflight all hunks, and serialize writes with Pi's mutation queue.
10
+ - **Model switching** — supported models replace Pi's `edit` and `write` tools with `apply_patch`; other active tools are preserved. Switching back restores only the file tools that were active before the switch.
11
+ - **Sequential patch calls** — the extension marks patch execution sequential and disables provider-side parallel tool calls when the patch tool is active.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pi install npm:pi-codex-tools
17
+ ```
18
+
19
+ For a one-off run:
20
+
21
+ ```bash
22
+ pi -e /path/to/pi-mono/packages/pi-codex-tools
23
+ ```
24
+
25
+ ## Scope decisions
26
+
27
+ The current Codex source does not define separate `read_file` or `write_file` tools: file inspection is normally done through shell commands and file mutation through `apply_patch`. This package keeps Pi's bounded `read` and `bash` tools, and uses `apply_patch` in place of Pi's `edit` and `write` tools for supported models. Because Pi does not provide Codex's OS-level filesystem sandbox, `apply_patch` runs only on Linux/macOS and fails closed on unsupported platforms.
28
+
29
+ | Codex surface | Decision |
30
+ | --- | --- |
31
+ | `apply_patch` | Included; it is a materially different freeform grammar tool. |
32
+ | `shell_command` | Deferred; Pi already has the bounded shell backend, while a faithful adapter needs Codex's approval and working-directory contract. |
33
+ | `exec_command` + `write_stdin` | Deferred; persistent PTY sessions need a separate process/session design. |
34
+ | `view_image` | Deferred; Pi's `read` already sends supported images as attachments. |
35
+ | `update_plan` | Deferred; it is workflow metadata rather than a capability-specific file tool. |
36
+ | Code Mode `exec` + `wait` | Deferred; it requires a real sandbox for model-authored JavaScript, not Node's ordinary `vm` wrapper. |
37
+
38
+ These choices are based on the Codex tool specifications in `codex-rs/core/src/tools`, the model profiles in `codex-rs/models-manager/models.json`, and the Code Mode protocol. They intentionally keep this package focused on the one tool with a distinct transport and model-facing contract.
39
+
40
+ ## Compatibility notes
41
+
42
+ `apply_patch` is line-oriented rather than byte-oriented:
43
+
44
+ - `*** Add File` requires at least one `+` line and writes a trailing newline. A `+`-only hunk creates a one-newline file, not a zero-byte file.
45
+ - Updates produce a trailing newline for non-empty output, so updating a file that lacks one may add it.
46
+ - Existing CRLF line endings are preserved when detected.
47
+ - Use `bash` when exact byte-level output or a truly empty file is required.
48
+
49
+ These behaviors intentionally match Codex `apply_patch`.
50
+
51
+ ## Development
52
+
53
+ ```bash
54
+ npm run -w packages/pi-codex-tools check
55
+ npm test -w packages/pi-codex-tools
56
+ npm run -w packages/pi-codex-tools pack:dry-run
57
+ ```
58
+
59
+ Install/update telemetry can be disabled with `PI_OFFLINE=1` or `PI_TELEMETRY=0`.
60
+
61
+ ## License
62
+
63
+ This package is Apache-2.0 licensed because its grammar/parser behavior is adapted from OpenAI Codex. See [NOTICE](./NOTICE).
package/SECURITY.md ADDED
@@ -0,0 +1,26 @@
1
+ # Security Policy
2
+
3
+ ## Supported versions
4
+
5
+ Security fixes are provided for the latest released version of `pi-codex-tools`.
6
+
7
+ ## Reporting a vulnerability
8
+
9
+ Please do not open a public issue for suspected security vulnerabilities.
10
+
11
+ Report privately through [GitHub Security Advisories](https://github.com/jvm/pi-mono/security/advisories/new) or by contacting the repository maintainer through GitHub. Include:
12
+
13
+ - a description of the issue;
14
+ - steps to reproduce;
15
+ - affected versions or commits, if known;
16
+ - any suggested mitigation.
17
+
18
+ ## Security model
19
+
20
+ Pi extensions execute with the same permissions as the local user running Pi. Review installed extensions and only install packages from sources you trust.
21
+
22
+ `apply_patch` does not access the network or credentials. It validates paths beneath the current working directory, rejects symlink paths and symlinked parents, limits patch input to 1 MiB and target-file reads to 64 MiB, preflights file changes before writing, and uses root-anchored descriptor-based no-follow operations on Linux/macOS. It fails closed on unsupported platforms because Pi does not provide Codex's OS-level filesystem sandbox. A failure during a multi-file write can still leave earlier files changed; callers should use version control and review the resulting diff.
23
+
24
+ The package reads the current provider/model capability flags only to select tools. It does not log prompts, patches, file contents, credentials, auth headers, or provider responses.
25
+
26
+ Install/update telemetry is best effort and can be disabled with `PI_OFFLINE=1`, `PI_TELEMETRY=0`, CI detection, or Pi's `enableInstallTelemetry: false` setting. It sends only package/version and runtime metadata through `@mocito/install-telemetry`.
@@ -0,0 +1,94 @@
1
+ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+ import { reportInstallTelemetry } from "../src/install-telemetry.js";
3
+ import { applyPatch, APPLY_PATCH_GRAMMAR, MAX_PATCH_BYTES } from "../src/apply-patch.js";
4
+ import { createFreeformInputSchema, createOpenAILarkSampling, type OpenAIGrammarSampling } from "../src/grammar.js";
5
+ import { supportsOpenAIGrammarTools } from "../src/model-support.js";
6
+
7
+ const APPLY_PATCH = "apply_patch";
8
+ const EDIT = "edit";
9
+ const WRITE = "write";
10
+ const REPLACED_TOOLS = [EDIT, WRITE] as const;
11
+ type ReplacedTool = (typeof REPLACED_TOOLS)[number];
12
+
13
+ const APPLY_PATCH_PARAMETERS = createFreeformInputSchema(
14
+ "patch",
15
+ "Raw Codex apply_patch text. Do not wrap it in JSON.",
16
+ );
17
+
18
+ export default function piCodexTools(pi: ExtensionAPI): void {
19
+ reportInstallTelemetry();
20
+
21
+ const registerGrammarTool = pi.registerTool as (tool: Parameters<ExtensionAPI["registerTool"]>[0] & {
22
+ constrainedSampling?: OpenAIGrammarSampling;
23
+ }) => void;
24
+
25
+ registerGrammarTool({
26
+ name: APPLY_PATCH,
27
+ label: APPLY_PATCH,
28
+ description: "Apply a Codex patch to files. This is a FREEFORM tool: send the patch text directly, never as JSON.",
29
+ promptSnippet: "Apply Codex-format file patches without JSON wrapping",
30
+ promptGuidelines: [
31
+ "Use apply_patch for file changes when it is available.",
32
+ "Send the patch body directly; do not wrap it in JSON or add a shell heredoc.",
33
+ `Patch paths must stay inside the current working directory and patches are limited to ${MAX_PATCH_BYTES} bytes.`,
34
+ ],
35
+ parameters: APPLY_PATCH_PARAMETERS,
36
+ constrainedSampling: createOpenAILarkSampling(APPLY_PATCH_GRAMMAR),
37
+ executionMode: "sequential",
38
+ async execute(_toolCallId, rawParams, signal, _onUpdate, ctx) {
39
+ const patch = (rawParams as { patch?: unknown }).patch;
40
+ if (typeof patch !== "string") throw new Error("apply_patch requires raw patch text.");
41
+ const result = await applyPatch(patch, { cwd: ctx.cwd, signal });
42
+ const summary = result.changes
43
+ .map((change) => `${change.kind[0].toUpperCase()}${change.kind.slice(1)} ${change.path}${change.moveTo ? ` -> ${change.moveTo}` : ""}`)
44
+ .join("\n");
45
+ return { content: [{ type: "text", text: `Applied patch:\n${summary}` }], details: result };
46
+ },
47
+ });
48
+
49
+ let replacedToolsWasActive: Record<ReplacedTool, boolean> | undefined;
50
+
51
+ function synchronizeTools(ctx: ExtensionContext): void {
52
+ if (typeof pi.getActiveTools !== "function" || typeof pi.setActiveTools !== "function") return;
53
+
54
+ const active = new Set(pi.getActiveTools());
55
+ if (supportsOpenAIGrammarTools(ctx.model)) {
56
+ if (replacedToolsWasActive === undefined) {
57
+ replacedToolsWasActive = {
58
+ edit: active.has(EDIT),
59
+ write: active.has(WRITE),
60
+ };
61
+ }
62
+ for (const tool of REPLACED_TOOLS) active.delete(tool);
63
+ active.add(APPLY_PATCH);
64
+ } else {
65
+ active.delete(APPLY_PATCH);
66
+ if (replacedToolsWasActive) {
67
+ for (const tool of REPLACED_TOOLS) {
68
+ if (replacedToolsWasActive[tool]) active.add(tool);
69
+ }
70
+ }
71
+ replacedToolsWasActive = undefined;
72
+ }
73
+ pi.setActiveTools([...active]);
74
+ }
75
+
76
+ pi.on("session_start", (_event, ctx) => {
77
+ synchronizeTools(ctx);
78
+ });
79
+
80
+ pi.on("model_select", (_event, ctx) => {
81
+ synchronizeTools(ctx);
82
+ });
83
+
84
+ pi.on("before_provider_request", (event, ctx) => {
85
+ if (!supportsOpenAIGrammarTools(ctx.model)) return;
86
+ if (typeof pi.getActiveTools !== "function" || !pi.getActiveTools().includes(APPLY_PATCH)) return;
87
+ if (!isRecord(event.payload) || event.payload.parallel_tool_calls !== true) return;
88
+ return { ...event.payload, parallel_tool_calls: false };
89
+ });
90
+ }
91
+
92
+ function isRecord(value: unknown): value is Record<string, unknown> {
93
+ return typeof value === "object" && value !== null && !Array.isArray(value);
94
+ }
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { default } from "./extensions/index.js";
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "pi-codex-tools",
3
+ "version": "0.1.0",
4
+ "description": "Codex-compatible apply_patch tooling for Pi's grammar-capable OpenAI models.",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "author": "Jose Mocito",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/jvm/pi-mono.git",
11
+ "directory": "packages/pi-codex-tools"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/jvm/pi-mono/issues"
15
+ },
16
+ "homepage": "https://github.com/jvm/pi-mono/tree/main/packages/pi-codex-tools#readme",
17
+ "keywords": [
18
+ "pi-package",
19
+ "pi-extension",
20
+ "pi",
21
+ "codex",
22
+ "openai",
23
+ "gpt-5",
24
+ "apply-patch"
25
+ ],
26
+ "exports": {
27
+ ".": "./src/index.ts"
28
+ },
29
+ "pi": {
30
+ "extensions": [
31
+ "./index.ts"
32
+ ]
33
+ },
34
+ "files": [
35
+ "index.ts",
36
+ "extensions",
37
+ "src",
38
+ "README.md",
39
+ "NOTICE",
40
+ "LICENSE",
41
+ "CHANGELOG.md",
42
+ "SECURITY.md",
43
+ "CONTRIBUTING.md",
44
+ "CODE_OF_CONDUCT.md"
45
+ ],
46
+ "scripts": {
47
+ "check": "tsc --noEmit",
48
+ "typecheck": "tsc --noEmit",
49
+ "test": "node --import tsx --test tests/*.test.mjs",
50
+ "pack:dry-run": "npm pack --dry-run"
51
+ },
52
+ "peerDependencies": {
53
+ "@earendil-works/pi-ai": "*",
54
+ "@earendil-works/pi-coding-agent": "*",
55
+ "typebox": "*"
56
+ },
57
+ "devDependencies": {
58
+ "@earendil-works/pi-ai": "^0.80.10",
59
+ "@earendil-works/pi-coding-agent": "^0.80.10",
60
+ "@types/node": "^26.1.1",
61
+ "tsx": "^4.23.1",
62
+ "typebox": "^1.3.6",
63
+ "typescript": "^7.0.2"
64
+ },
65
+ "publishConfig": {
66
+ "access": "public"
67
+ },
68
+ "engines": {
69
+ "node": ">=20.6.0"
70
+ },
71
+ "dependencies": {
72
+ "@mocito/install-telemetry": "0.1.1"
73
+ }
74
+ }