@romandmitri/ai-patch 0.3.0 → 0.4.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/README.md +8 -13
- package/dist/content/ContentFormat.d.ts +1 -0
- package/dist/content/ContentFormat.d.ts.map +1 -1
- package/dist/content/ContentFormat.js +5 -0
- package/dist/content/ContentFormat.js.map +1 -1
- package/dist/index.d.ts +2 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -2
- package/dist/index.js.map +1 -1
- package/dist/operation/OperationType.d.ts +0 -1
- package/dist/operation/OperationType.d.ts.map +1 -1
- package/dist/operation/OperationType.js +0 -3
- package/dist/operation/OperationType.js.map +1 -1
- package/dist/operation/Operation_Delete.d.ts +1 -1
- package/dist/operation/Operation_Delete.d.ts.map +1 -1
- package/dist/operation/Operation_Delete.js +1 -1
- package/dist/operation/Operation_Delete.js.map +1 -1
- package/dist/operation/Operation_InsertAfter.d.ts +1 -1
- package/dist/operation/Operation_InsertAfter.d.ts.map +1 -1
- package/dist/operation/Operation_InsertAfter.js +1 -1
- package/dist/operation/Operation_InsertAfter.js.map +1 -1
- package/dist/operation/Operation_InsertBefore.d.ts +1 -1
- package/dist/operation/Operation_InsertBefore.d.ts.map +1 -1
- package/dist/operation/Operation_InsertBefore.js +1 -1
- package/dist/operation/Operation_InsertBefore.js.map +1 -1
- package/dist/operation/Operation_Replace.d.ts +1 -1
- package/dist/operation/Operation_Replace.d.ts.map +1 -1
- package/dist/operation/Operation_Replace.js +1 -1
- package/dist/operation/Operation_Replace.js.map +1 -1
- package/dist/patch/{Edit.d.ts → Patch.d.ts} +5 -5
- package/dist/patch/Patch.d.ts.map +1 -0
- package/dist/patch/Patch.js +2 -0
- package/dist/patch/Patch.js.map +1 -0
- package/dist/patch/PatchIndex.d.ts +2 -0
- package/dist/patch/PatchIndex.d.ts.map +1 -0
- package/dist/patch/PatchIndex.js +2 -0
- package/dist/patch/PatchIndex.js.map +1 -0
- package/dist/{patch → patcher}/Patcher.d.ts +1 -1
- package/dist/patcher/Patcher.d.ts.map +1 -0
- package/dist/patcher/Patcher.js.map +1 -0
- package/dist/{patch → patcher}/Patcher_Text.d.ts +3 -3
- package/dist/patcher/Patcher_Text.d.ts.map +1 -0
- package/dist/{patch → patcher}/Patcher_Text.js +28 -29
- package/dist/patcher/Patcher_Text.js.map +1 -0
- package/dist/{target/Target.d.ts → patchlet/Patchlet.d.ts} +8 -7
- package/dist/patchlet/Patchlet.d.ts.map +1 -0
- package/dist/patchlet/Patchlet.js +45 -0
- package/dist/patchlet/Patchlet.js.map +1 -0
- package/dist/patchlet/test/Patchlet.apply.test.d.ts +2 -0
- package/dist/patchlet/test/Patchlet.apply.test.d.ts.map +1 -0
- package/dist/{target/test/Target.apply.test.js → patchlet/test/Patchlet.apply.test.js} +10 -14
- package/dist/patchlet/test/Patchlet.apply.test.js.map +1 -0
- package/dist/patchlet/test/Patchlet.complex.test.d.ts +2 -0
- package/dist/patchlet/test/Patchlet.complex.test.d.ts.map +1 -0
- package/dist/{target/test/Target.complex.test.js → patchlet/test/Patchlet.complex.test.js} +9 -15
- package/dist/patchlet/test/Patchlet.complex.test.js.map +1 -0
- package/dist/patchlet/test/Patchlet.schema.test.d.ts +2 -0
- package/dist/patchlet/test/Patchlet.schema.test.d.ts.map +1 -0
- package/dist/{target/test/Target.schema.test.js → patchlet/test/Patchlet.schema.test.js} +9 -12
- package/dist/patchlet/test/Patchlet.schema.test.js.map +1 -0
- package/docs/development.md +1 -1
- package/package.json +2 -2
- package/dist/error/PatchError.d.ts +0 -19
- package/dist/error/PatchError.d.ts.map +0 -1
- package/dist/error/PatchError.js +0 -26
- package/dist/error/PatchError.js.map +0 -1
- package/dist/patch/Edit.d.ts.map +0 -1
- package/dist/patch/Edit.js +0 -2
- package/dist/patch/Edit.js.map +0 -1
- package/dist/patch/InputIndex.d.ts +0 -2
- package/dist/patch/InputIndex.d.ts.map +0 -1
- package/dist/patch/InputIndex.js +0 -2
- package/dist/patch/InputIndex.js.map +0 -1
- package/dist/patch/Patcher.d.ts.map +0 -1
- package/dist/patch/Patcher.js.map +0 -1
- package/dist/patch/Patcher_Text.d.ts.map +0 -1
- package/dist/patch/Patcher_Text.js.map +0 -1
- package/dist/target/Target.d.ts.map +0 -1
- package/dist/target/Target.js +0 -64
- package/dist/target/Target.js.map +0 -1
- package/dist/target/test/Target.apply.test.d.ts +0 -2
- package/dist/target/test/Target.apply.test.d.ts.map +0 -1
- package/dist/target/test/Target.apply.test.js.map +0 -1
- package/dist/target/test/Target.complex.test.d.ts +0 -2
- package/dist/target/test/Target.complex.test.d.ts.map +0 -1
- package/dist/target/test/Target.complex.test.js.map +0 -1
- package/dist/target/test/Target.errors.test.d.ts +0 -2
- package/dist/target/test/Target.errors.test.d.ts.map +0 -1
- package/dist/target/test/Target.errors.test.js +0 -108
- package/dist/target/test/Target.errors.test.js.map +0 -1
- package/dist/target/test/Target.schema.test.d.ts +0 -2
- package/dist/target/test/Target.schema.test.d.ts.map +0 -1
- package/dist/target/test/Target.schema.test.js.map +0 -1
- /package/dist/{patch → patcher}/Patcher.js +0 -0
package/README.md
CHANGED
|
@@ -32,9 +32,6 @@ Text is the currently supported content format. JSON coming... if needed.
|
|
|
32
32
|
|
|
33
33
|
## Structured Output
|
|
34
34
|
|
|
35
|
-
`Target.toSchema()` returns a Zod schema for one operation array. Use it directly for a single target or compose several target schemas into a caller-owned
|
|
36
|
-
object.
|
|
37
|
-
|
|
38
35
|
```ts
|
|
39
36
|
import { Target } from "@romandmitri/ai-patch";
|
|
40
37
|
import { Output, generateText } from "ai";
|
|
@@ -44,27 +41,25 @@ const document = "# Guide\n\nOld instructions.\n";
|
|
|
44
41
|
const summary = "# Summary\n\nOld summary.\n";
|
|
45
42
|
const updateInstructions = "Update the guide and keep its summary in sync.";
|
|
46
43
|
|
|
47
|
-
const
|
|
48
|
-
const
|
|
44
|
+
const documentPatchlet = Patchlet.from(document);
|
|
45
|
+
const summaryPatchlet = Patchlet.from(summary);
|
|
49
46
|
|
|
50
47
|
const { output } = await generateText({
|
|
51
48
|
model,
|
|
52
49
|
instructions: "Return only the patches needed to perform the requested update.",
|
|
53
50
|
messages: [
|
|
54
51
|
{ role: "user", content: "The [update_instructions]:\n" + updateInstructions },
|
|
55
|
-
{ role: "user", content: "The [document]:\n" +
|
|
56
|
-
{ role: "user", content: "The [summary]:\n" +
|
|
52
|
+
{ role: "user", content: "The [document]:\n" + documentPatchlet.content },
|
|
53
|
+
{ role: "user", content: "The [summary]:\n" + summaryPatchlet.content },
|
|
57
54
|
],
|
|
58
55
|
output: Output.object({
|
|
59
56
|
schema: z.object({
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
documentPatches: documentPatchlet.toSchema(),
|
|
58
|
+
summaryPatches: summaryPatchlet.toSchema(),
|
|
62
59
|
}),
|
|
63
60
|
}),
|
|
64
61
|
});
|
|
65
62
|
|
|
66
|
-
const updatedDocument =
|
|
67
|
-
const updatedSummary =
|
|
63
|
+
const updatedDocument = documentPatchlet.patch(output.documentPatches);
|
|
64
|
+
const updatedSummary = summaryPatchlet.patch(output.summaryPatches);
|
|
68
65
|
```
|
|
69
|
-
|
|
70
|
-
Each schema describes one immutable target. Operations cannot address another target or change which content snapshot supplies their coordinates.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContentFormat.d.ts","sourceRoot":"","sources":["../../src/content/ContentFormat.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IAGxB,IAAI,SAAS;CACb"}
|
|
1
|
+
{"version":3,"file":"ContentFormat.d.ts","sourceRoot":"","sources":["../../src/content/ContentFormat.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IAGxB,IAAI,SAAS;CACb;AAED,eAAO,MAAM,oBAAoB,WAAY,aAAa,GAAG,SAAS,KAAG,OAGxE,CAAA"}
|
|
@@ -4,4 +4,9 @@ export var ContentFormat;
|
|
|
4
4
|
// Json = "json",
|
|
5
5
|
ContentFormat["Text"] = "text";
|
|
6
6
|
})(ContentFormat || (ContentFormat = {}));
|
|
7
|
+
export const isValidContentFormat = (format) => {
|
|
8
|
+
if (!format)
|
|
9
|
+
return false;
|
|
10
|
+
return Object.values(ContentFormat).includes(format);
|
|
11
|
+
};
|
|
7
12
|
//# sourceMappingURL=ContentFormat.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ContentFormat.js","sourceRoot":"","sources":["../../src/content/ContentFormat.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAIX;AAJD,WAAY,aAAa;IACxB,2DAA2D;IAC3D,iBAAiB;IACjB,8BAAa,CAAA;AACd,CAAC,EAJW,aAAa,KAAb,aAAa,QAIxB"}
|
|
1
|
+
{"version":3,"file":"ContentFormat.js","sourceRoot":"","sources":["../../src/content/ContentFormat.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAIX;AAJD,WAAY,aAAa;IACxB,2DAA2D;IAC3D,iBAAiB;IACjB,8BAAa,CAAA;AACd,CAAC,EAJW,aAAa,KAAb,aAAa,QAIxB;AAED,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,MAAiC,EAAW,EAAE;IAClF,IAAI,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC1B,OAAO,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AACtD,CAAC,CAAA"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
export { ContentFormat } from "#src/content/ContentFormat.js";
|
|
2
|
-
export { PatchError, PatchErrorCode } from "#src/error/PatchError.js";
|
|
3
2
|
export type { Operation } from "#src/operation/Operation.js";
|
|
4
3
|
export type { Operation_Delete } from "#src/operation/Operation_Delete.js";
|
|
5
4
|
export type { Operation_InsertAfter } from "#src/operation/Operation_InsertAfter.js";
|
|
6
5
|
export type { Operation_InsertBefore } from "#src/operation/Operation_InsertBefore.js";
|
|
7
6
|
export type { Operation_Replace } from "#src/operation/Operation_Replace.js";
|
|
8
7
|
export { OperationType } from "#src/operation/OperationType.js";
|
|
9
|
-
export {
|
|
10
|
-
export type {
|
|
8
|
+
export { Patchlet } from "#src/patchlet/Patchlet.js";
|
|
9
|
+
export type { PatchletOptions } from "#src/patchlet/Patchlet.js";
|
|
11
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,YAAY,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7D,YAAY,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,YAAY,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AACrF,YAAY,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AACvF,YAAY,EAAE,iBAAiB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AACrD,YAAY,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export { ContentFormat } from "#src/content/ContentFormat.js";
|
|
2
|
-
export { PatchError, PatchErrorCode } from "#src/error/PatchError.js";
|
|
3
2
|
export { OperationType } from "#src/operation/OperationType.js";
|
|
4
|
-
export {
|
|
3
|
+
export { Patchlet } from "#src/patchlet/Patchlet.js";
|
|
5
4
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAM9D,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OperationType.d.ts","sourceRoot":"","sources":["../../src/operation/OperationType.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACxB,OAAO,YAAY;IACnB,YAAY,iBAAiB;IAC7B,WAAW,gBAAgB;IAC3B,MAAM,WAAW;CACjB
|
|
1
|
+
{"version":3,"file":"OperationType.d.ts","sourceRoot":"","sources":["../../src/operation/OperationType.ts"],"names":[],"mappings":"AAAA,oBAAY,aAAa;IACxB,OAAO,YAAY;IACnB,YAAY,iBAAiB;IAC7B,WAAW,gBAAgB;IAC3B,MAAM,WAAW;CACjB"}
|
|
@@ -5,7 +5,4 @@ export var OperationType;
|
|
|
5
5
|
OperationType["InsertAfter"] = "insertAfter";
|
|
6
6
|
OperationType["Delete"] = "delete";
|
|
7
7
|
})(OperationType || (OperationType = {}));
|
|
8
|
-
export const isValidOperationType = (type) => {
|
|
9
|
-
return Object.values(OperationType).includes(type);
|
|
10
|
-
};
|
|
11
8
|
//# sourceMappingURL=OperationType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OperationType.js","sourceRoot":"","sources":["../../src/operation/OperationType.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAKX;AALD,WAAY,aAAa;IACxB,oCAAmB,CAAA;IACnB,8CAA6B,CAAA;IAC7B,4CAA2B,CAAA;IAC3B,kCAAiB,CAAA;AAClB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB
|
|
1
|
+
{"version":3,"file":"OperationType.js","sourceRoot":"","sources":["../../src/operation/OperationType.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,aAKX;AALD,WAAY,aAAa;IACxB,oCAAmB,CAAA;IACnB,8CAA6B,CAAA;IAC7B,4CAA2B,CAAA;IAC3B,kCAAiB,CAAA;AAClB,CAAC,EALW,aAAa,KAAb,aAAa,QAKxB"}
|
|
@@ -7,7 +7,7 @@ export type Operation_Delete = {
|
|
|
7
7
|
readonly expectedLine: ContentLineNumber;
|
|
8
8
|
readonly oldText: ContentPartial;
|
|
9
9
|
};
|
|
10
|
-
export declare const
|
|
10
|
+
export declare const Operation_Delete_Schema: z.ZodObject<{
|
|
11
11
|
operation: z.ZodLiteral<OperationType.Delete>;
|
|
12
12
|
expectedLine: z.ZodNumber;
|
|
13
13
|
oldText: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_Delete.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_Delete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC;IACzC,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_Delete.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_Delete.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,gBAAgB,GAAG;IAC9B,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC;IACzC,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,uBAAuB;;;;kBAQiE,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OperationType } from "#src/operation/OperationType.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
export const
|
|
3
|
+
export const Operation_Delete_Schema = z
|
|
4
4
|
.strictObject({
|
|
5
5
|
operation: z.literal(OperationType.Delete).describe('Must be exactly "delete" (lowercase); delete the exact oldText anchor.'),
|
|
6
6
|
expectedLine: z.number().int().min(1).describe("The exact 1-based line in the original target content where oldText must begin."),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_Delete.js","sourceRoot":"","sources":["../../src/operation/Operation_Delete.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAQxB,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_Delete.js","sourceRoot":"","sources":["../../src/operation/Operation_Delete.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAQxB,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC;KACtC,YAAY,CAAC;IACb,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,wEAAwE,CAAC;IAC7H,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,iFAAiF,CAAC;IACjI,OAAO,EAAE,CAAC;SACR,MAAM,EAAE;SACR,QAAQ,CAAC,4HAA4H,CAAC;CACxI,CAAC;KACD,QAAQ,CAAC,mDAAmD,CAAuC,CAAC"}
|
|
@@ -8,7 +8,7 @@ export type Operation_InsertAfter = {
|
|
|
8
8
|
readonly oldText: ContentPartial;
|
|
9
9
|
readonly newText: ContentPartial;
|
|
10
10
|
};
|
|
11
|
-
export declare const
|
|
11
|
+
export declare const Operation_InsertAfter_Schema: z.ZodObject<{
|
|
12
12
|
operation: z.ZodLiteral<OperationType.InsertAfter>;
|
|
13
13
|
expectedLine: z.ZodNumber;
|
|
14
14
|
oldText: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_InsertAfter.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_InsertAfter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,qBAAqB,GAAG;IACnC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC;IAC9C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_InsertAfter.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_InsertAfter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,qBAAqB,GAAG;IACnC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,WAAW,CAAC;IAC9C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,4BAA4B;;;;;kBAWkE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_InsertAfter.js","sourceRoot":"","sources":["../../src/operation/Operation_InsertAfter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_InsertAfter.js","sourceRoot":"","sources":["../../src/operation/Operation_InsertAfter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC;KAC3C,YAAY,CAAC;IACb,SAAS,EAAE,CAAC;SACV,OAAO,CAAC,aAAa,CAAC,WAAW,CAAC;SAClC,QAAQ,CAAC,4GAA4G,CAAC;IACxH,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,iFAAiF,CAAC;IACjI,OAAO,EAAE,CAAC;SACR,MAAM,EAAE;SACR,QAAQ,CAAC,4HAA4H,CAAC;IACxI,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,qDAAqD,CAAC;CACnF,CAAC;KACD,QAAQ,CAAC,oDAAoD,CAA4C,CAAC"}
|
|
@@ -8,7 +8,7 @@ export type Operation_InsertBefore = {
|
|
|
8
8
|
readonly oldText: ContentPartial;
|
|
9
9
|
readonly newText: ContentPartial;
|
|
10
10
|
};
|
|
11
|
-
export declare const
|
|
11
|
+
export declare const Operation_InsertBefore_Schema: z.ZodObject<{
|
|
12
12
|
operation: z.ZodLiteral<OperationType.InsertBefore>;
|
|
13
13
|
expectedLine: z.ZodNumber;
|
|
14
14
|
oldText: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_InsertBefore.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_InsertBefore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,sBAAsB,GAAG;IACpC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,YAAY,CAAC;IAC/C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_InsertBefore.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_InsertBefore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,sBAAsB,GAAG;IACpC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,YAAY,CAAC;IAC/C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,6BAA6B;;;;;kBAWmE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_InsertBefore.js","sourceRoot":"","sources":["../../src/operation/Operation_InsertBefore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_InsertBefore.js","sourceRoot":"","sources":["../../src/operation/Operation_InsertBefore.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC;KAC5C,YAAY,CAAC;IACb,SAAS,EAAE,CAAC;SACV,OAAO,CAAC,aAAa,CAAC,YAAY,CAAC;SACnC,QAAQ,CAAC,8GAA8G,CAAC;IAC1H,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,iFAAiF,CAAC;IACjI,OAAO,EAAE,CAAC;SACR,MAAM,EAAE;SACR,QAAQ,CAAC,4HAA4H,CAAC;IACxI,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,sDAAsD,CAAC;CACpF,CAAC;KACD,QAAQ,CAAC,qDAAqD,CAA6C,CAAC"}
|
|
@@ -8,7 +8,7 @@ export type Operation_Replace = {
|
|
|
8
8
|
readonly oldText: ContentPartial;
|
|
9
9
|
readonly newText: ContentPartial;
|
|
10
10
|
};
|
|
11
|
-
export declare const
|
|
11
|
+
export declare const Operation_Replace_Schema: z.ZodObject<{
|
|
12
12
|
operation: z.ZodLiteral<OperationType.Replace>;
|
|
13
13
|
expectedLine: z.ZodNumber;
|
|
14
14
|
oldText: z.ZodString;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_Replace.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_Replace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,iBAAiB,GAAG;IAC/B,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,OAAO,CAAC;IAC1C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_Replace.d.ts","sourceRoot":"","sources":["../../src/operation/Operation_Replace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAC3E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,MAAM,iBAAiB,GAAG;IAC/B,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,OAAO,CAAC;IAC1C,QAAQ,CAAC,YAAY,EAAE,iBAAiB,CAAC;IACzC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;IACjC,QAAQ,CAAC,OAAO,EAAE,cAAc,CAAC;CACjC,CAAC;AAEF,eAAO,MAAM,wBAAwB;;;;;kBASgE,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OperationType } from "#src/operation/OperationType.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
export const
|
|
3
|
+
export const Operation_Replace_Schema = z
|
|
4
4
|
.strictObject({
|
|
5
5
|
operation: z.literal(OperationType.Replace).describe('Must be exactly "replace" (lowercase); replace the exact oldText anchor with newText.'),
|
|
6
6
|
expectedLine: z.number().int().min(1).describe("The exact 1-based line in the original target content where oldText must begin."),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Operation_Replace.js","sourceRoot":"","sources":["../../src/operation/Operation_Replace.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"Operation_Replace.js","sourceRoot":"","sources":["../../src/operation/Operation_Replace.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AASxB,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC;KACvC,YAAY,CAAC;IACb,SAAS,EAAE,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,uFAAuF,CAAC;IAC7I,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,iFAAiF,CAAC;IACjI,OAAO,EAAE,CAAC;SACR,MAAM,EAAE;SACR,QAAQ,CAAC,4HAA4H,CAAC;IACxI,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,oDAAoD,CAAC;CAClF,CAAC;KACD,QAAQ,CAAC,kDAAkD,CAAwC,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { ContentPartial } from "#src/content/ContentPartial.js";
|
|
2
2
|
import type { ContentPosition } from "#src/content/ContentPosition.js";
|
|
3
|
-
import type {
|
|
4
|
-
export
|
|
5
|
-
readonly inputIndex:
|
|
3
|
+
import type { PatchIndex } from "#src/patch/PatchIndex.js";
|
|
4
|
+
export type Patch = {
|
|
5
|
+
readonly inputIndex: PatchIndex;
|
|
6
6
|
readonly start: ContentPosition;
|
|
7
7
|
readonly end: ContentPosition;
|
|
8
8
|
readonly replacement: ContentPartial;
|
|
9
9
|
readonly isInsertion: boolean;
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=Patch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patch.d.ts","sourceRoot":"","sources":["../../src/patch/Patch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAE3D,MAAM,MAAM,KAAK,GAAG;IACnB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,eAAe,CAAC;IAChC,QAAQ,CAAC,GAAG,EAAE,eAAe,CAAC;IAC9B,QAAQ,CAAC,WAAW,EAAE,cAAc,CAAC;IACrC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAC;CAC9B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patch.js","sourceRoot":"","sources":["../../src/patch/Patch.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PatchIndex.d.ts","sourceRoot":"","sources":["../../src/patch/PatchIndex.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PatchIndex.js","sourceRoot":"","sources":["../../src/patch/PatchIndex.ts"],"names":[],"mappings":""}
|
|
@@ -3,6 +3,6 @@ import type { Operation } from "#src/operation/Operation.js";
|
|
|
3
3
|
export declare abstract class Patcher {
|
|
4
4
|
readonly content: Content;
|
|
5
5
|
constructor(content: Content);
|
|
6
|
-
abstract
|
|
6
|
+
abstract patch(operations: readonly Operation[]): Content;
|
|
7
7
|
}
|
|
8
8
|
//# sourceMappingURL=Patcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patcher.d.ts","sourceRoot":"","sources":["../../src/patcher/Patcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAE7D,8BAAsB,OAAO;IAC5B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAE1B,YAAY,OAAO,EAAE,OAAO,EAE3B;IAED,QAAQ,CAAC,KAAK,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,GAAG,OAAO,CAAC;CAC1D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patcher.js","sourceRoot":"","sources":["../../src/patcher/Patcher.ts"],"names":[],"mappings":"AAGA,MAAM,OAAgB,OAAO;IACnB,OAAO,CAAU;IAE1B,YAAY,OAAgB;QAC3B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;CAGD"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { Content } from "#src/content/Content.js";
|
|
2
2
|
import type { Operation } from "#src/operation/Operation.js";
|
|
3
|
-
import { Patcher } from "#src/
|
|
3
|
+
import { Patcher } from "#src/patcher/Patcher.js";
|
|
4
4
|
export declare class Patcher_Text extends Patcher {
|
|
5
|
-
|
|
5
|
+
patch: (operations: readonly Operation[]) => Content;
|
|
6
6
|
private normalizeNewlines;
|
|
7
7
|
private detectNewline;
|
|
8
8
|
private buildLineStarts;
|
|
9
|
-
private
|
|
9
|
+
private createPatch;
|
|
10
10
|
private rangesOverlap;
|
|
11
11
|
private validateConflicts;
|
|
12
12
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patcher_Text.d.ts","sourceRoot":"","sources":["../../src/patcher/Patcher_Text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAGvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAI7D,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,qBAAa,YAAa,SAAQ,OAAO;IACxC,KAAK,eAAgB,SAAS,SAAS,EAAE,KAAG,OAAO,CAoBjD;IAEF,OAAO,CAAC,iBAAiB,CAEvB;IAEF,OAAO,CAAC,aAAa,CAGnB;IAEF,OAAO,CAAC,eAAe,CASrB;IAEF,OAAO,CAAC,WAAW,CA+CjB;IAEF,OAAO,CAAC,aAAa,CAEnB;IAEF,OAAO,CAAC,iBAAiB,CAyBvB;CACF"}
|
|
@@ -1,47 +1,46 @@
|
|
|
1
|
-
import { fail, PatchErrorCode } from "#src/error/PatchError.js";
|
|
2
1
|
import { OperationType } from "#src/operation/OperationType.js";
|
|
3
|
-
import { Patcher } from "#src/
|
|
2
|
+
import { Patcher } from "#src/patcher/Patcher.js";
|
|
4
3
|
export class Patcher_Text extends Patcher {
|
|
5
|
-
|
|
4
|
+
patch = (operations) => {
|
|
6
5
|
const newline = this.detectNewline();
|
|
7
6
|
const normalizedContent = this.normalizeNewlines(this.content);
|
|
8
7
|
const lineStarts = this.buildLineStarts(normalizedContent);
|
|
9
|
-
const
|
|
10
|
-
this.validateConflicts(
|
|
11
|
-
if (
|
|
8
|
+
const patches = operations.map((operation, inputIndex) => this.createPatch(operation, inputIndex, normalizedContent, lineStarts));
|
|
9
|
+
this.validateConflicts(patches);
|
|
10
|
+
if (patches.every((edit) => normalizedContent.slice(edit.start, edit.end) === edit.replacement)) {
|
|
12
11
|
return this.content;
|
|
13
12
|
}
|
|
14
|
-
const
|
|
13
|
+
const orderedPatches = [...patches].sort((left, right) => right.start - left.start || right.end - left.end || right.inputIndex - left.inputIndex);
|
|
15
14
|
let result = normalizedContent;
|
|
16
|
-
for (const edit of
|
|
15
|
+
for (const edit of orderedPatches) {
|
|
17
16
|
result = result.slice(0, edit.start) + edit.replacement + result.slice(edit.end);
|
|
18
17
|
}
|
|
19
18
|
return newline === "\r\n" ? result.replace(/\n/g, "\r\n") : result;
|
|
20
|
-
}
|
|
21
|
-
normalizeNewlines(text) {
|
|
19
|
+
};
|
|
20
|
+
normalizeNewlines = (text) => {
|
|
22
21
|
return text.replace(/\r\n/g, "\n");
|
|
23
|
-
}
|
|
24
|
-
detectNewline() {
|
|
22
|
+
};
|
|
23
|
+
detectNewline = () => {
|
|
25
24
|
const firstNewline = this.content.indexOf("\n");
|
|
26
25
|
return firstNewline > 0 && this.content[firstNewline - 1] === "\r" ? "\r\n" : "\n";
|
|
27
|
-
}
|
|
28
|
-
buildLineStarts(content) {
|
|
26
|
+
};
|
|
27
|
+
buildLineStarts = (content) => {
|
|
29
28
|
const starts = [0];
|
|
30
29
|
for (let offset = content.indexOf("\n"); offset !== -1;) {
|
|
31
30
|
starts.push(offset + 1);
|
|
32
31
|
offset = content.indexOf("\n", offset + 1);
|
|
33
32
|
}
|
|
34
33
|
return starts;
|
|
35
|
-
}
|
|
36
|
-
|
|
34
|
+
};
|
|
35
|
+
createPatch = (operation, inputIndex, content, lineStarts) => {
|
|
37
36
|
if (operation.expectedLine > lineStarts.length) {
|
|
38
|
-
|
|
37
|
+
throw new Error("expectedLine must identify a 1-based line in the original content");
|
|
39
38
|
}
|
|
40
39
|
const start = lineStarts[operation.expectedLine - 1];
|
|
41
40
|
const oldText = this.normalizeNewlines(operation.oldText);
|
|
42
41
|
const anchorEnd = start + oldText.length;
|
|
43
42
|
if (anchorEnd > content.length || content.slice(start, anchorEnd) !== oldText) {
|
|
44
|
-
|
|
43
|
+
throw new Error("oldText does not match exactly at expectedLine");
|
|
45
44
|
}
|
|
46
45
|
switch (operation.operation) {
|
|
47
46
|
case OperationType.Replace:
|
|
@@ -77,30 +76,30 @@ export class Patcher_Text extends Patcher {
|
|
|
77
76
|
isInsertion: false,
|
|
78
77
|
};
|
|
79
78
|
}
|
|
80
|
-
}
|
|
81
|
-
rangesOverlap(left, right) {
|
|
79
|
+
};
|
|
80
|
+
rangesOverlap = (left, right) => {
|
|
82
81
|
return Math.max(left.start, right.start) < Math.min(left.end, right.end);
|
|
83
|
-
}
|
|
84
|
-
validateConflicts(
|
|
85
|
-
for (let index = 0; index <
|
|
86
|
-
const current =
|
|
82
|
+
};
|
|
83
|
+
validateConflicts = (patches) => {
|
|
84
|
+
for (let index = 0; index < patches.length; index += 1) {
|
|
85
|
+
const current = patches[index];
|
|
87
86
|
for (let priorIndex = 0; priorIndex < index; priorIndex += 1) {
|
|
88
|
-
const prior =
|
|
87
|
+
const prior = patches[priorIndex];
|
|
89
88
|
const currentConsumes = current.start < current.end;
|
|
90
89
|
const priorConsumes = prior.start < prior.end;
|
|
91
90
|
if (currentConsumes && priorConsumes && this.rangesOverlap(current, prior)) {
|
|
92
|
-
|
|
91
|
+
throw new Error(`consumed range overlaps patch at index ${prior.inputIndex}`);
|
|
93
92
|
}
|
|
94
93
|
const insertion = current.isInsertion ? current : prior.isInsertion ? prior : undefined;
|
|
95
94
|
const consumed = currentConsumes ? current : priorConsumes ? prior : undefined;
|
|
96
95
|
if (insertion !== undefined && consumed !== undefined && insertion.start > consumed.start && insertion.start < consumed.end) {
|
|
97
|
-
|
|
96
|
+
throw new Error(`insertion falls inside the range consumed by patch at index ${consumed.inputIndex}`);
|
|
98
97
|
}
|
|
99
98
|
if (current.isInsertion && prior.isInsertion && current.start === prior.start) {
|
|
100
|
-
|
|
99
|
+
throw new Error(`insertion conflicts with patch at index ${prior.inputIndex}`);
|
|
101
100
|
}
|
|
102
101
|
}
|
|
103
102
|
}
|
|
104
|
-
}
|
|
103
|
+
};
|
|
105
104
|
}
|
|
106
105
|
//# sourceMappingURL=Patcher_Text.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patcher_Text.js","sourceRoot":"","sources":["../../src/patcher/Patcher_Text.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAGhE,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,MAAM,OAAO,YAAa,SAAQ,OAAO;IACxC,KAAK,GAAG,CAAC,UAAgC,EAAW,EAAE;QACrD,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QACrC,MAAM,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/D,MAAM,UAAU,GAAG,IAAI,CAAC,eAAe,CAAC,iBAAiB,CAAC,CAAC;QAC3D,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,UAAU,EAAE,EAAE,CAA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AAC,KAAK,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC;oBAC/E,MAAM,IAAI,KAAK,CAAC,2CAA2C,KAAK,CAAC,UAAU,EAAE,CAAC,CAAC;gBAChF,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC,CAAC;CACF"}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import type { Content } from "#src/content/Content.js";
|
|
2
2
|
import { ContentFormat } from "#src/content/ContentFormat.js";
|
|
3
3
|
import type { Operation } from "#src/operation/Operation.js";
|
|
4
|
+
import { Patcher } from "#src/patcher/Patcher.js";
|
|
4
5
|
import { type ZodType } from "zod";
|
|
5
|
-
export interface
|
|
6
|
+
export interface PatchletOptions {
|
|
6
7
|
readonly content: Content;
|
|
7
8
|
readonly format: ContentFormat;
|
|
8
9
|
}
|
|
9
|
-
export declare class
|
|
10
|
+
export declare class Patchlet {
|
|
10
11
|
readonly content: Content;
|
|
11
12
|
readonly format: ContentFormat;
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
readonly patcher: Patcher;
|
|
14
|
+
constructor(p: PatchletOptions);
|
|
15
|
+
static from: (content: Content, format?: ContentFormat) => Patchlet;
|
|
14
16
|
toSchema(): ZodType<Operation[]>;
|
|
15
|
-
|
|
16
|
-
private failValidation;
|
|
17
|
+
patch: (operations: readonly Operation[]) => Content;
|
|
17
18
|
}
|
|
18
|
-
//# sourceMappingURL=
|
|
19
|
+
//# sourceMappingURL=Patchlet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patchlet.d.ts","sourceRoot":"","sources":["../../src/patchlet/Patchlet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAwB,MAAM,+BAA+B,CAAC;AACpF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAK7D,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAElD,OAAO,EAAK,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAEtC,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;CAC/B;AAED,qBAAa,QAAQ;IACpB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAE1B,YAAY,CAAC,EAAE,eAAe,EAU7B;IAED,MAAM,CAAC,IAAI,YAAa,OAAO,WAAW,aAAa,cAKrD;IAEF,QAAQ,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC,CAc/B;IAED,KAAK,eAAgB,SAAS,SAAS,EAAE,KAAG,OAAO,CAIjD;CACF"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ContentFormat, isValidContentFormat } from "#src/content/ContentFormat.js";
|
|
2
|
+
import { Operation_Delete_Schema } from "#src/operation/Operation_Delete.js";
|
|
3
|
+
import { Operation_InsertAfter_Schema } from "#src/operation/Operation_InsertAfter.js";
|
|
4
|
+
import { Operation_InsertBefore_Schema } from "#src/operation/Operation_InsertBefore.js";
|
|
5
|
+
import { Operation_Replace_Schema } from "#src/operation/Operation_Replace.js";
|
|
6
|
+
import { Patcher_Text } from "#src/patcher/Patcher_Text.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
export class Patchlet {
|
|
9
|
+
content;
|
|
10
|
+
format;
|
|
11
|
+
patcher;
|
|
12
|
+
constructor(p) {
|
|
13
|
+
if (!isValidContentFormat(p.format)) {
|
|
14
|
+
throw new Error(`Patchlet.constructor.format [${p.format}]`);
|
|
15
|
+
}
|
|
16
|
+
this.content = p.content;
|
|
17
|
+
this.format = p.format;
|
|
18
|
+
// TODO: reidenzon - Choose patcher as function of ContentFormat.
|
|
19
|
+
this.patcher = new Patcher_Text(this.content);
|
|
20
|
+
}
|
|
21
|
+
static from = (content, format) => {
|
|
22
|
+
return new Patchlet({
|
|
23
|
+
content: content,
|
|
24
|
+
format: format ?? ContentFormat.Text,
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
toSchema() {
|
|
28
|
+
return z
|
|
29
|
+
.array(z.union([
|
|
30
|
+
//
|
|
31
|
+
Operation_Replace_Schema,
|
|
32
|
+
Operation_InsertBefore_Schema,
|
|
33
|
+
Operation_InsertAfter_Schema,
|
|
34
|
+
Operation_Delete_Schema,
|
|
35
|
+
]))
|
|
36
|
+
.describe("Atomically update this target only. Every anchor and conflict is validated against the original content coordinates, and any invalid patch rejects the entire list.");
|
|
37
|
+
}
|
|
38
|
+
patch = (operations) => {
|
|
39
|
+
const parsed = this.toSchema().safeParse(operations);
|
|
40
|
+
if (!parsed.success)
|
|
41
|
+
throw parsed.error;
|
|
42
|
+
return this.patcher.patch(parsed.data);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=Patchlet.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Patchlet.js","sourceRoot":"","sources":["../../src/patchlet/Patchlet.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAEpF,OAAO,EAAE,uBAAuB,EAAE,MAAM,oCAAoC,CAAC;AAC7E,OAAO,EAAE,4BAA4B,EAAE,MAAM,yCAAyC,CAAC;AACvF,OAAO,EAAE,6BAA6B,EAAE,MAAM,0CAA0C,CAAC;AACzF,OAAO,EAAE,wBAAwB,EAAE,MAAM,qCAAqC,CAAC;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAC5D,OAAO,EAAE,CAAC,EAAgB,MAAM,KAAK,CAAC;AAOtC,MAAM,OAAO,QAAQ;IACX,OAAO,CAAU;IACjB,MAAM,CAAgB;IACtB,OAAO,CAAU;IAE1B,YAAY,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