@nseng-ai/branch-context 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +48 -0
- package/src/api/index.ts +43 -0
- package/src/api/prompt-assets.ts +4 -0
- package/src/core/attach.ts +455 -0
- package/src/core/attached-plan.ts +447 -0
- package/src/core/branch-context-creation.ts +495 -0
- package/src/core/branch-memory.ts +147 -0
- package/src/core/cli.ts +98 -0
- package/src/core/constants.ts +19 -0
- package/src/core/context.ts +45 -0
- package/src/core/existing-branch-reuse.ts +230 -0
- package/src/core/index.ts +47 -0
- package/src/core/operations.ts +590 -0
- package/src/core/plan-content-slug.ts +50 -0
- package/src/core/prompt-assets.ts +14 -0
- package/src/core/prompts/branch-context-impl.md +34 -0
- package/src/core/session-artifact.ts +114 -0
- package/src/ns/command.ts +33 -0
- package/src/ns/commands/attach.ts +19 -0
- package/src/ns/commands/check.ts +18 -0
- package/src/ns/commands/delete.ts +18 -0
- package/src/ns/commands/from-plan.ts +21 -0
- package/src/ns/commands/list.ts +17 -0
- package/src/ns/commands/load.ts +19 -0
- package/src/ns/repo-local-ns-extension.ts +32 -0
- package/src/pi/enriched-plan-save.ts +551 -0
- package/src/pi/extension.ts +154 -0
- package/src/pi/from-plan-commands.ts +1243 -0
- package/src/pi/gt/upstack-impl-launch.ts +154 -0
- package/src/pi/host-types.ts +135 -0
- package/src/pi/index.ts +16 -0
- package/src/pi/options.ts +32 -0
- package/src/pi/surfaces.ts +11 -0
- package/src/testing/index.ts +258 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
import { isRecord } from "@nseng-ai/foundation/primitives";
|
|
4
|
+
|
|
5
|
+
import { BRANCH_CREATION_METHODS, type BranchContextEvidence } from "./branch-context-creation.ts";
|
|
6
|
+
import { BRANCH_CONTEXT_NAMESPACE } from "./constants.ts";
|
|
7
|
+
|
|
8
|
+
export const BRANCH_CONTEXT_OUTPUT_MESSAGE_TYPE = "branch-context-output";
|
|
9
|
+
|
|
10
|
+
export type BranchContextOutputDetails =
|
|
11
|
+
| { status: "usage" }
|
|
12
|
+
| { status: "dry-run"; targetBranch: string; key: string }
|
|
13
|
+
| { status: "success"; evidence: BranchContextEvidence }
|
|
14
|
+
| { status: "loaded-plan" }
|
|
15
|
+
| { status: "reuse" }
|
|
16
|
+
| { status: "failure"; error: string }
|
|
17
|
+
| { status: "cancelled" };
|
|
18
|
+
|
|
19
|
+
export interface BranchContextOutputMessage {
|
|
20
|
+
customType: typeof BRANCH_CONTEXT_OUTPUT_MESSAGE_TYPE;
|
|
21
|
+
content: string;
|
|
22
|
+
display: true;
|
|
23
|
+
details: BranchContextOutputDetails;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const nonEmptyEvidenceStringSchema = z.string().min(1);
|
|
27
|
+
|
|
28
|
+
const branchContextEvidenceSchema = z.object({
|
|
29
|
+
slug: nonEmptyEvidenceStringSchema,
|
|
30
|
+
branch: nonEmptyEvidenceStringSchema,
|
|
31
|
+
branchCreation: z.enum(BRANCH_CREATION_METHODS),
|
|
32
|
+
startPoint: nonEmptyEvidenceStringSchema,
|
|
33
|
+
namespace: nonEmptyEvidenceStringSchema,
|
|
34
|
+
key: nonEmptyEvidenceStringSchema,
|
|
35
|
+
refName: nonEmptyEvidenceStringSchema,
|
|
36
|
+
commit: nonEmptyEvidenceStringSchema,
|
|
37
|
+
sourceFile: nonEmptyEvidenceStringSchema,
|
|
38
|
+
summary: z.string().optional(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const successfulBranchContextOutputDetailsSchema = z.object({
|
|
42
|
+
status: z.literal("success"),
|
|
43
|
+
evidence: branchContextEvidenceSchema,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
export function buildBranchContextOutputMessage(
|
|
47
|
+
content: string,
|
|
48
|
+
details: BranchContextOutputDetails,
|
|
49
|
+
): BranchContextOutputMessage {
|
|
50
|
+
return {
|
|
51
|
+
customType: BRANCH_CONTEXT_OUTPUT_MESSAGE_TYPE,
|
|
52
|
+
content,
|
|
53
|
+
display: true,
|
|
54
|
+
details,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function extractBranchContextEvidence(details: unknown): BranchContextEvidence | undefined {
|
|
59
|
+
const result = successfulBranchContextOutputDetailsSchema.safeParse(details);
|
|
60
|
+
if (!result.success) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return toBranchContextEvidence(result.data.evidence);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function toBranchContextEvidence(
|
|
68
|
+
data: z.infer<typeof branchContextEvidenceSchema>,
|
|
69
|
+
): BranchContextEvidence {
|
|
70
|
+
const { summary, ...evidence } = data;
|
|
71
|
+
return { ...evidence, ...(summary === undefined ? {} : { summary }) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function extractBranchContextEvidenceFromSessionEntry(
|
|
75
|
+
entry: unknown,
|
|
76
|
+
): BranchContextEvidence | undefined {
|
|
77
|
+
const message = extractMessageFromEntry(entry);
|
|
78
|
+
if (
|
|
79
|
+
message === undefined ||
|
|
80
|
+
customTypeFromMessage(message) !== BRANCH_CONTEXT_OUTPUT_MESSAGE_TYPE
|
|
81
|
+
) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
return extractBranchContextEvidence(message.details);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function findLatestBranchContextEvidence(
|
|
88
|
+
entries: readonly unknown[],
|
|
89
|
+
): BranchContextEvidence | undefined {
|
|
90
|
+
for (let index = entries.length - 1; index >= 0; index -= 1) {
|
|
91
|
+
const evidence = extractBranchContextEvidenceFromSessionEntry(entries[index]);
|
|
92
|
+
if (evidence !== undefined && evidence.namespace === BRANCH_CONTEXT_NAMESPACE) {
|
|
93
|
+
return evidence;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function extractMessageFromEntry(entry: unknown): Record<string, unknown> | undefined {
|
|
100
|
+
if (!isRecord(entry)) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
if (isRecord(entry.message)) {
|
|
104
|
+
return entry.message;
|
|
105
|
+
}
|
|
106
|
+
if (typeof entry.customType === "string" || entry.content !== undefined) {
|
|
107
|
+
return entry;
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function customTypeFromMessage(message: Record<string, unknown>): string | undefined {
|
|
113
|
+
return typeof message.customType === "string" ? message.customType : undefined;
|
|
114
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createNsDomainCommand,
|
|
3
|
+
type NsDomainCommandOptions,
|
|
4
|
+
} from "@nseng-ai/capability-kit/ns-command";
|
|
5
|
+
import { optionalEntry } from "@nseng-ai/foundation/primitives";
|
|
6
|
+
import type { NsCommand, NsCommandSchema, NsExtensionApi } from "@nseng-ai/ns/kernel/sdk";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
createRealBranchContextCliContext,
|
|
10
|
+
type BranchContextCliContext,
|
|
11
|
+
} from "../core/operations.ts";
|
|
12
|
+
|
|
13
|
+
type BranchContextNsCommandOptions<S extends NsCommandSchema, T> = Omit<
|
|
14
|
+
NsDomainCommandOptions<S, T, BranchContextCliContext>,
|
|
15
|
+
"createContext"
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
export function branchContextCommand<S extends NsCommandSchema, T>(
|
|
19
|
+
options: BranchContextNsCommandOptions<S, T>,
|
|
20
|
+
): NsCommand<S, T> {
|
|
21
|
+
return createNsDomainCommand({
|
|
22
|
+
...options,
|
|
23
|
+
createContext: createBranchContextExtensionContext,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function createBranchContextExtensionContext(ctx: NsExtensionApi): BranchContextCliContext {
|
|
28
|
+
return createRealBranchContextCliContext({
|
|
29
|
+
cwd: ctx.cwd,
|
|
30
|
+
env: ctx.env,
|
|
31
|
+
...optionalEntry("stderr", ctx.stderr),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import { attachRequestSchema, attachResultSchema, handleAttach } from "../../core/operations.ts";
|
|
5
|
+
|
|
6
|
+
export const branchContextAttachNsCommand = branchContextCommand({
|
|
7
|
+
name: "attach",
|
|
8
|
+
summary: "Attach a saved plan or file as branch context.",
|
|
9
|
+
description:
|
|
10
|
+
"Attach a saved plan or arbitrary file to the current or selected branch context namespace.",
|
|
11
|
+
schema: attachRequestSchema,
|
|
12
|
+
resultSchema: attachResultSchema,
|
|
13
|
+
positionals: { key: { position: 0 } },
|
|
14
|
+
handler: handleAttach,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export default defineExtension({
|
|
18
|
+
commands: [branchContextAttachNsCommand],
|
|
19
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import { checkResultSchema, handleCheck, keyRequestSchema } from "../../core/operations.ts";
|
|
5
|
+
|
|
6
|
+
export const branchContextCheckNsCommand = branchContextCommand({
|
|
7
|
+
name: "check",
|
|
8
|
+
summary: "Check a branch-context entry.",
|
|
9
|
+
description: "Check whether a branch-context entry exists on the current or selected branch.",
|
|
10
|
+
schema: keyRequestSchema,
|
|
11
|
+
resultSchema: checkResultSchema,
|
|
12
|
+
positionals: { key: { position: 0 } },
|
|
13
|
+
handler: handleCheck,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export default defineExtension({
|
|
17
|
+
commands: [branchContextCheckNsCommand],
|
|
18
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import { deleteResultSchema, handleDelete, keyRequestSchema } from "../../core/operations.ts";
|
|
5
|
+
|
|
6
|
+
export const branchContextDeleteNsCommand = branchContextCommand({
|
|
7
|
+
name: "delete",
|
|
8
|
+
summary: "Delete a branch-context entry.",
|
|
9
|
+
description: "Delete a branch-context entry from the current or selected branch.",
|
|
10
|
+
schema: keyRequestSchema,
|
|
11
|
+
resultSchema: deleteResultSchema,
|
|
12
|
+
positionals: { key: { position: 0 } },
|
|
13
|
+
handler: handleDelete,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export default defineExtension({
|
|
17
|
+
commands: [branchContextDeleteNsCommand],
|
|
18
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import {
|
|
5
|
+
branchContextResultSchema,
|
|
6
|
+
createRequestSchema,
|
|
7
|
+
handleCreate,
|
|
8
|
+
} from "../../core/operations.ts";
|
|
9
|
+
|
|
10
|
+
export const branchContextFromPlanNsCommand = branchContextCommand({
|
|
11
|
+
name: "from-plan",
|
|
12
|
+
summary: "Create branch context from a saved plan.",
|
|
13
|
+
description: "Create a branch context entry from a saved plan file for agent implementation.",
|
|
14
|
+
schema: createRequestSchema,
|
|
15
|
+
resultSchema: branchContextResultSchema,
|
|
16
|
+
handler: handleCreate,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
export default defineExtension({
|
|
20
|
+
commands: [branchContextFromPlanNsCommand],
|
|
21
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import { handleList, listRequestSchema, listResultSchema } from "../../core/operations.ts";
|
|
5
|
+
|
|
6
|
+
export const branchContextListNsCommand = branchContextCommand({
|
|
7
|
+
name: "list",
|
|
8
|
+
summary: "List branch-context entries.",
|
|
9
|
+
description: "List branch-context entries attached to the current or selected branch.",
|
|
10
|
+
schema: listRequestSchema,
|
|
11
|
+
resultSchema: listResultSchema,
|
|
12
|
+
handler: handleList,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export default defineExtension({
|
|
16
|
+
commands: [branchContextListNsCommand],
|
|
17
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineExtension } from "@nseng-ai/ns/kernel/sdk";
|
|
2
|
+
|
|
3
|
+
import { branchContextCommand } from "../command.ts";
|
|
4
|
+
import { handleLoad, loadPlanResultSchema, loadRequestSchema } from "../../core/operations.ts";
|
|
5
|
+
|
|
6
|
+
export const branchContextLoadNsCommand = branchContextCommand({
|
|
7
|
+
name: "load",
|
|
8
|
+
summary: "Load an attached branch-context plan.",
|
|
9
|
+
description:
|
|
10
|
+
"Load a branch-context entry and render the implementation prompt for agent invocation.",
|
|
11
|
+
schema: loadRequestSchema,
|
|
12
|
+
resultSchema: loadPlanResultSchema,
|
|
13
|
+
positionals: { key: { position: 0 } },
|
|
14
|
+
handler: handleLoad,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export default defineExtension({
|
|
18
|
+
commands: [branchContextLoadNsCommand],
|
|
19
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineRepoLocalNsExtensionDescriptor,
|
|
3
|
+
repoLocalNsCommandDescriptor,
|
|
4
|
+
} from "@nseng-ai/ns/kernel/sdk";
|
|
5
|
+
|
|
6
|
+
import { branchContextAttachNsCommand } from "./commands/attach.ts";
|
|
7
|
+
import { branchContextCheckNsCommand } from "./commands/check.ts";
|
|
8
|
+
import { branchContextDeleteNsCommand } from "./commands/delete.ts";
|
|
9
|
+
import { branchContextFromPlanNsCommand } from "./commands/from-plan.ts";
|
|
10
|
+
import { branchContextListNsCommand } from "./commands/list.ts";
|
|
11
|
+
import { branchContextLoadNsCommand } from "./commands/load.ts";
|
|
12
|
+
|
|
13
|
+
const BRANCH_CONTEXT_COMMANDS = [
|
|
14
|
+
branchContextFromPlanNsCommand,
|
|
15
|
+
branchContextLoadNsCommand,
|
|
16
|
+
branchContextAttachNsCommand,
|
|
17
|
+
branchContextListNsCommand,
|
|
18
|
+
branchContextCheckNsCommand,
|
|
19
|
+
branchContextDeleteNsCommand,
|
|
20
|
+
] as const;
|
|
21
|
+
|
|
22
|
+
export const branchContextRepoLocalNsExtension = defineRepoLocalNsExtensionDescriptor({
|
|
23
|
+
group: "branch-context",
|
|
24
|
+
description: "Create and load branch-scoped implementation context.",
|
|
25
|
+
commands: BRANCH_CONTEXT_COMMANDS.map((command) =>
|
|
26
|
+
repoLocalNsCommandDescriptor({
|
|
27
|
+
command,
|
|
28
|
+
manifestPath: ["exec", command.name],
|
|
29
|
+
packageExportPrefix: "@nseng-ai/branch-context/ns/commands",
|
|
30
|
+
}),
|
|
31
|
+
),
|
|
32
|
+
});
|