@howaboua/pi-durable-apply-patch 0.0.0-stage → 0.9.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.
@@ -0,0 +1,4 @@
1
+ import type { Context } from "@earendil-works/chord";
2
+ import { type ExecutionEnv } from "@earendil-works/pi-durable/env";
3
+ export declare function pathIdentity(path: string): string;
4
+ export declare function withMutationKeys<T>(env: ExecutionEnv, paths: string[], context: Context, run: () => Promise<T>, warnings?: string[]): Promise<T>;
@@ -0,0 +1,32 @@
1
+ export type Chunk = {
2
+ context: string | undefined;
3
+ oldLines: string[];
4
+ newLines: string[];
5
+ contextLines: {
6
+ old: number;
7
+ new: number;
8
+ }[];
9
+ eof: boolean;
10
+ };
11
+ export type Action = {
12
+ type: "add";
13
+ path: string;
14
+ contents: string;
15
+ } | {
16
+ type: "delete";
17
+ path: string;
18
+ } | {
19
+ type: "update";
20
+ path: string;
21
+ movePath: string | undefined;
22
+ chunks: Chunk[];
23
+ line: number;
24
+ };
25
+ export type ParsedPatch = {
26
+ actions: Action[];
27
+ environmentId: string | undefined;
28
+ };
29
+ export declare function trimRust(text: string, endOnly?: boolean): string;
30
+ export declare function parsePatch(input: string): ParsedPatch;
31
+ /** The pinned TS executor's pre-parse guard also rejects duplicate sources in empty update hunks. */
32
+ export declare function sourcePathsForDuplicateGuard(input: string): string[] | undefined;
@@ -0,0 +1,40 @@
1
+ import type { ExecutionEnv } from "@earendil-works/pi-durable/env";
2
+ import type { Action } from "./parser.ts";
3
+ export type ExecutePatchResult = {
4
+ changedFiles: string[];
5
+ createdFiles: string[];
6
+ deletedFiles: string[];
7
+ movedFiles: string[];
8
+ fuzz: number;
9
+ };
10
+ export type FileChange = {
11
+ path: string;
12
+ kind: "add";
13
+ content: string;
14
+ overwrittenContent: string | null;
15
+ } | {
16
+ path: string;
17
+ kind: "delete";
18
+ content: string;
19
+ } | {
20
+ path: string;
21
+ kind: "update";
22
+ movePath: string | null;
23
+ oldContent: string;
24
+ overwrittenMoveContent: string | null;
25
+ newContent: string;
26
+ };
27
+ export type PatchOutcome = {
28
+ result: ExecutePatchResult;
29
+ changes: FileChange[];
30
+ exact: boolean;
31
+ serializationWarnings?: string[];
32
+ };
33
+ export declare class ExecutePatchError extends Error {
34
+ readonly outcome: PatchOutcome;
35
+ readonly action: Action | undefined;
36
+ constructor(message: string, outcome: PatchOutcome, action: Action | undefined);
37
+ get result(): ExecutePatchResult;
38
+ hasPartialSuccess(): boolean;
39
+ }
40
+ export declare function summarize(env: ExecutionEnv, changes: FileChange[], exact: boolean): PatchOutcome;
@@ -0,0 +1,42 @@
1
+ import type { ApplyPatchToolDetails } from "./index.ts";
2
+ import type { ExecutePatchResult, FileChange } from "./patch-outcome.ts";
3
+ export interface ApplyPatchPresentationState {
4
+ status: ApplyPatchToolDetails["status"];
5
+ result: ExecutePatchResult;
6
+ exact: boolean;
7
+ failedTargets: string[];
8
+ serializationWarnings: string[];
9
+ /** Committed file bodies, acquired by the host only when requested. */
10
+ detail: FileChange[] | null;
11
+ }
12
+ /** Preserve partial failure and committed metadata without retaining file bodies. */
13
+ export declare function createApplyPatchPresentationState(receipt: ApplyPatchToolDetails): ApplyPatchPresentationState;
14
+ declare function parseState(value: unknown): ApplyPatchPresentationState;
15
+ export declare const applyPatchCapability: {
16
+ id: string;
17
+ version: number;
18
+ parseState: typeof parseState;
19
+ actions: readonly string[];
20
+ streams: readonly string[];
21
+ presentations: readonly string[];
22
+ };
23
+ export declare const applyPatchSummary: {
24
+ id: string;
25
+ select: (state: ApplyPatchPresentationState) => {
26
+ status: "partial_failure" | "success";
27
+ exact: boolean;
28
+ changed: number;
29
+ created: number;
30
+ deleted: number;
31
+ moved: number;
32
+ failedTargets: string[];
33
+ serializationWarnings: string[];
34
+ };
35
+ requests: readonly string[];
36
+ };
37
+ export declare const applyPatchDetail: {
38
+ id: string;
39
+ select: (state: ApplyPatchPresentationState) => FileChange[] | null;
40
+ requests: readonly string[];
41
+ };
42
+ export {};
@@ -0,0 +1,120 @@
1
+ // packages/apply-patch/src/presentation.ts
2
+ function createApplyPatchPresentationState(receipt) {
3
+ return parseState({
4
+ status: receipt.status,
5
+ result: receipt.result,
6
+ exact: receipt.exact,
7
+ failedTargets: receipt.failedTargets,
8
+ serializationWarnings: receipt.serializationWarnings ?? [],
9
+ detail: null
10
+ });
11
+ }
12
+ function parseState(value) {
13
+ const state = record(value);
14
+ const status = state["status"];
15
+ if (status !== "success" && status !== "partial_failure")
16
+ throw new TypeError("Invalid patch status");
17
+ const receipt = record(state["result"]);
18
+ const result = {
19
+ changedFiles: strings(receipt["changedFiles"]),
20
+ createdFiles: strings(receipt["createdFiles"]),
21
+ deletedFiles: strings(receipt["deletedFiles"]),
22
+ movedFiles: strings(receipt["movedFiles"]),
23
+ fuzz: receipt["fuzz"] === 0 ? 0 : receipt["fuzz"] === 1 ? 1 : invalid()
24
+ };
25
+ if (typeof state["exact"] !== "boolean" || state["exact"] !== (result.fuzz === 0))
26
+ throw new TypeError("Invalid patch exactness");
27
+ const failedTargets = strings(state["failedTargets"]);
28
+ if (status === "success" && failedTargets.length)
29
+ throw new TypeError("Successful patch has failed targets");
30
+ let detail = null;
31
+ if (state["detail"] !== null) {
32
+ if (!Array.isArray(state["detail"]))
33
+ invalid();
34
+ detail = state["detail"].map(change);
35
+ }
36
+ return {
37
+ status,
38
+ result,
39
+ exact: state["exact"],
40
+ failedTargets,
41
+ serializationWarnings: strings(state["serializationWarnings"]),
42
+ detail
43
+ };
44
+ }
45
+ function change(value) {
46
+ const entry = record(value);
47
+ const path = string(entry["path"]);
48
+ switch (entry["kind"]) {
49
+ case "add":
50
+ return {
51
+ kind: "add",
52
+ path,
53
+ content: string(entry["content"]),
54
+ overwrittenContent: nullable(entry["overwrittenContent"])
55
+ };
56
+ case "delete":
57
+ return { kind: "delete", path, content: string(entry["content"]) };
58
+ case "update":
59
+ return {
60
+ kind: "update",
61
+ path,
62
+ movePath: nullable(entry["movePath"]),
63
+ oldContent: string(entry["oldContent"]),
64
+ newContent: string(entry["newContent"]),
65
+ overwrittenMoveContent: nullable(entry["overwrittenMoveContent"])
66
+ };
67
+ default:
68
+ return invalid();
69
+ }
70
+ }
71
+ function record(value) {
72
+ if (!value || typeof value !== "object" || Array.isArray(value))
73
+ invalid();
74
+ return value;
75
+ }
76
+ function invalid() {
77
+ throw new TypeError("Invalid patch receipt");
78
+ }
79
+ function string(value) {
80
+ return typeof value === "string" ? value : invalid();
81
+ }
82
+ function nullable(value) {
83
+ return value === null ? null : string(value);
84
+ }
85
+ function strings(value) {
86
+ return Array.isArray(value) ? value.map(string) : invalid();
87
+ }
88
+ var applyPatchCapability = {
89
+ id: "apply-patch",
90
+ version: 1,
91
+ parseState,
92
+ actions: [],
93
+ streams: [],
94
+ presentations: ["summary", "detail"]
95
+ };
96
+ var applyPatchSummary = {
97
+ id: "summary",
98
+ select: (state) => ({
99
+ status: state.status,
100
+ exact: state.exact,
101
+ changed: state.result.changedFiles.length,
102
+ created: state.result.createdFiles.length,
103
+ deleted: state.result.deletedFiles.length,
104
+ moved: state.result.movedFiles.length,
105
+ failedTargets: state.failedTargets,
106
+ serializationWarnings: state.serializationWarnings
107
+ }),
108
+ requests: ["detail"]
109
+ };
110
+ var applyPatchDetail = {
111
+ id: "detail",
112
+ select: (state) => state.detail,
113
+ requests: []
114
+ };
115
+ export {
116
+ applyPatchCapability,
117
+ applyPatchDetail,
118
+ applyPatchSummary,
119
+ createApplyPatchPresentationState
120
+ };
@@ -0,0 +1,2 @@
1
+ import { type Chunk } from "./parser.ts";
2
+ export declare function updateContents(contents: string, path: string, chunks: Chunk[]): string;
package/package.json CHANGED
@@ -1,6 +1,44 @@
1
1
  {
2
- "name": "@howaboua/pi-durable-apply-patch",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@howaboua/pi-durable-apply-patch",
3
+ "version": "0.9.1",
4
+ "description": "Codex-style patches for Pi Durable",
5
+ "type": "module",
6
+ "license": "Apache-2.0 AND MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/IgorWarzocha/pi-durable-components.git",
10
+ "directory": "packages/apply-patch"
11
+ },
12
+ "author": "Igor Warzocha",
13
+ "engines": {
14
+ "node": ">=22.19.0"
15
+ },
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ "./presentation": {
20
+ "types": "./dist/presentation.d.ts",
21
+ "import": "./dist/presentation.js"
22
+ },
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "README.md",
31
+ "NOTICE",
32
+ "LICENSE"
33
+ ],
34
+ "sideEffects": false,
35
+ "peerDependencies": {
36
+ "@earendil-works/chord": "1.4.0",
37
+ "@earendil-works/pi-ai": "1.4.0",
38
+ "@earendil-works/pi-durable": "1.4.0",
39
+ "@modelcontextprotocol/sdk": "1.25.2"
40
+ },
41
+ "publishConfig": {
42
+ "access": "public"
43
+ }
44
+ }