@namzu/sdk 70.0.0 → 71.0.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 +117 -0
- package/dist/authorization/bare-repository.d.ts +21 -0
- package/dist/authorization/bare-repository.d.ts.map +1 -0
- package/dist/authorization/bare-repository.js +62 -0
- package/dist/authorization/bare-repository.js.map +1 -0
- package/dist/authorization/command-line.d.ts.map +1 -1
- package/dist/authorization/command-line.js +10 -3
- package/dist/authorization/command-line.js.map +1 -1
- package/dist/authorization/gate.d.ts +7 -0
- package/dist/authorization/gate.d.ts.map +1 -1
- package/dist/authorization/gate.js +4 -0
- package/dist/authorization/gate.js.map +1 -1
- package/dist/authorization/protected-paths.d.ts.map +1 -1
- package/dist/authorization/protected-paths.js +46 -1
- package/dist/authorization/protected-paths.js.map +1 -1
- package/dist/authorization/readonly-commands.d.ts +17 -5
- package/dist/authorization/readonly-commands.d.ts.map +1 -1
- package/dist/authorization/readonly-commands.js +77 -6
- package/dist/authorization/readonly-commands.js.map +1 -1
- package/dist/authorization/readonly-flags.d.ts.map +1 -1
- package/dist/authorization/readonly-flags.js +98 -0
- package/dist/authorization/readonly-flags.js.map +1 -1
- package/dist/authorization/shell-lexer.d.ts +15 -0
- package/dist/authorization/shell-lexer.d.ts.map +1 -1
- package/dist/authorization/shell-lexer.js +108 -3
- package/dist/authorization/shell-lexer.js.map +1 -1
- package/dist/authorization/shell-paths.d.ts.map +1 -1
- package/dist/authorization/shell-paths.js +276 -0
- package/dist/authorization/shell-paths.js.map +1 -1
- package/dist/authorization/shell-session-state.d.ts +47 -0
- package/dist/authorization/shell-session-state.d.ts.map +1 -0
- package/dist/authorization/shell-session-state.js +70 -0
- package/dist/authorization/shell-session-state.js.map +1 -0
- package/dist/authorization/shell-session.d.ts +9 -0
- package/dist/authorization/shell-session.d.ts.map +1 -0
- package/dist/authorization/shell-session.js +19 -0
- package/dist/authorization/shell-session.js.map +1 -0
- package/dist/authorization/skill-grant.d.ts.map +1 -1
- package/dist/authorization/skill-grant.js +1 -0
- package/dist/authorization/skill-grant.js.map +1 -1
- package/dist/compaction/extractor.d.ts.map +1 -1
- package/dist/compaction/extractor.js +2 -0
- package/dist/compaction/extractor.js.map +1 -1
- package/dist/compaction/hooks.d.ts +16 -0
- package/dist/compaction/hooks.d.ts.map +1 -1
- package/dist/compaction/hooks.js +26 -1
- package/dist/compaction/hooks.js.map +1 -1
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +6 -1
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/restore.d.ts.map +1 -1
- package/dist/compaction/restore.js +3 -2
- package/dist/compaction/restore.js.map +1 -1
- package/dist/config/runtime.d.ts +2 -0
- package/dist/config/runtime.d.ts.map +1 -1
- package/dist/config/runtime.js +7 -0
- package/dist/config/runtime.js.map +1 -1
- package/dist/connector/mcp/client.d.ts +7 -0
- package/dist/connector/mcp/client.d.ts.map +1 -1
- package/dist/connector/mcp/client.js +54 -4
- package/dist/connector/mcp/client.js.map +1 -1
- package/dist/connector/web/types.d.ts +8 -0
- package/dist/connector/web/types.d.ts.map +1 -1
- package/dist/constants/compaction/index.d.ts.map +1 -1
- package/dist/constants/compaction/index.js +1 -1
- package/dist/constants/compaction/index.js.map +1 -1
- package/dist/execution/code-runtime/worker-program.d.ts +1 -1
- package/dist/execution/code-runtime/worker-program.d.ts.map +1 -1
- package/dist/execution/code-runtime/worker-program.js +9 -5
- package/dist/execution/code-runtime/worker-program.js.map +1 -1
- package/dist/execution/local.d.ts.map +1 -1
- package/dist/execution/local.js +6 -1
- package/dist/execution/local.js.map +1 -1
- package/dist/peers/index.d.ts +2 -2
- package/dist/peers/index.d.ts.map +1 -1
- package/dist/peers/index.js +1 -1
- package/dist/peers/index.js.map +1 -1
- package/dist/peers/record.d.ts +74 -0
- package/dist/peers/record.d.ts.map +1 -1
- package/dist/peers/record.js +41 -1
- package/dist/peers/record.js.map +1 -1
- package/dist/peers/registry.d.ts.map +1 -1
- package/dist/peers/registry.js +20 -3
- package/dist/peers/registry.js.map +1 -1
- package/dist/plugin/bin-path.d.ts +41 -0
- package/dist/plugin/bin-path.d.ts.map +1 -0
- package/dist/plugin/bin-path.js +146 -0
- package/dist/plugin/bin-path.js.map +1 -0
- package/dist/plugin/common-layout/read.js +1 -1
- package/dist/plugin/common-layout/read.js.map +1 -1
- package/dist/plugin/common-layout-names.d.ts.map +1 -1
- package/dist/plugin/common-layout-names.js +0 -1
- package/dist/plugin/common-layout-names.js.map +1 -1
- package/dist/plugin/hook-module/scan.d.ts +16 -0
- package/dist/plugin/hook-module/scan.d.ts.map +1 -1
- package/dist/plugin/hook-module/scan.js +67 -2
- package/dist/plugin/hook-module/scan.js.map +1 -1
- package/dist/plugin/hook-module/worker-source.d.ts.map +1 -1
- package/dist/plugin/hook-module/worker-source.js +56 -0
- package/dist/plugin/hook-module/worker-source.js.map +1 -1
- package/dist/plugin/lifecycle.d.ts +21 -0
- package/dist/plugin/lifecycle.d.ts.map +1 -1
- package/dist/plugin/lifecycle.js +64 -0
- package/dist/plugin/lifecycle.js.map +1 -1
- package/dist/plugin/shell-hook-tool-names.d.ts.map +1 -1
- package/dist/plugin/shell-hook-tool-names.js +1 -0
- package/dist/plugin/shell-hook-tool-names.js.map +1 -1
- package/dist/plugin/shell-hook.d.ts +1 -1
- package/dist/plugin/shell-hook.d.ts.map +1 -1
- package/dist/plugin/shell-hook.js +22 -4
- package/dist/plugin/shell-hook.js.map +1 -1
- package/dist/public-runtime.d.ts +8 -6
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +8 -5
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +6 -2
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +6 -2
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +2 -2
- package/dist/public-types.d.ts.map +1 -1
- package/dist/runtime/jobs/monitor-stream.d.ts +9 -1
- package/dist/runtime/jobs/monitor-stream.d.ts.map +1 -1
- package/dist/runtime/jobs/monitor-stream.js +13 -4
- package/dist/runtime/jobs/monitor-stream.js.map +1 -1
- package/dist/runtime/jobs/registry.d.ts +4 -0
- package/dist/runtime/jobs/registry.d.ts.map +1 -1
- package/dist/runtime/jobs/registry.js +12 -2
- package/dist/runtime/jobs/registry.js.map +1 -1
- package/dist/runtime/query/auto-review/git-status.d.ts.map +1 -1
- package/dist/runtime/query/auto-review/git-status.js +3 -2
- package/dist/runtime/query/auto-review/git-status.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +16 -6
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +60 -5
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +2 -2
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/outstanding-work.d.ts.map +1 -1
- package/dist/runtime/query/iteration/outstanding-work.js +14 -4
- package/dist/runtime/query/iteration/outstanding-work.js.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/compaction.js +11 -1
- package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts +2 -0
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +19 -8
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/path-escalation.d.ts +6 -0
- package/dist/runtime/query/path-escalation.d.ts.map +1 -1
- package/dist/runtime/query/path-escalation.js +58 -4
- package/dist/runtime/query/path-escalation.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +4 -2
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/steering.d.ts +11 -2
- package/dist/runtime/query/steering.d.ts.map +1 -1
- package/dist/runtime/query/steering.js +18 -4
- package/dist/runtime/query/steering.js.map +1 -1
- package/dist/sandbox/provider/local.d.ts.map +1 -1
- package/dist/sandbox/provider/local.js +3 -1
- package/dist/sandbox/provider/local.js.map +1 -1
- package/dist/schedules/describe.d.ts.map +1 -1
- package/dist/schedules/describe.js +2 -0
- package/dist/schedules/describe.js.map +1 -1
- package/dist/schedules/evaluate.d.ts +4 -0
- package/dist/schedules/evaluate.d.ts.map +1 -1
- package/dist/schedules/evaluate.js +26 -1
- package/dist/schedules/evaluate.js.map +1 -1
- package/dist/schedules/index.d.ts +3 -1
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +2 -1
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/next-fire.d.ts.map +1 -1
- package/dist/schedules/next-fire.js +10 -0
- package/dist/schedules/next-fire.js.map +1 -1
- package/dist/schedules/spec.d.ts +9 -0
- package/dist/schedules/spec.d.ts.map +1 -1
- package/dist/schedules/spec.js +33 -0
- package/dist/schedules/spec.js.map +1 -1
- package/dist/schedules/types.d.ts +32 -4
- package/dist/schedules/types.d.ts.map +1 -1
- package/dist/schedules/types.js +3 -2
- package/dist/schedules/types.js.map +1 -1
- package/dist/schedules/watch.d.ts +23 -0
- package/dist/schedules/watch.d.ts.map +1 -0
- package/dist/schedules/watch.js +46 -0
- package/dist/schedules/watch.js.map +1 -0
- package/dist/session/workspace/git-worktree.d.ts.map +1 -1
- package/dist/session/workspace/git-worktree.js +2 -1
- package/dist/session/workspace/git-worktree.js.map +1 -1
- package/dist/skills/arguments.d.ts +58 -0
- package/dist/skills/arguments.d.ts.map +1 -0
- package/dist/skills/arguments.js +208 -0
- package/dist/skills/arguments.js.map +1 -0
- package/dist/skills/index.d.ts +3 -1
- package/dist/skills/index.d.ts.map +1 -1
- package/dist/skills/index.js +1 -0
- package/dist/skills/index.js.map +1 -1
- package/dist/skills/inline-shell.d.ts +17 -0
- package/dist/skills/inline-shell.d.ts.map +1 -1
- package/dist/skills/inline-shell.js +1232 -14
- package/dist/skills/inline-shell.js.map +1 -1
- package/dist/tools/builtins/bash-cwd.d.ts.map +1 -1
- package/dist/tools/builtins/bash-cwd.js +2 -0
- package/dist/tools/builtins/bash-cwd.js.map +1 -1
- package/dist/tools/builtins/bash.d.ts.map +1 -1
- package/dist/tools/builtins/bash.js +79 -11
- package/dist/tools/builtins/bash.js.map +1 -1
- package/dist/tools/builtins/exit-semantics.d.ts +6 -0
- package/dist/tools/builtins/exit-semantics.d.ts.map +1 -1
- package/dist/tools/builtins/exit-semantics.js +10 -0
- package/dist/tools/builtins/exit-semantics.js.map +1 -1
- package/dist/tools/builtins/glob.d.ts.map +1 -1
- package/dist/tools/builtins/glob.js +68 -13
- package/dist/tools/builtins/glob.js.map +1 -1
- package/dist/tools/builtins/grep.d.ts.map +1 -1
- package/dist/tools/builtins/grep.js +37 -12
- package/dist/tools/builtins/grep.js.map +1 -1
- package/dist/tools/builtins/index.d.ts +11 -0
- package/dist/tools/builtins/index.d.ts.map +1 -1
- package/dist/tools/builtins/index.js +14 -0
- package/dist/tools/builtins/index.js.map +1 -1
- package/dist/tools/builtins/job.js +1 -1
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/monitor.d.ts +1 -1
- package/dist/tools/builtins/monitor.d.ts.map +1 -1
- package/dist/tools/builtins/monitor.js +4 -2
- package/dist/tools/builtins/monitor.js.map +1 -1
- package/dist/tools/builtins/notebook-cells.d.ts +31 -0
- package/dist/tools/builtins/notebook-cells.d.ts.map +1 -0
- package/dist/tools/builtins/notebook-cells.js +160 -0
- package/dist/tools/builtins/notebook-cells.js.map +1 -0
- package/dist/tools/builtins/notebook-edit.d.ts +8 -0
- package/dist/tools/builtins/notebook-edit.d.ts.map +1 -0
- package/dist/tools/builtins/notebook-edit.js +249 -0
- package/dist/tools/builtins/notebook-edit.js.map +1 -0
- package/dist/tools/builtins/notebook-guard.d.ts.map +1 -1
- package/dist/tools/builtins/notebook-guard.js +2 -1
- package/dist/tools/builtins/notebook-guard.js.map +1 -1
- package/dist/tools/builtins/push-notification.d.ts +49 -0
- package/dist/tools/builtins/push-notification.d.ts.map +1 -0
- package/dist/tools/builtins/push-notification.js +107 -0
- package/dist/tools/builtins/push-notification.js.map +1 -0
- package/dist/tools/builtins/read-file.d.ts.map +1 -1
- package/dist/tools/builtins/read-file.js +8 -3
- package/dist/tools/builtins/read-file.js.map +1 -1
- package/dist/tools/builtins/read-media.d.ts.map +1 -1
- package/dist/tools/builtins/read-media.js +2 -1
- package/dist/tools/builtins/read-media.js.map +1 -1
- package/dist/tools/builtins/read-render.d.ts.map +1 -1
- package/dist/tools/builtins/read-render.js +5 -3
- package/dist/tools/builtins/read-render.js.map +1 -1
- package/dist/tools/builtins/search-support.d.ts +6 -4
- package/dist/tools/builtins/search-support.d.ts.map +1 -1
- package/dist/tools/builtins/search-support.js +15 -3
- package/dist/tools/builtins/search-support.js.map +1 -1
- package/dist/tools/builtins/skill.d.ts.map +1 -1
- package/dist/tools/builtins/skill.js +28 -8
- package/dist/tools/builtins/skill.js.map +1 -1
- package/dist/tools/builtins/sleep-guard.d.ts +20 -0
- package/dist/tools/builtins/sleep-guard.d.ts.map +1 -1
- package/dist/tools/builtins/sleep-guard.js +125 -1
- package/dist/tools/builtins/sleep-guard.js.map +1 -1
- package/dist/tools/builtins/wait-for-job.js +1 -1
- package/dist/tools/builtins/wait-for-job.js.map +1 -1
- package/dist/tools/builtins/web.d.ts +2 -0
- package/dist/tools/builtins/web.d.ts.map +1 -1
- package/dist/tools/builtins/web.js +97 -3
- package/dist/tools/builtins/web.js.map +1 -1
- package/dist/tools/command-shell.d.ts +50 -0
- package/dist/tools/command-shell.d.ts.map +1 -1
- package/dist/tools/command-shell.js +145 -0
- package/dist/tools/command-shell.js.map +1 -1
- package/dist/tools/env-scrub.d.ts +2 -0
- package/dist/tools/env-scrub.d.ts.map +1 -1
- package/dist/tools/env-scrub.js +15 -1
- package/dist/tools/env-scrub.js.map +1 -1
- package/dist/tools/git-config-env.d.ts +55 -0
- package/dist/tools/git-config-env.d.ts.map +1 -0
- package/dist/tools/git-config-env.js +130 -0
- package/dist/tools/git-config-env.js.map +1 -0
- package/dist/tools/schedules/schedule-tool.d.ts +7 -0
- package/dist/tools/schedules/schedule-tool.d.ts.map +1 -1
- package/dist/tools/schedules/schedule-tool.js +72 -3
- package/dist/tools/schedules/schedule-tool.js.map +1 -1
- package/dist/tools/schedules/types.d.ts +38 -2
- package/dist/tools/schedules/types.d.ts.map +1 -1
- package/dist/types/connector/mcp.d.ts +24 -0
- package/dist/types/connector/mcp.d.ts.map +1 -1
- package/dist/types/message/index.d.ts +21 -1
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js +37 -2
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/plugin/index.d.ts +10 -2
- package/dist/types/plugin/index.d.ts.map +1 -1
- package/dist/types/plugin/index.js.map +1 -1
- package/dist/types/tool/index.d.ts +6 -0
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/utils/plain-terminal-text.d.ts +21 -0
- package/dist/utils/plain-terminal-text.d.ts.map +1 -0
- package/dist/utils/plain-terminal-text.js +156 -0
- package/dist/utils/plain-terminal-text.js.map +1 -0
- package/package.json +1 -1
- package/src/authorization/bare-repository.ts +70 -0
- package/src/authorization/command-line.ts +18 -4
- package/src/authorization/gate.ts +11 -0
- package/src/authorization/protected-paths.ts +45 -1
- package/src/authorization/readonly-commands.ts +75 -6
- package/src/authorization/readonly-flags.ts +103 -0
- package/src/authorization/shell-lexer.ts +126 -3
- package/src/authorization/shell-paths.ts +386 -0
- package/src/authorization/shell-session-state.ts +97 -0
- package/src/authorization/shell-session.ts +23 -0
- package/src/authorization/skill-grant.ts +1 -0
- package/src/compaction/extractor.ts +1 -0
- package/src/compaction/hooks.ts +43 -1
- package/src/compaction/manual.ts +6 -0
- package/src/compaction/restore.ts +3 -2
- package/src/config/runtime.ts +7 -0
- package/src/connector/mcp/client.ts +61 -4
- package/src/connector/web/types.ts +8 -0
- package/src/constants/compaction/index.ts +1 -1
- package/src/execution/code-runtime/worker-program.ts +9 -5
- package/src/execution/local.ts +6 -1
- package/src/peers/index.ts +8 -2
- package/src/peers/record.ts +45 -1
- package/src/peers/registry.ts +21 -5
- package/src/plugin/bin-path.ts +149 -0
- package/src/plugin/common-layout/read.ts +1 -1
- package/src/plugin/common-layout-names.ts +0 -1
- package/src/plugin/hook-module/scan.ts +74 -2
- package/src/plugin/hook-module/worker-source.ts +56 -0
- package/src/plugin/lifecycle.ts +67 -0
- package/src/plugin/shell-hook-tool-names.ts +1 -0
- package/src/plugin/shell-hook.ts +23 -4
- package/src/public-runtime.ts +21 -0
- package/src/public-tools.ts +32 -1
- package/src/public-types.ts +3 -1
- package/src/runtime/jobs/monitor-stream.ts +18 -4
- package/src/runtime/jobs/registry.ts +22 -2
- package/src/runtime/query/auto-review/git-status.ts +3 -2
- package/src/runtime/query/executor.ts +79 -5
- package/src/runtime/query/index.ts +5 -2
- package/src/runtime/query/iteration/outstanding-work.ts +22 -4
- package/src/runtime/query/iteration/phases/compaction.ts +14 -1
- package/src/runtime/query/iteration/phases/tool-review.ts +24 -7
- package/src/runtime/query/path-escalation.ts +52 -4
- package/src/runtime/query/review-policy.ts +5 -2
- package/src/runtime/query/steering.ts +32 -4
- package/src/sandbox/provider/local.ts +3 -1
- package/src/schedules/describe.ts +2 -0
- package/src/schedules/evaluate.ts +29 -1
- package/src/schedules/index.ts +9 -1
- package/src/schedules/next-fire.ts +9 -0
- package/src/schedules/spec.ts +42 -0
- package/src/schedules/types.ts +33 -3
- package/src/schedules/watch.ts +71 -0
- package/src/session/workspace/git-worktree.ts +3 -1
- package/src/skills/arguments.ts +250 -0
- package/src/skills/index.ts +10 -0
- package/src/skills/inline-shell.ts +1268 -14
- package/src/tools/builtins/bash-cwd.ts +2 -0
- package/src/tools/builtins/bash.ts +93 -13
- package/src/tools/builtins/exit-semantics.ts +10 -0
- package/src/tools/builtins/glob.ts +79 -15
- package/src/tools/builtins/grep.ts +37 -10
- package/src/tools/builtins/index.ts +15 -0
- package/src/tools/builtins/job.ts +1 -1
- package/src/tools/builtins/monitor.ts +4 -2
- package/src/tools/builtins/notebook-cells.ts +196 -0
- package/src/tools/builtins/notebook-edit.ts +280 -0
- package/src/tools/builtins/notebook-guard.ts +2 -1
- package/src/tools/builtins/push-notification.ts +138 -0
- package/src/tools/builtins/read-file.ts +9 -3
- package/src/tools/builtins/read-media.ts +2 -1
- package/src/tools/builtins/read-render.ts +5 -3
- package/src/tools/builtins/search-support.ts +17 -3
- package/src/tools/builtins/skill.ts +27 -7
- package/src/tools/builtins/sleep-guard.ts +128 -1
- package/src/tools/builtins/wait-for-job.ts +1 -1
- package/src/tools/builtins/web.ts +100 -3
- package/src/tools/command-shell.ts +166 -0
- package/src/tools/env-scrub.ts +13 -1
- package/src/tools/git-config-env.ts +143 -0
- package/src/tools/schedules/schedule-tool.ts +79 -3
- package/src/tools/schedules/types.ts +47 -9
- package/src/types/connector/mcp.ts +25 -0
- package/src/types/message/index.ts +53 -1
- package/src/types/plugin/index.ts +10 -2
- package/src/types/tool/index.ts +6 -0
- package/src/utils/plain-terminal-text.ts +142 -0
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cell-level changes to the text of a notebook, kept apart from the tool so
|
|
5
|
+
* they are plain input to output: a body in, a body out, or a plain refusal.
|
|
6
|
+
*
|
|
7
|
+
* What a change leaves alone is the point. Every key it does not name keeps its
|
|
8
|
+
* place and value, the notebook's indentation, line endings, byte-order mark
|
|
9
|
+
* and final newline come back as they were, and the ids of the other cells do
|
|
10
|
+
* not change. A cell is touched only where the change says so.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type NotebookEditMode = 'replace' | 'insert' | 'delete'
|
|
14
|
+
export type NotebookCellType = 'code' | 'markdown'
|
|
15
|
+
|
|
16
|
+
export interface NotebookCellEdit {
|
|
17
|
+
readonly cellId?: string | undefined
|
|
18
|
+
readonly newSource?: string | undefined
|
|
19
|
+
readonly cellType?: NotebookCellType | undefined
|
|
20
|
+
readonly editMode: NotebookEditMode
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type NotebookEditOutcome =
|
|
24
|
+
| {
|
|
25
|
+
readonly ok: true
|
|
26
|
+
readonly content: string
|
|
27
|
+
/** The id the cell has, when it has one: a new cell's fresh id, or the id that was named. */
|
|
28
|
+
readonly cellId: string | undefined
|
|
29
|
+
readonly cellType: string
|
|
30
|
+
readonly oldSource: string | undefined
|
|
31
|
+
readonly editMode: NotebookEditMode
|
|
32
|
+
}
|
|
33
|
+
| { readonly ok: false; readonly error: string }
|
|
34
|
+
|
|
35
|
+
type Json = Record<string, unknown>
|
|
36
|
+
|
|
37
|
+
function isRecord(value: unknown): value is Json {
|
|
38
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function sourceText(source: unknown): string {
|
|
42
|
+
if (typeof source === 'string') return source
|
|
43
|
+
if (Array.isArray(source)) return source.filter((s) => typeof s === 'string').join('')
|
|
44
|
+
return ''
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The lines a notebook stores for a body: each keeps its newline, and an empty body is no lines. */
|
|
48
|
+
function sourceLines(text: string): string[] {
|
|
49
|
+
if (text === '') return []
|
|
50
|
+
return text.split(/(?<=\n)/u)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function indentOf(body: string): string {
|
|
54
|
+
return /^\{\r?\n([ \t]+)"/u.exec(body)?.[1] ?? ' '
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A fresh cell id: eight lower-case hex digits, never one already in the notebook. */
|
|
58
|
+
function freshId(taken: ReadonlySet<string>): string {
|
|
59
|
+
for (;;) {
|
|
60
|
+
const id = randomBytes(4).toString('hex')
|
|
61
|
+
if (!taken.has(id)) return id
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Where a cell is, by its id or by the `cell-N` name a read gives a cell that has no id. */
|
|
66
|
+
function locate(cells: readonly Json[], cellId: string): number | string {
|
|
67
|
+
const byId = cells.findIndex((cell) => cell.id === cellId)
|
|
68
|
+
if (byId >= 0) return byId
|
|
69
|
+
const named = /^cell-(\d+)$/u.exec(cellId)?.[1]
|
|
70
|
+
if (named === undefined) return `Cell with ID "${cellId}" not found in notebook.`
|
|
71
|
+
const index = Number.parseInt(named, 10)
|
|
72
|
+
if (cells[index] === undefined) return `Cell with index ${index} does not exist in notebook.`
|
|
73
|
+
return index
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const HAS_ATTACHMENTS_TO_CODE =
|
|
77
|
+
'The notebook was not changed: this cell has attachments (such as pasted images), and a code cell cannot hold attachments. To add code after this cell, use edit_mode "insert" with cell_type "code" and the same cell_id. To edit this cell\'s text, leave out cell_type and keep any `attachment:` links in new_source. Deleting this cell loses the attachments, so do that only if the user agrees.'
|
|
78
|
+
|
|
79
|
+
export function applyNotebookEdit(body: string, edit: NotebookCellEdit): NotebookEditOutcome {
|
|
80
|
+
const bom = body.startsWith('\uFEFF') ? '\uFEFF' : ''
|
|
81
|
+
const text = bom ? body.slice(1) : body
|
|
82
|
+
let parsed: unknown
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(text)
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
error: `The notebook is not valid JSON (${error instanceof Error ? error.message : String(error)}). It was not changed.`,
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
// A rewrite goes through JSON.parse and JSON.stringify. Two things it cannot
|
|
92
|
+
// give back as they were: an indent over ten characters (cut to ten, so every
|
|
93
|
+
// line changes) and an integer beyond 2^53 (rounded, so a value changes).
|
|
94
|
+
// Refuse and leave the file alone rather than damage cells the call did not name.
|
|
95
|
+
if ((indentOf(text) ?? '').length > 10 || /[:[,]\s*-?\d{16,}(?![\d.eE])/u.test(text)) {
|
|
96
|
+
return {
|
|
97
|
+
ok: false,
|
|
98
|
+
error:
|
|
99
|
+
'The notebook was not changed: it uses an indent wider than ten characters or holds an integer too large to rewrite exactly, so saving it would alter parts the edit does not name. Use `edit` for this file.',
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (!isRecord(parsed)) {
|
|
103
|
+
return { ok: false, error: 'The notebook is not a valid Jupyter notebook. It was not changed.' }
|
|
104
|
+
}
|
|
105
|
+
const nbformat = parsed.nbformat
|
|
106
|
+
if (typeof nbformat !== 'number' || nbformat < 4) {
|
|
107
|
+
return {
|
|
108
|
+
ok: false,
|
|
109
|
+
error:
|
|
110
|
+
'Only notebooks in format 4 or later have a top-level "cells" list that this tool changes. It was not changed.',
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (!Array.isArray(parsed.cells) || !parsed.cells.every(isRecord)) {
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
error:
|
|
117
|
+
'The notebook is not a valid Jupyter notebook (top-level "cells" must be a list of cell objects). It was not changed.',
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const cells = parsed.cells as Json[]
|
|
121
|
+
const { editMode } = edit
|
|
122
|
+
|
|
123
|
+
if (editMode !== 'insert' && edit.cellId === undefined) {
|
|
124
|
+
return { ok: false, error: 'Cell ID must be specified when not inserting a new cell.' }
|
|
125
|
+
}
|
|
126
|
+
if (editMode === 'insert' && edit.cellType === undefined) {
|
|
127
|
+
return { ok: false, error: 'Cell type is required when using edit_mode "insert".' }
|
|
128
|
+
}
|
|
129
|
+
if (editMode !== 'delete' && edit.newSource === undefined) {
|
|
130
|
+
return { ok: false, error: `new_source is required when edit_mode is "${editMode}".` }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let at = 0
|
|
134
|
+
if (edit.cellId !== undefined) {
|
|
135
|
+
const found = locate(cells, edit.cellId)
|
|
136
|
+
if (typeof found === 'string') return { ok: false, error: found }
|
|
137
|
+
at = found
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const minor = typeof parsed.nbformat_minor === 'number' ? parsed.nbformat_minor : 0
|
|
141
|
+
const givesIds = nbformat > 4 || minor >= 5
|
|
142
|
+
let cellId: string | undefined = edit.cellId
|
|
143
|
+
let cellType: string
|
|
144
|
+
let oldSource: string | undefined
|
|
145
|
+
|
|
146
|
+
if (editMode === 'delete') {
|
|
147
|
+
const cell = cells[at] as Json
|
|
148
|
+
cellType = typeof cell.cell_type === 'string' ? cell.cell_type : 'code'
|
|
149
|
+
oldSource = sourceText(cell.source)
|
|
150
|
+
cells.splice(at, 1)
|
|
151
|
+
} else if (editMode === 'insert') {
|
|
152
|
+
const type = edit.cellType as NotebookCellType
|
|
153
|
+
cellType = type
|
|
154
|
+
cellId = undefined
|
|
155
|
+
const cell: Json = { cell_type: type }
|
|
156
|
+
if (givesIds) {
|
|
157
|
+
cellId = freshId(new Set(cells.flatMap((c) => (typeof c.id === 'string' ? [c.id] : []))))
|
|
158
|
+
cell.id = cellId
|
|
159
|
+
}
|
|
160
|
+
cell.metadata = {}
|
|
161
|
+
if (type === 'code') {
|
|
162
|
+
cell.execution_count = null
|
|
163
|
+
cell.outputs = []
|
|
164
|
+
}
|
|
165
|
+
cell.source = sourceLines(edit.newSource as string)
|
|
166
|
+
cells.splice(edit.cellId === undefined ? 0 : at + 1, 0, cell)
|
|
167
|
+
} else {
|
|
168
|
+
const cell = cells[at] as Json
|
|
169
|
+
const had = typeof cell.cell_type === 'string' ? cell.cell_type : 'code'
|
|
170
|
+
const next = edit.cellType ?? had
|
|
171
|
+
if (next === 'code' && had !== 'code' && isRecord(cell.attachments)) {
|
|
172
|
+
if (Object.keys(cell.attachments).length > 0) {
|
|
173
|
+
return { ok: false, error: HAS_ATTACHMENTS_TO_CODE }
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
oldSource = sourceText(cell.source)
|
|
177
|
+
cell.source = sourceLines(edit.newSource as string)
|
|
178
|
+
cell.cell_type = next
|
|
179
|
+
cellType = next
|
|
180
|
+
if (next === 'code') {
|
|
181
|
+
// What the cell printed belongs to the code it ran, which is gone.
|
|
182
|
+
cell.execution_count = null
|
|
183
|
+
cell.outputs = []
|
|
184
|
+
cell.attachments = undefined
|
|
185
|
+
} else {
|
|
186
|
+
cell.execution_count = undefined
|
|
187
|
+
cell.outputs = undefined
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n'
|
|
192
|
+
let out = JSON.stringify(parsed, null, indentOf(text))
|
|
193
|
+
if (text.endsWith('\n')) out += '\n'
|
|
194
|
+
if (eol === '\r\n') out = out.replace(/\r?\n/gu, '\r\n')
|
|
195
|
+
return { ok: true, content: bom + out, cellId, cellType, oldSource, editMode }
|
|
196
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import type { ToolContext, ToolResult } from '../../types/tool/index.js'
|
|
4
|
+
import type { ToolFacts } from '../../types/tool/presentation.js'
|
|
5
|
+
import { defineTool } from '../defineTool.js'
|
|
6
|
+
import { resolveWithinAnyReal, toolRoots } from '../paths.js'
|
|
7
|
+
import { atomicWriteFile } from './atomic-write-file.js'
|
|
8
|
+
import { clip } from './classifier-input.js'
|
|
9
|
+
import { CURRENT_ON_DISK, movedSinceRead, staleFileError } from './content-fingerprint.js'
|
|
10
|
+
import { withFileMutationLock } from './file-mutation-lock.js'
|
|
11
|
+
import { lineDiff } from './line-diff.js'
|
|
12
|
+
import { applyNotebookEdit } from './notebook-cells.js'
|
|
13
|
+
import { isNotebookPath, notebookRefusal } from './notebook-guard.js'
|
|
14
|
+
import { NOTEBOOK_MAX_BYTES } from './read-media.js'
|
|
15
|
+
|
|
16
|
+
const inputSchema = z
|
|
17
|
+
.object({
|
|
18
|
+
notebook_path: z
|
|
19
|
+
.string()
|
|
20
|
+
.min(1)
|
|
21
|
+
.refine((value) => value.trim().length > 0, 'notebook_path must not be empty.')
|
|
22
|
+
.describe('The path of the Jupyter notebook (.ipynb) to edit.'),
|
|
23
|
+
cell_id: z
|
|
24
|
+
.string()
|
|
25
|
+
.optional()
|
|
26
|
+
.describe(
|
|
27
|
+
'The ID of the cell to edit. When inserting, the new cell goes after the cell with this ID, or at the beginning when it is not given.',
|
|
28
|
+
),
|
|
29
|
+
new_source: z
|
|
30
|
+
.string()
|
|
31
|
+
.optional()
|
|
32
|
+
.describe('The new source for the cell. Not needed to delete.'),
|
|
33
|
+
cell_type: z
|
|
34
|
+
.enum(['code', 'markdown'])
|
|
35
|
+
.optional()
|
|
36
|
+
.describe(
|
|
37
|
+
'The type of the cell (code or markdown). When left out, a replaced cell keeps its type. Required when inserting.',
|
|
38
|
+
),
|
|
39
|
+
edit_mode: z
|
|
40
|
+
.enum(['replace', 'insert', 'delete'])
|
|
41
|
+
.optional()
|
|
42
|
+
.describe('The kind of edit (replace, insert or delete). Defaults to replace.'),
|
|
43
|
+
})
|
|
44
|
+
.strict()
|
|
45
|
+
|
|
46
|
+
type NotebookEditInput = z.infer<typeof inputSchema>
|
|
47
|
+
|
|
48
|
+
const modelInputSchema: Record<string, unknown> = {
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: {
|
|
51
|
+
notebook_path: {
|
|
52
|
+
type: 'string',
|
|
53
|
+
description: 'The path of the Jupyter notebook (.ipynb) to edit.',
|
|
54
|
+
},
|
|
55
|
+
cell_id: {
|
|
56
|
+
type: 'string',
|
|
57
|
+
description:
|
|
58
|
+
'The ID of the cell to edit. When inserting, the new cell goes after the cell with this ID, or at the beginning when it is not given.',
|
|
59
|
+
},
|
|
60
|
+
new_source: {
|
|
61
|
+
type: 'string',
|
|
62
|
+
description: 'The new source for the cell. Not needed to delete.',
|
|
63
|
+
},
|
|
64
|
+
cell_type: {
|
|
65
|
+
type: 'string',
|
|
66
|
+
enum: ['code', 'markdown'],
|
|
67
|
+
description:
|
|
68
|
+
'The type of the cell (code or markdown). When left out, a replaced cell keeps its type. Required when inserting.',
|
|
69
|
+
},
|
|
70
|
+
edit_mode: {
|
|
71
|
+
type: 'string',
|
|
72
|
+
enum: ['replace', 'insert', 'delete'],
|
|
73
|
+
description: 'The kind of edit (replace, insert or delete). Defaults to replace.',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
required: ['notebook_path'],
|
|
77
|
+
additionalProperties: false,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const SHAPES =
|
|
81
|
+
'Replace: {"notebook_path":"a.ipynb","cell_id":"<id>","new_source":"print(1)"}. Insert after a cell: {"notebook_path":"a.ipynb","cell_id":"<id>","new_source":"# Title","cell_type":"markdown","edit_mode":"insert"} (leave out cell_id to insert at the top; cell_type is required). Delete: {"notebook_path":"a.ipynb","cell_id":"<id>","edit_mode":"delete"}.'
|
|
82
|
+
|
|
83
|
+
type Failure = { success: false; output: ''; error: string; facts?: ToolFacts }
|
|
84
|
+
|
|
85
|
+
function failure(error: string, facts?: ToolFacts): Failure {
|
|
86
|
+
return { success: false, output: '', error, ...(facts ? { facts } : {}) }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Why the notebook cannot be edited from what the session has read, or `undefined` when it can. */
|
|
90
|
+
function freshnessProblem(context: ToolContext, key: string, current: string): Failure | undefined {
|
|
91
|
+
const tracker = context.fileReadTracker
|
|
92
|
+
if (!tracker) return undefined
|
|
93
|
+
if (!tracker.hasRead(key)) {
|
|
94
|
+
return failure(
|
|
95
|
+
`${key} has not been read in this conversation, so no cell was changed. Read the notebook with the \`read\` tool (it shows each cell with its id), then call \`notebook_edit\` again.`,
|
|
96
|
+
{ kind: 'failure', reason: 'not_read', subject: key },
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
if (movedSinceRead(tracker, key, current)) {
|
|
100
|
+
tracker.recordDriftObserved?.(key)
|
|
101
|
+
return failure(staleFileError(key), {
|
|
102
|
+
kind: 'failure',
|
|
103
|
+
reason: 'changed_on_disk',
|
|
104
|
+
subject: key,
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
return undefined
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function record(context: ToolContext, key: string, content: string): void {
|
|
111
|
+
const tracker = context.fileReadTracker
|
|
112
|
+
if (!tracker) return
|
|
113
|
+
if (tracker.recordEdit && context.toolUseId) {
|
|
114
|
+
tracker.recordEdit(key, content, context.toolUseId)
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
tracker.recordRead(key, content)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function readHost(path: string): Promise<string | undefined> {
|
|
121
|
+
try {
|
|
122
|
+
return await readFile(path, 'utf-8')
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if ((error as { code?: unknown } | null)?.code === 'ENOENT') return undefined
|
|
125
|
+
throw error
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* What can be refused before a person is asked: arguments that cannot work, a
|
|
131
|
+
* notebook the session never read, one that moved since it was read. The text
|
|
132
|
+
* is the one `execute` gives.
|
|
133
|
+
*/
|
|
134
|
+
async function refuseBeforeReview(
|
|
135
|
+
input: NotebookEditInput,
|
|
136
|
+
context: ToolContext,
|
|
137
|
+
): Promise<{ readonly error: string; readonly facts?: ToolFacts } | undefined> {
|
|
138
|
+
const parsed = inputSchema.safeParse(input)
|
|
139
|
+
if (!parsed.success) return undefined
|
|
140
|
+
const path = parsed.data.notebook_path
|
|
141
|
+
if (!isNotebookPath(path)) return { error: NOT_A_NOTEBOOK }
|
|
142
|
+
if (context.sandbox) return undefined
|
|
143
|
+
let key: string
|
|
144
|
+
let current: string | undefined
|
|
145
|
+
try {
|
|
146
|
+
key = await resolveWithinAnyReal(toolRoots(context), path)
|
|
147
|
+
current = await readHost(key)
|
|
148
|
+
} catch {
|
|
149
|
+
return undefined
|
|
150
|
+
}
|
|
151
|
+
if (current === undefined) return undefined
|
|
152
|
+
const problem = freshnessProblem(context, key, current)
|
|
153
|
+
return problem
|
|
154
|
+
? { error: problem.error, ...(problem.facts ? { facts: problem.facts } : {}) }
|
|
155
|
+
: undefined
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const NOT_A_NOTEBOOK =
|
|
159
|
+
'notebook_path must name a Jupyter notebook (a .ipynb file). To change any other file use `edit`.'
|
|
160
|
+
|
|
161
|
+
export const NotebookEditTool = defineTool({
|
|
162
|
+
name: 'notebook_edit',
|
|
163
|
+
description:
|
|
164
|
+
'Replaces, inserts or deletes a single cell in a Jupyter notebook (.ipynb file). Everything else in the notebook (other cells, metadata, format) is kept as it was (the file is saved as JSON with the indent it had, so number spellings such as `1.0` may become `1`; a notebook that cannot be rewritten exactly is refused).\n\nUsage:\n- You must use the `read` tool on the notebook in this conversation before editing; this tool fails otherwise, and also when the notebook changed on disk since you read it.\n- `cell_id` is the `id` shown in the `<cell id="...">` rows of the `read` output. It is required for `replace` and `delete`.\n- `edit_mode` defaults to `replace`, which sets the cell\'s source (and clears the output and run count of a code cell, which belonged to the old code). `insert` adds a new cell after the cell with the given `cell_id`, or at the beginning of the notebook when `cell_id` is left out; `cell_type` is required when inserting. `delete` removes the cell.\n- A notebook change that is not a single cell (the notebook\'s metadata, several cells at once) is a job for `edit`; use this tool for cell text.',
|
|
165
|
+
inputSchema,
|
|
166
|
+
modelInputSchema,
|
|
167
|
+
enforceModelInput: true,
|
|
168
|
+
validationErrorHint: SHAPES,
|
|
169
|
+
malformedInputHint: `${SHAPES} Inside new_source, a newline is \\n and a quote is \\".`,
|
|
170
|
+
category: 'filesystem',
|
|
171
|
+
// Declared so a path outside the turn's roots is reviewed before the call
|
|
172
|
+
// runs. See `ToolDefinition.pathArgument`.
|
|
173
|
+
pathArgument: 'notebook_path',
|
|
174
|
+
refuseBeforeReview,
|
|
175
|
+
classifierInput: (input: NotebookEditInput) =>
|
|
176
|
+
clip(
|
|
177
|
+
`${input.notebook_path}: ${input.edit_mode ?? 'replace'} cell ${input.cell_id ?? '(start)'}${input.new_source !== undefined ? `, ${input.new_source.length} characters` : ''}`,
|
|
178
|
+
500,
|
|
179
|
+
),
|
|
180
|
+
permissions: ['file_write'],
|
|
181
|
+
readOnly: false,
|
|
182
|
+
destructive: false,
|
|
183
|
+
concurrencySafe: false,
|
|
184
|
+
|
|
185
|
+
presentCall(input: NotebookEditInput) {
|
|
186
|
+
return { kind: 'generic' as const, label: input.notebook_path }
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
presentResult(_input: NotebookEditInput, result: ToolResult) {
|
|
190
|
+
return { kind: 'generic' as const, label: result.output?.split('\n')[0] ?? '' }
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
async execute(input: NotebookEditInput, context) {
|
|
194
|
+
const parsed = inputSchema.safeParse(input)
|
|
195
|
+
if (!parsed.success) {
|
|
196
|
+
return failure(
|
|
197
|
+
`Invalid notebook_edit input: ${parsed.error.issues.map((issue) => issue.message).join('; ')}`,
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
const valid = parsed.data
|
|
201
|
+
if (!isNotebookPath(valid.notebook_path)) return failure(NOT_A_NOTEBOOK)
|
|
202
|
+
const edit = {
|
|
203
|
+
cellId: valid.cell_id,
|
|
204
|
+
newSource: valid.new_source,
|
|
205
|
+
cellType: valid.cell_type,
|
|
206
|
+
editMode: valid.edit_mode ?? 'replace',
|
|
207
|
+
} as const
|
|
208
|
+
|
|
209
|
+
const hostPath = context.sandbox
|
|
210
|
+
? undefined
|
|
211
|
+
: await resolveWithinAnyReal(toolRoots(context), valid.notebook_path)
|
|
212
|
+
const key = hostPath ?? valid.notebook_path
|
|
213
|
+
const lockKey = context.sandbox ? `sandbox:${key}` : `local:${key}`
|
|
214
|
+
|
|
215
|
+
return withFileMutationLock(lockKey, async () => {
|
|
216
|
+
let before: string | undefined
|
|
217
|
+
if (context.sandbox) {
|
|
218
|
+
try {
|
|
219
|
+
before = (await context.sandbox.readFile(key)).toString('utf-8')
|
|
220
|
+
} catch {
|
|
221
|
+
before = undefined
|
|
222
|
+
}
|
|
223
|
+
} else {
|
|
224
|
+
before = await readHost(key)
|
|
225
|
+
}
|
|
226
|
+
if (before === undefined) return failure(`Notebook file does not exist: ${key}.`)
|
|
227
|
+
if (Buffer.byteLength(before) > NOTEBOOK_MAX_BYTES) {
|
|
228
|
+
return failure(
|
|
229
|
+
`${key} is larger than this tool reads. Change it with \`edit\` or a shell command. The file was not changed.`,
|
|
230
|
+
)
|
|
231
|
+
}
|
|
232
|
+
const stale = freshnessProblem(context, key, before)
|
|
233
|
+
if (stale) return stale
|
|
234
|
+
|
|
235
|
+
const outcome = applyNotebookEdit(before, edit)
|
|
236
|
+
if (!outcome.ok) return failure(outcome.error)
|
|
237
|
+
// The same check `edit` and `write` make: whatever was done, the file must still open.
|
|
238
|
+
const refusal = notebookRefusal(key, outcome.content, before)
|
|
239
|
+
if (refusal) return failure(refusal)
|
|
240
|
+
if (Buffer.byteLength(outcome.content) > NOTEBOOK_MAX_BYTES) {
|
|
241
|
+
return failure(
|
|
242
|
+
'The updated notebook would be larger than this tool can read. The file was not changed.',
|
|
243
|
+
)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (context.sandbox) await context.sandbox.writeFile(key, outcome.content)
|
|
247
|
+
else await atomicWriteFile(key, outcome.content)
|
|
248
|
+
record(context, key, outcome.content)
|
|
249
|
+
|
|
250
|
+
const diff = lineDiff(before, outcome.content)
|
|
251
|
+
const named = outcome.cellId ? `cell ${outcome.cellId}` : 'a new cell'
|
|
252
|
+
const done =
|
|
253
|
+
outcome.editMode === 'replace'
|
|
254
|
+
? `Updated cell ${outcome.cellId}`
|
|
255
|
+
: outcome.editMode === 'insert'
|
|
256
|
+
? `Inserted ${named}`
|
|
257
|
+
: `Deleted cell ${outcome.cellId}`
|
|
258
|
+
return {
|
|
259
|
+
success: true as const,
|
|
260
|
+
output: `${done} in ${key} (+${diff.added} -${diff.removed})${context.sandbox ? ' [sandboxed]' : ''}. ${CURRENT_ON_DISK}`,
|
|
261
|
+
data: {
|
|
262
|
+
notebook_path: key,
|
|
263
|
+
edit_mode: outcome.editMode,
|
|
264
|
+
cell_type: outcome.cellType,
|
|
265
|
+
...(outcome.cellId ? { cell_id: outcome.cellId } : {}),
|
|
266
|
+
...(outcome.oldSource !== undefined ? { old_source: outcome.oldSource } : {}),
|
|
267
|
+
},
|
|
268
|
+
facts: {
|
|
269
|
+
kind: 'edit' as const,
|
|
270
|
+
path: key,
|
|
271
|
+
replacements: 1,
|
|
272
|
+
added: diff.added,
|
|
273
|
+
removed: diff.removed,
|
|
274
|
+
...(diff.hunks.length > 0 ? { hunks: diff.hunks } : {}),
|
|
275
|
+
...(diff.truncated ? { hunksTruncated: true as const } : {}),
|
|
276
|
+
},
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
},
|
|
280
|
+
})
|
|
@@ -43,7 +43,8 @@ function shapeProblem(parsed: unknown): string | undefined {
|
|
|
43
43
|
function bodyProblem(content: string): string | undefined {
|
|
44
44
|
let parsed: unknown
|
|
45
45
|
try {
|
|
46
|
-
|
|
46
|
+
// A byte-order mark is how some editors save a notebook that opens fine.
|
|
47
|
+
parsed = JSON.parse(content.replace(/^\uFEFF/u, ''))
|
|
47
48
|
} catch (error) {
|
|
48
49
|
const reason = error instanceof Error ? error.message : String(error)
|
|
49
50
|
return `it is not valid JSON (${reason})`
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import type { ToolDefinition } from '../../types/tool/index.js'
|
|
3
|
+
import { defineTool } from '../defineTool.js'
|
|
4
|
+
|
|
5
|
+
export const PUSH_NOTIFICATION_TOOL_NAME = 'push_notification'
|
|
6
|
+
|
|
7
|
+
/** The longest message one notification carries; the rest is cut. */
|
|
8
|
+
export const PUSH_NOTIFICATION_MAX_CHARS = 200
|
|
9
|
+
|
|
10
|
+
/** The least time between two notifications from one session. */
|
|
11
|
+
export const PUSH_NOTIFICATION_MIN_INTERVAL_MS = 60_000
|
|
12
|
+
|
|
13
|
+
const inputSchema = z.object({
|
|
14
|
+
message: z
|
|
15
|
+
.string()
|
|
16
|
+
.min(1)
|
|
17
|
+
.describe(
|
|
18
|
+
`The notification text. One short line, under ${PUSH_NOTIFICATION_MAX_CHARS} characters; a longer one is cut. Say what finished or what you need. Never put a secret, a token or file contents in it.`,
|
|
19
|
+
),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
/** What the host did with one request. */
|
|
23
|
+
export type PushNotificationOutcome =
|
|
24
|
+
| { readonly status: 'sent' }
|
|
25
|
+
/** The person is at the terminal already, so a notification would repeat what they see. */
|
|
26
|
+
| { readonly status: 'user-present' }
|
|
27
|
+
/** The person turned notifications from the model off. */
|
|
28
|
+
| { readonly status: 'disabled' }
|
|
29
|
+
| { readonly status: 'unavailable'; readonly detail: string }
|
|
30
|
+
|
|
31
|
+
export interface PushNotificationToolOptions {
|
|
32
|
+
/** Raise one notification. The message is already one safe line. */
|
|
33
|
+
readonly notify: (message: string) => PushNotificationOutcome | Promise<PushNotificationOutcome>
|
|
34
|
+
/** The clock, in milliseconds; a test hands its own. */
|
|
35
|
+
readonly now?: () => number
|
|
36
|
+
/** Overrides {@link PUSH_NOTIFICATION_MIN_INTERVAL_MS}. */
|
|
37
|
+
readonly minIntervalMs?: number
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isStripped(code: number): boolean {
|
|
41
|
+
return (
|
|
42
|
+
code <= 0x1f ||
|
|
43
|
+
(code >= 0x7f && code <= 0x9f) ||
|
|
44
|
+
code === 0xad ||
|
|
45
|
+
code === 0x061c ||
|
|
46
|
+
code === 0x180e ||
|
|
47
|
+
(code >= 0x200b && code <= 0x200f) ||
|
|
48
|
+
(code >= 0x2028 && code <= 0x202e) ||
|
|
49
|
+
(code >= 0x2060 && code <= 0x206f) ||
|
|
50
|
+
code === 0xfeff ||
|
|
51
|
+
(code >= 0xfff9 && code <= 0xfffb)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* One safe line: control, format and bidirectional characters removed (a
|
|
57
|
+
* terminal reads escape sequences in what it is shown), whitespace runs
|
|
58
|
+
* collapsed, cut at the cap with an ellipsis.
|
|
59
|
+
*/
|
|
60
|
+
export function pushNotificationLine(text: string): string {
|
|
61
|
+
let out = ''
|
|
62
|
+
for (const ch of text) {
|
|
63
|
+
const code = ch.codePointAt(0) ?? 0
|
|
64
|
+
if (code === 0x09 || code === 0x0a || code === 0x0d) out += ' '
|
|
65
|
+
else if (!isStripped(code)) out += ch
|
|
66
|
+
}
|
|
67
|
+
out = out.replace(/\s+/g, ' ').trim()
|
|
68
|
+
const chars = [...out]
|
|
69
|
+
return chars.length > PUSH_NOTIFICATION_MAX_CHARS
|
|
70
|
+
? `${chars.slice(0, PUSH_NOTIFICATION_MAX_CHARS - 1).join('')}…`
|
|
71
|
+
: out
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const DESCRIPTION = `Send a desktop notification to the person's terminal, to pull their attention back. Use it only when a long task has finished or you need a decision and they have probably walked away, or they asked for notifications. Do not use it for routine progress, for a quick task, or while they are in the conversation: it is not sent when they are at the terminal, and at most one goes out a minute. One short line; never a secret.`
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Raises a notification through the host. The host owns delivery (the tool
|
|
78
|
+
* knows no terminal); the tool owns the message (one cleaned line, capped) and
|
|
79
|
+
* the pace (one a minute per session, counted only for notifications sent).
|
|
80
|
+
* Build it only where a person can see a notification; a host with nobody to
|
|
81
|
+
* notify does not offer it.
|
|
82
|
+
*/
|
|
83
|
+
export function createPushNotificationTool(
|
|
84
|
+
options: PushNotificationToolOptions,
|
|
85
|
+
): ToolDefinition<z.infer<typeof inputSchema>> {
|
|
86
|
+
const now = options.now ?? Date.now
|
|
87
|
+
const interval = options.minIntervalMs ?? PUSH_NOTIFICATION_MIN_INTERVAL_MS
|
|
88
|
+
let lastSentAt: number | undefined
|
|
89
|
+
return defineTool({
|
|
90
|
+
name: PUSH_NOTIFICATION_TOOL_NAME,
|
|
91
|
+
description: DESCRIPTION,
|
|
92
|
+
inputSchema,
|
|
93
|
+
category: 'custom',
|
|
94
|
+
permissions: [],
|
|
95
|
+
readOnly: true,
|
|
96
|
+
destructive: false,
|
|
97
|
+
concurrencySafe: false,
|
|
98
|
+
presentCall: (input) => ({
|
|
99
|
+
kind: 'generic',
|
|
100
|
+
label: `Notify · ${pushNotificationLine(input.message)}`,
|
|
101
|
+
presentation: 'activity',
|
|
102
|
+
}),
|
|
103
|
+
async execute(input) {
|
|
104
|
+
const message = pushNotificationLine(input.message)
|
|
105
|
+
if (message === '') {
|
|
106
|
+
const text = 'Not sent: the message is empty once control characters are removed.'
|
|
107
|
+
return { success: false, output: text, error: text }
|
|
108
|
+
}
|
|
109
|
+
const at = now()
|
|
110
|
+
if (lastSentAt !== undefined && at - lastSentAt < interval) {
|
|
111
|
+
const wait = Math.ceil((interval - (at - lastSentAt)) / 1000)
|
|
112
|
+
return {
|
|
113
|
+
success: true,
|
|
114
|
+
output: `Not sent: a notification went out a moment ago (one a minute). Try again in ${wait}s only if it still matters.`,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const outcome = await options.notify(message)
|
|
118
|
+
switch (outcome.status) {
|
|
119
|
+
case 'sent':
|
|
120
|
+
lastSentAt = at
|
|
121
|
+
return { success: true, output: 'Notification sent.', data: { message } }
|
|
122
|
+
case 'user-present':
|
|
123
|
+
return {
|
|
124
|
+
success: true,
|
|
125
|
+
output:
|
|
126
|
+
'Not sent: the person is at the terminal, so your reply here already reaches them.',
|
|
127
|
+
}
|
|
128
|
+
case 'disabled':
|
|
129
|
+
return {
|
|
130
|
+
success: true,
|
|
131
|
+
output: 'Not sent: the person has turned notifications from the model off.',
|
|
132
|
+
}
|
|
133
|
+
case 'unavailable':
|
|
134
|
+
return { success: true, output: `Not sent: ${outcome.detail}` }
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
}
|
|
@@ -42,7 +42,9 @@ const inputSchema = z.object({
|
|
|
42
42
|
.int()
|
|
43
43
|
.min(0)
|
|
44
44
|
.optional()
|
|
45
|
-
.describe(
|
|
45
|
+
.describe(
|
|
46
|
+
'Line number to start from, counted from 1 (offset 100 is line 100, as grep reports it). Defaults to 1; 0 is read as 1.',
|
|
47
|
+
),
|
|
46
48
|
limit: z.coerce.number().optional().describe('Maximum number of lines to read'),
|
|
47
49
|
pages: z
|
|
48
50
|
.string()
|
|
@@ -61,7 +63,7 @@ const inputSchema = z.object({
|
|
|
61
63
|
export const ReadFileTool = defineTool({
|
|
62
64
|
name: 'read',
|
|
63
65
|
description:
|
|
64
|
-
'Reads a file and returns its contents with line numbers: a right-aligned number, an arrow (→) and the line as it is in the file. The number and arrow are not file text; the tabs and spaces after the arrow are. Also reads images (PNG, JPEG, WebP), which come back as pictures; PDFs, whose pages come back as images (pass pages, for example "1-5", at most 20 pages per request; a PDF of more than 10 pages needs it); and Jupyter notebooks (.ipynb), which come back as cells with their outputs. Supports readRange ([start,end], 1-indexed inclusive) or offset/limit for large files. Without a window it returns the first 2000 lines and says so — pass offset/limit to continue. A file over 256 KiB is refused unless a window is named, and a result over about 25,000 tokens is cut to a first page that names the next call; pass allow_large only when you need all of it.',
|
|
66
|
+
'Reads a file and returns its contents with line numbers: a right-aligned number, an arrow (→) and the line as it is in the file. The number and arrow are not file text; the tabs and spaces after the arrow are. Also reads images (PNG, JPEG, WebP), which come back as pictures; PDFs, whose pages come back as images (pass pages, for example "1-5", at most 20 pages per request; a PDF of more than 10 pages needs it); and Jupyter notebooks (.ipynb), which come back as cells with their outputs. Supports readRange ([start,end], 1-indexed inclusive) or offset (a line number from 1)/limit for large files. Without a window it returns the first 2000 lines and says so — pass offset/limit to continue. A file over 256 KiB is refused unless a window is named, and a result over about 25,000 tokens is cut to a first page that names the next call; pass allow_large only when you need all of it.',
|
|
65
67
|
inputSchema,
|
|
66
68
|
category: 'filesystem',
|
|
67
69
|
// Declared so a path outside the turn's roots can be reviewed before the
|
|
@@ -292,6 +294,10 @@ async function readMediaFile(
|
|
|
292
294
|
? await readPdfPages(buffer, input.pages, path, context.abortSignal)
|
|
293
295
|
: readNotebook(buffer, path)
|
|
294
296
|
if (!media.ok) return mediaFailure(path, buffer.length, media.message, sandboxed)
|
|
297
|
+
// A notebook shown whole is a read like any other: the body is what a later
|
|
298
|
+
// edit of it is checked against, and `notebook_edit` refuses a notebook the
|
|
299
|
+
// session has not read.
|
|
300
|
+
if (kind === 'notebook') context.fileReadTracker?.recordRead(path, buffer.toString('utf-8'))
|
|
295
301
|
const text = media.parts
|
|
296
302
|
.filter((part) => part.type === 'text')
|
|
297
303
|
.map((part) => part.text)
|
|
@@ -422,7 +428,7 @@ function windowKey(input: ReadWindowInput): string {
|
|
|
422
428
|
const range = input.readRange
|
|
423
429
|
const lines = range
|
|
424
430
|
? `r${range[0]}-${range[1]}`
|
|
425
|
-
: `o${input.offset ??
|
|
431
|
+
: `o${Math.max(1, input.offset ?? 1)}l${input.limit === undefined ? 'all' : input.limit}`
|
|
426
432
|
return input.allow_large === true ? `${lines}+large` : lines
|
|
427
433
|
}
|
|
428
434
|
|
|
@@ -414,7 +414,8 @@ export function readNotebook(bytes: Buffer, label: string): MediaRead {
|
|
|
414
414
|
}
|
|
415
415
|
let parsed: unknown
|
|
416
416
|
try {
|
|
417
|
-
|
|
417
|
+
// A byte-order mark is part of how some editors save a notebook, not of its JSON.
|
|
418
|
+
parsed = JSON.parse(bytes.toString('utf-8').replace(/^\uFEFF/u, ''))
|
|
418
419
|
} catch (error) {
|
|
419
420
|
return {
|
|
420
421
|
ok: false,
|