@howaboua/pi-codex-web-run 0.0.10 → 0.0.11
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 +9 -0
- package/index.ts +4 -7
- package/package.json +79 -81
- package/src/codex-runtime/resolve.ts +3 -3
- package/src/contract.ts +31 -45
- package/src/execute.ts +6 -9
- package/src/tool.ts +32 -15
- package/biome.json +0 -31
- package/changelog.ts +0 -337
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @howaboua/pi-codex-web-run
|
|
2
2
|
|
|
3
|
+
## 0.0.11
|
|
4
|
+
|
|
5
|
+
- Requires Pi 1.5 or newer.
|
|
6
|
+
|
|
7
|
+
- Removed custom changelog suppression. Updates use Pi's native notices and `collapseChangelog`.
|
|
8
|
+
- Replaced the TypeBox dependency with Pi's native schema builders.
|
|
9
|
+
|
|
10
|
+
Web search examples no longer prescribe `await`.
|
|
11
|
+
|
|
3
12
|
## 0.0.10
|
|
4
13
|
|
|
5
14
|
- Added an extension API for Web Run search and navigation, using the existing provider and login without adding conversation messages.
|
package/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import registerPackageChangelog from "./changelog.js";
|
|
3
2
|
import { webRunCodeModeResult } from "./src/code-mode.js";
|
|
4
3
|
import { resolveHostedCodexToolProvider } from "./src/codex-runtime/policy.js";
|
|
5
4
|
import type { WebSearchToolOptions } from "./src/contract.js";
|
|
@@ -20,7 +19,6 @@ export {
|
|
|
20
19
|
export { createWebSearchTool } from "./src/tool.js";
|
|
21
20
|
|
|
22
21
|
export default async function webRunExtension(pi: ExtensionAPI): Promise<void> {
|
|
23
|
-
registerPackageChangelog(pi);
|
|
24
22
|
const options: WebSearchToolOptions = {
|
|
25
23
|
allowCodexProviderFallback: true,
|
|
26
24
|
resolveProvider: (ctx) => resolveHostedCodexToolProvider(pi, ctx),
|
|
@@ -35,9 +33,8 @@ export default async function webRunExtension(pi: ExtensionAPI): Promise<void> {
|
|
|
35
33
|
guidelines: tool.promptGuidelines,
|
|
36
34
|
};
|
|
37
35
|
try {
|
|
38
|
-
const { registerGptPromptMode } =
|
|
39
|
-
"@howaboua/pi-codex-conversion/prompt-mode"
|
|
40
|
-
);
|
|
36
|
+
const { registerGptPromptMode } =
|
|
37
|
+
await import("@howaboua/pi-codex-conversion/prompt-mode");
|
|
41
38
|
registerGptPromptMode(pi, (active) => {
|
|
42
39
|
if (gpt !== active) {
|
|
43
40
|
gpt = active;
|
|
@@ -85,8 +82,8 @@ async function registerWebRunInCodeMode(
|
|
|
85
82
|
return registerCodeModeExtensionTools(pi, () => [
|
|
86
83
|
adaptToolForCodeMode(tool, {
|
|
87
84
|
usage: gpt()
|
|
88
|
-
? '
|
|
89
|
-
: '
|
|
85
|
+
? 'tools.web__run({search_query?:[{q:string,recency?:number,domains?:string[]}],image_query?:[{q:string}],open?:[{ref_id:string,lineno?:number}],click?:[{ref_id:string,id:number}],find?:[{ref_id:string,pattern:string}],response_length?:"short"|"medium"|"long"}) // calls=returned ref_ids; final citations=Markdown result URLs; never refs/cite markers'
|
|
86
|
+
: 'tools.web__run({search_query?:[{q:string,recency?:number,domains?:string[]}],image_query?:[{q:string}],open?:[{ref_id:string,lineno?:number}],click?:[{ref_id:string,id:number}],find?:[{ref_id:string,pattern:string}],response_length?:"short"|"medium"|"long"}) // calls use returned ref_ids; final answers use Markdown result URLs, never refs/cite markers',
|
|
90
87
|
promptMetadata: false,
|
|
91
88
|
toolName: { namespace: "web", name: "run" },
|
|
92
89
|
resultValue: webRunCodeModeResult,
|
package/package.json
CHANGED
|
@@ -1,83 +1,81 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
2
|
+
"name": "@howaboua/pi-codex-web-run",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "Codex web search and navigation for Pi, Code Mode and Notebook Mode.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Igor Warzocha",
|
|
8
|
+
"homepage": "https://github.com/IgorWarzocha/howaboua-pi-stuff/tree/main/packages/pi-codex-web-run#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/IgorWarzocha/howaboua-pi-stuff.git",
|
|
12
|
+
"directory": "packages/pi-codex-web-run"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/IgorWarzocha/howaboua-pi-stuff/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"pi-package",
|
|
19
|
+
"pi-extension",
|
|
20
|
+
"pi",
|
|
21
|
+
"codex",
|
|
22
|
+
"web-search",
|
|
23
|
+
"code-mode"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"index.ts",
|
|
27
|
+
"src",
|
|
28
|
+
"upstream",
|
|
29
|
+
"README.md",
|
|
30
|
+
"UPSTREAM_SYNC.md",
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"CHANGELOG.md",
|
|
33
|
+
"tsconfig.json"
|
|
34
|
+
],
|
|
35
|
+
"scripts": {
|
|
36
|
+
"check": "bun run lint && bun run typecheck && bun run test",
|
|
37
|
+
"prepack": "bun run check",
|
|
38
|
+
"typecheck": "tsrs --noEmit",
|
|
39
|
+
"lint": "bun run format:check && oxlint --type-aware",
|
|
40
|
+
"test": "tsx --test tests/**/*.test.ts",
|
|
41
|
+
"pack:dry": "npm pack --dry-run",
|
|
42
|
+
"format:check": "oxfmt --check '**/*.{js,cjs,mjs,jsx,ts,cts,mts,tsx,json,jsonc,json5,css}'"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"proxy-from-env": "2.1.0",
|
|
46
|
+
"undici": "8.12.4",
|
|
47
|
+
"@earendil-works/pi-schema": "1.5.2"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": ">=1.5.0",
|
|
51
|
+
"@earendil-works/pi-tui": ">=1.0.0",
|
|
52
|
+
"@howaboua/pi-codex-conversion": ">=5.0.1"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
56
|
+
"@earendil-works/pi-tui": "1.5.2",
|
|
57
|
+
"@types/node": "^26.6.6",
|
|
58
|
+
"tsx": "^4.23.15",
|
|
59
|
+
"typescript": "^7.0.2",
|
|
60
|
+
"@howaboua/pi-codex-conversion": "5.0.1",
|
|
61
|
+
"oxfmt": "^0.78.0"
|
|
62
|
+
},
|
|
63
|
+
"pi": {
|
|
64
|
+
"extensions": [
|
|
65
|
+
"./index.ts"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=22.19.0"
|
|
70
|
+
},
|
|
71
|
+
"main": "index.ts",
|
|
72
|
+
"exports": "./index.ts",
|
|
73
|
+
"publishConfig": {
|
|
74
|
+
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"@howaboua/pi-codex-conversion": {
|
|
78
|
+
"optional": true
|
|
79
|
+
}
|
|
80
|
+
}
|
|
83
81
|
}
|
|
@@ -36,9 +36,9 @@ function isCodexTransportModel(
|
|
|
36
36
|
): boolean {
|
|
37
37
|
return Boolean(
|
|
38
38
|
model &&
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
(isOpenAICodexModel(model) ||
|
|
40
|
+
(model.api ?? "").trim().toLowerCase() === "openai-codex-responses" ||
|
|
41
|
+
isConfiguredCodexTransport?.(model as ExtensionContext["model"])),
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
44
|
|
package/src/contract.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
2
|
+
import { arr, lit, num, obj, opt, str, union } from "@earendil-works/pi-schema";
|
|
3
3
|
import type { CodexToolProvider } from "./codex-runtime/types.js";
|
|
4
4
|
|
|
5
5
|
export const WEB_SEARCH_TOOL_NAME = "web_run";
|
|
@@ -7,70 +7,56 @@ export const DEFAULT_WEB_SEARCH_MODEL = "gpt-6-luna";
|
|
|
7
7
|
export const WEB_SEARCH_MAX_OUTPUT_TOKENS = 2_500;
|
|
8
8
|
export const WEB_SEARCH_MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
|
|
9
9
|
|
|
10
|
-
const SearchQueryParameters =
|
|
10
|
+
const SearchQueryParameters = obj(
|
|
11
11
|
{
|
|
12
|
-
q:
|
|
13
|
-
recency:
|
|
14
|
-
domains:
|
|
12
|
+
q: str(),
|
|
13
|
+
recency: opt(num({ description: "Days" })),
|
|
14
|
+
domains: opt(arr(str())),
|
|
15
15
|
},
|
|
16
|
-
{
|
|
16
|
+
{ open: true },
|
|
17
17
|
);
|
|
18
18
|
|
|
19
|
-
export const WEB_SEARCH_PARAMETERS =
|
|
19
|
+
export const WEB_SEARCH_PARAMETERS = obj(
|
|
20
20
|
{
|
|
21
|
-
search_query:
|
|
22
|
-
image_query:
|
|
23
|
-
open:
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
search_query: opt(arr(SearchQueryParameters)),
|
|
22
|
+
image_query: opt(arr(SearchQueryParameters)),
|
|
23
|
+
open: opt(
|
|
24
|
+
arr(
|
|
25
|
+
obj(
|
|
26
26
|
{
|
|
27
|
-
ref_id:
|
|
28
|
-
lineno:
|
|
27
|
+
ref_id: str(),
|
|
28
|
+
lineno: opt(num()),
|
|
29
29
|
},
|
|
30
|
-
{
|
|
30
|
+
{ open: true },
|
|
31
31
|
),
|
|
32
32
|
{ description: "ref_id or URL" },
|
|
33
33
|
),
|
|
34
34
|
),
|
|
35
|
-
click:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
),
|
|
41
|
-
|
|
42
|
-
),
|
|
43
|
-
find: Type.Optional(
|
|
44
|
-
Type.Array(
|
|
45
|
-
Type.Object(
|
|
46
|
-
{ ref_id: Type.String(), pattern: Type.String() },
|
|
47
|
-
{ additionalProperties: true },
|
|
48
|
-
),
|
|
49
|
-
),
|
|
50
|
-
),
|
|
51
|
-
response_length: Type.Optional(
|
|
52
|
-
Type.Union([
|
|
53
|
-
Type.Literal("short"),
|
|
54
|
-
Type.Literal("medium"),
|
|
55
|
-
Type.Literal("long"),
|
|
35
|
+
click: opt(arr(obj({ ref_id: str(), id: num() }, { open: true }))),
|
|
36
|
+
find: opt(arr(obj({ ref_id: str(), pattern: str() }, { open: true }))),
|
|
37
|
+
response_length: opt(
|
|
38
|
+
union([
|
|
39
|
+
{ ...lit("short"), type: "string" },
|
|
40
|
+
{ ...lit("medium"), type: "string" },
|
|
41
|
+
{ ...lit("long"), type: "string" },
|
|
56
42
|
]),
|
|
57
43
|
),
|
|
58
|
-
settings:
|
|
59
|
-
|
|
44
|
+
settings: opt(
|
|
45
|
+
obj(
|
|
60
46
|
{
|
|
61
|
-
search_context_size:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
47
|
+
search_context_size: opt(
|
|
48
|
+
union([
|
|
49
|
+
{ ...lit("low"), type: "string" },
|
|
50
|
+
{ ...lit("medium"), type: "string" },
|
|
51
|
+
{ ...lit("high"), type: "string" },
|
|
66
52
|
]),
|
|
67
53
|
),
|
|
68
54
|
},
|
|
69
|
-
{
|
|
55
|
+
{ open: true },
|
|
70
56
|
),
|
|
71
57
|
),
|
|
72
58
|
},
|
|
73
|
-
{
|
|
59
|
+
{ open: true },
|
|
74
60
|
);
|
|
75
61
|
|
|
76
62
|
export type WebRunOutput = Record<string, unknown> & {
|
package/src/execute.ts
CHANGED
|
@@ -53,9 +53,8 @@ async function resolveProvider(
|
|
|
53
53
|
const isConfiguredCodexTransport = (model: ExtensionContext["model"]) =>
|
|
54
54
|
isCodexToolRoute(config, model);
|
|
55
55
|
if (isConfiguredCodexTransport(ctx.model)) {
|
|
56
|
-
const { resolveCodexToolProvider } =
|
|
57
|
-
"./codex-runtime/resolve.js"
|
|
58
|
-
);
|
|
56
|
+
const { resolveCodexToolProvider } =
|
|
57
|
+
await import("./codex-runtime/resolve.js");
|
|
59
58
|
return resolveCodexToolProvider(
|
|
60
59
|
ctx,
|
|
61
60
|
options.allowConfiguredProvider,
|
|
@@ -75,9 +74,8 @@ async function resolveProvider(
|
|
|
75
74
|
throw codexProviderFailure(error, legacy);
|
|
76
75
|
}
|
|
77
76
|
if (hosted) return hosted;
|
|
78
|
-
const { resolveCodexToolProvider } =
|
|
79
|
-
"./codex-runtime/resolve.js"
|
|
80
|
-
);
|
|
77
|
+
const { resolveCodexToolProvider } =
|
|
78
|
+
await import("./codex-runtime/resolve.js");
|
|
81
79
|
return resolveCodexToolProvider(
|
|
82
80
|
ctx,
|
|
83
81
|
options.allowConfiguredProvider,
|
|
@@ -91,9 +89,8 @@ export async function executeCodexWebSearch(
|
|
|
91
89
|
signal: AbortSignal | undefined | null,
|
|
92
90
|
options: WebSearchToolOptions = {},
|
|
93
91
|
): Promise<WebRunExecutionResult> {
|
|
94
|
-
const { readCodexToolRouteConfig, resolveCodexToolModel } =
|
|
95
|
-
"./codex-runtime/config.js"
|
|
96
|
-
);
|
|
92
|
+
const { readCodexToolRouteConfig, resolveCodexToolModel } =
|
|
93
|
+
await import("./codex-runtime/config.js");
|
|
97
94
|
const routeConfig = readCodexToolRouteConfig();
|
|
98
95
|
const provider = await resolveProvider(ctx, options, routeConfig);
|
|
99
96
|
const [{ codexToolProviderHeaders }, { fetchCodexTool }] = await Promise.all([
|
package/src/tool.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import type { Static } from "@earendil-works/pi-schema";
|
|
3
4
|
import { Container, Text } from "@earendil-works/pi-tui";
|
|
4
5
|
import { codexLoginDiagnostic } from "./codex-runtime/auth-diagnostics.js";
|
|
5
6
|
import {
|
|
@@ -19,18 +20,28 @@ function firstString(value: unknown, key: string): string | undefined {
|
|
|
19
20
|
return typeof field === "string" && field.trim() ? field.trim() : undefined;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
function webSearchCallDetail(
|
|
23
|
-
params
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const image =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
23
|
+
function webSearchCallDetail(params: unknown): string | undefined {
|
|
24
|
+
if (!params || typeof params !== "object") return undefined;
|
|
25
|
+
const search =
|
|
26
|
+
"search_query" in params && Array.isArray(params["search_query"])
|
|
27
|
+
? params["search_query"][0]
|
|
28
|
+
: undefined;
|
|
29
|
+
const image =
|
|
30
|
+
"image_query" in params && Array.isArray(params["image_query"])
|
|
31
|
+
? params["image_query"][0]
|
|
32
|
+
: undefined;
|
|
33
|
+
const open =
|
|
34
|
+
"open" in params && Array.isArray(params["open"])
|
|
35
|
+
? params["open"][0]
|
|
36
|
+
: undefined;
|
|
37
|
+
const click =
|
|
38
|
+
"click" in params && Array.isArray(params["click"])
|
|
39
|
+
? params["click"][0]
|
|
40
|
+
: undefined;
|
|
41
|
+
const find =
|
|
42
|
+
"find" in params && Array.isArray(params["find"])
|
|
43
|
+
? params["find"][0]
|
|
44
|
+
: undefined;
|
|
34
45
|
const query = firstString(search, "q") ?? firstString(image, "q");
|
|
35
46
|
if (query) return query;
|
|
36
47
|
const opened =
|
|
@@ -55,12 +66,18 @@ export function createWebSearchTool(
|
|
|
55
66
|
? {}
|
|
56
67
|
: { promptSnippet: "Explicit args" }),
|
|
57
68
|
promptGuidelines: [
|
|
58
|
-
"web_run: Calls use returned turn refs; final answers use Markdown result URLs, never refs or cite
|
|
69
|
+
"web_run: Calls use returned turn refs; final answers use Markdown result URLs, never refs or cite…",
|
|
59
70
|
],
|
|
60
71
|
parameters: WEB_SEARCH_PARAMETERS,
|
|
61
72
|
prepareArguments: (args) =>
|
|
62
73
|
args && typeof args === "object" ? (args as Record<string, unknown>) : {},
|
|
63
|
-
async execute(
|
|
74
|
+
async execute(
|
|
75
|
+
_toolCallId,
|
|
76
|
+
params: Static<typeof WEB_SEARCH_PARAMETERS>,
|
|
77
|
+
signal,
|
|
78
|
+
_onUpdate,
|
|
79
|
+
ctx,
|
|
80
|
+
) {
|
|
64
81
|
if (!supportsExecutableWebSearch(ctx.model, toolOptions))
|
|
65
82
|
throw new Error(codexLoginDiagnostic());
|
|
66
83
|
const output = await executeCodexWebSearch(
|
|
@@ -80,7 +97,7 @@ export function createWebSearchTool(
|
|
|
80
97
|
renderCall(args, theme) {
|
|
81
98
|
return renderToolCell(
|
|
82
99
|
"Searched the web",
|
|
83
|
-
webSearchCallDetail(args
|
|
100
|
+
webSearchCallDetail(args),
|
|
84
101
|
theme,
|
|
85
102
|
);
|
|
86
103
|
},
|
package/biome.json
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://biomejs.dev/schemas/2.6.3/schema.json",
|
|
3
|
-
"vcs": {
|
|
4
|
-
"enabled": true,
|
|
5
|
-
"clientKind": "git",
|
|
6
|
-
"useIgnoreFile": false
|
|
7
|
-
},
|
|
8
|
-
"files": {
|
|
9
|
-
"includes": [
|
|
10
|
-
"**",
|
|
11
|
-
"!node_modules",
|
|
12
|
-
"!dist",
|
|
13
|
-
"!coverage",
|
|
14
|
-
"!package-lock.json"
|
|
15
|
-
]
|
|
16
|
-
},
|
|
17
|
-
"formatter": {
|
|
18
|
-
"enabled": true,
|
|
19
|
-
"indentStyle": "tab"
|
|
20
|
-
},
|
|
21
|
-
"linter": {
|
|
22
|
-
"enabled": false
|
|
23
|
-
},
|
|
24
|
-
"javascript": {
|
|
25
|
-
"formatter": {
|
|
26
|
-
"quoteStyle": "double",
|
|
27
|
-
"semicolons": "always",
|
|
28
|
-
"trailingCommas": "all"
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
package/changelog.ts
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
// Generated extension packages copy this file during changelog:extensions.
|
|
2
|
-
|
|
3
|
-
import { randomUUID } from "node:crypto";
|
|
4
|
-
import { readFileSync } from "node:fs";
|
|
5
|
-
import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
6
|
-
import { dirname, join } from "node:path";
|
|
7
|
-
import { fileURLToPath } from "node:url";
|
|
8
|
-
import {
|
|
9
|
-
DynamicBorder,
|
|
10
|
-
type ExtensionAPI,
|
|
11
|
-
getAgentDir,
|
|
12
|
-
getMarkdownTheme,
|
|
13
|
-
} from "@earendil-works/pi-coding-agent";
|
|
14
|
-
import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
15
|
-
|
|
16
|
-
const ENTRY_TYPE = "@howaboua/pi-stuff/changelog";
|
|
17
|
-
const REGISTRATION_CHANNEL = "@howaboua/pi-stuff/changelog/register/v1";
|
|
18
|
-
const STATE_FILENAME = "howaboua-pi-stuff-changelog.json";
|
|
19
|
-
const SUPPRESS_KEY = "suppress";
|
|
20
|
-
const LOCK_STALE_MS = 30_000;
|
|
21
|
-
const LOCK_TIMEOUT_MS = 2_000;
|
|
22
|
-
|
|
23
|
-
interface PackageMetadata {
|
|
24
|
-
name: string;
|
|
25
|
-
version: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface PackageRegistration extends PackageMetadata {
|
|
29
|
-
changelogPath: string;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface RegistrationEvent {
|
|
33
|
-
accept: () => void;
|
|
34
|
-
registration: PackageRegistration;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface ChangelogEntry {
|
|
38
|
-
content: string;
|
|
39
|
-
version: string;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
interface ChangelogEntryData {
|
|
43
|
-
markdown: string;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
type ChangelogState = Record<string, boolean | string>;
|
|
47
|
-
|
|
48
|
-
function packageRegistration(): PackageRegistration {
|
|
49
|
-
const packageDirectory = fileURLToPath(new URL(".", import.meta.url));
|
|
50
|
-
const metadata = JSON.parse(
|
|
51
|
-
readFileSync(join(packageDirectory, "package.json"), "utf8"),
|
|
52
|
-
) as Partial<PackageMetadata>;
|
|
53
|
-
if (typeof metadata.name !== "string" || typeof metadata.version !== "string")
|
|
54
|
-
throw new Error(`Invalid package metadata in ${packageDirectory}`);
|
|
55
|
-
return {
|
|
56
|
-
name: metadata.name,
|
|
57
|
-
version: metadata.version,
|
|
58
|
-
changelogPath: join(packageDirectory, "CHANGELOG.md"),
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function isRegistrationEvent(value: unknown): value is RegistrationEvent {
|
|
63
|
-
if (!value || typeof value !== "object") return false;
|
|
64
|
-
const event = value as Partial<RegistrationEvent>;
|
|
65
|
-
return (
|
|
66
|
-
typeof event.accept === "function" &&
|
|
67
|
-
typeof event.registration?.name === "string" &&
|
|
68
|
-
typeof event.registration.version === "string" &&
|
|
69
|
-
typeof event.registration.changelogPath === "string"
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function parseChangelog(markdown: string): ChangelogEntry[] {
|
|
74
|
-
const entries: ChangelogEntry[] = [];
|
|
75
|
-
let currentLines: string[] = [];
|
|
76
|
-
let currentVersion: string | undefined;
|
|
77
|
-
|
|
78
|
-
for (const line of markdown.split("\n")) {
|
|
79
|
-
const version = line.match(/^##\s+\[?(\d+\.\d+\.\d+)\]?(?:\s.*)?$/)?.[1];
|
|
80
|
-
if (version) {
|
|
81
|
-
if (currentVersion)
|
|
82
|
-
entries.push({
|
|
83
|
-
content: currentLines.join("\n").trim(),
|
|
84
|
-
version: currentVersion,
|
|
85
|
-
});
|
|
86
|
-
currentVersion = version;
|
|
87
|
-
currentLines = [line];
|
|
88
|
-
} else if (currentVersion) {
|
|
89
|
-
currentLines.push(line);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (currentVersion)
|
|
94
|
-
entries.push({
|
|
95
|
-
content: currentLines.join("\n").trim(),
|
|
96
|
-
version: currentVersion,
|
|
97
|
-
});
|
|
98
|
-
return entries;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function compareVersions(left: string, right: string): number {
|
|
102
|
-
const leftParts = left.split(".").map(Number);
|
|
103
|
-
const rightParts = right.split(".").map(Number);
|
|
104
|
-
for (let index = 0; index < 3; index++) {
|
|
105
|
-
const difference = (leftParts[index] ?? 0) - (rightParts[index] ?? 0);
|
|
106
|
-
if (difference !== 0) return difference;
|
|
107
|
-
}
|
|
108
|
-
return 0;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function isStableVersion(version: string): boolean {
|
|
112
|
-
return /^\d+\.\d+\.\d+$/.test(version);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function statePath(): string {
|
|
116
|
-
return join(getAgentDir(), STATE_FILENAME);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function readState(path: string): Promise<ChangelogState> {
|
|
120
|
-
let text: string;
|
|
121
|
-
try {
|
|
122
|
-
text = await readFile(path, "utf8");
|
|
123
|
-
} catch (error) {
|
|
124
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
const parsed = JSON.parse(text) as unknown;
|
|
128
|
-
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
129
|
-
throw new Error(`${path} must contain a JSON object`);
|
|
130
|
-
for (const [name, value] of Object.entries(parsed)) {
|
|
131
|
-
if (name === SUPPRESS_KEY) {
|
|
132
|
-
if (typeof value !== "boolean")
|
|
133
|
-
throw new Error(`${path} ${SUPPRESS_KEY} must be a boolean`);
|
|
134
|
-
} else if (typeof value !== "string") {
|
|
135
|
-
throw new Error(`${path} version for ${name} must be a string`);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return parsed as ChangelogState;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async function acquireLock(path: string): Promise<() => Promise<void>> {
|
|
142
|
-
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
143
|
-
while (true) {
|
|
144
|
-
try {
|
|
145
|
-
await mkdir(path);
|
|
146
|
-
return () => rm(path, { force: true, recursive: true });
|
|
147
|
-
} catch (error) {
|
|
148
|
-
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
149
|
-
const info = await stat(path).catch(() => undefined);
|
|
150
|
-
if (info && Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
151
|
-
await rm(path, { force: true, recursive: true });
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
if (Date.now() >= deadline)
|
|
155
|
-
throw new Error(`Timed out waiting for ${path}`);
|
|
156
|
-
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function writeState(path: string, state: ChangelogState): Promise<void> {
|
|
162
|
-
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
163
|
-
const temporaryPath = `${path}.${process.pid}.${randomUUID()}.tmp`;
|
|
164
|
-
try {
|
|
165
|
-
const ordered = Object.fromEntries(
|
|
166
|
-
Object.entries(state).sort(([left], [right]) =>
|
|
167
|
-
left.localeCompare(right),
|
|
168
|
-
),
|
|
169
|
-
);
|
|
170
|
-
await writeFile(temporaryPath, `${JSON.stringify(ordered, null, 2)}\n`, {
|
|
171
|
-
encoding: "utf8",
|
|
172
|
-
mode: 0o600,
|
|
173
|
-
});
|
|
174
|
-
await rename(temporaryPath, path);
|
|
175
|
-
} catch (error) {
|
|
176
|
-
await rm(temporaryPath, { force: true });
|
|
177
|
-
throw error;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function packageMarkdown(
|
|
182
|
-
registration: PackageRegistration,
|
|
183
|
-
entries: ChangelogEntry[],
|
|
184
|
-
): string {
|
|
185
|
-
const body = (entry: ChangelogEntry) =>
|
|
186
|
-
entry.content
|
|
187
|
-
.replace(/^##[^\n]*\n*/, "")
|
|
188
|
-
.replace(/^###\s+(?:Changes|Minor Changes|Patch Changes)\s*\n*/gim, "")
|
|
189
|
-
.replace(/^###\s+(.+)$/gm, "**$1**");
|
|
190
|
-
if (entries.length === 1) {
|
|
191
|
-
const [entry] = entries;
|
|
192
|
-
if (!entry) return "";
|
|
193
|
-
return `## ${registration.name} ${entry.version}\n\n${body(entry)}`;
|
|
194
|
-
}
|
|
195
|
-
return [
|
|
196
|
-
`## ${registration.name}`,
|
|
197
|
-
...entries.map((entry) => `**${entry.version}**\n\n${body(entry)}`),
|
|
198
|
-
].join("\n\n");
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async function claimUpdates(
|
|
202
|
-
registrations: Iterable<PackageRegistration>,
|
|
203
|
-
): Promise<{ errors: string[]; markdown?: string }> {
|
|
204
|
-
const stableRegistrations = [...registrations]
|
|
205
|
-
.filter((registration) => isStableVersion(registration.version))
|
|
206
|
-
.sort((left, right) => left.name.localeCompare(right.name));
|
|
207
|
-
if (stableRegistrations.length === 0) return { errors: [] };
|
|
208
|
-
const path = statePath();
|
|
209
|
-
await mkdir(dirname(path), { mode: 0o700, recursive: true });
|
|
210
|
-
const release = await acquireLock(`${path}.lock`);
|
|
211
|
-
try {
|
|
212
|
-
const state = await readState(path);
|
|
213
|
-
const errors: string[] = [];
|
|
214
|
-
const sections: string[] = [];
|
|
215
|
-
let changed = false;
|
|
216
|
-
if (state[SUPPRESS_KEY] === true) {
|
|
217
|
-
for (const registration of stableRegistrations) {
|
|
218
|
-
const seenVersion = state[registration.name];
|
|
219
|
-
if (
|
|
220
|
-
typeof seenVersion !== "string" ||
|
|
221
|
-
compareVersions(registration.version, seenVersion) > 0
|
|
222
|
-
) {
|
|
223
|
-
state[registration.name] = registration.version;
|
|
224
|
-
changed = true;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (changed) await writeState(path, state);
|
|
228
|
-
return { errors };
|
|
229
|
-
}
|
|
230
|
-
for (const registration of stableRegistrations) {
|
|
231
|
-
const seenVersion = state[registration.name];
|
|
232
|
-
if (
|
|
233
|
-
typeof seenVersion === "string" &&
|
|
234
|
-
compareVersions(registration.version, seenVersion) <= 0
|
|
235
|
-
)
|
|
236
|
-
continue;
|
|
237
|
-
try {
|
|
238
|
-
const entries = parseChangelog(
|
|
239
|
-
await readFile(registration.changelogPath, "utf8"),
|
|
240
|
-
).filter(
|
|
241
|
-
(entry) =>
|
|
242
|
-
compareVersions(entry.version, registration.version) <= 0 &&
|
|
243
|
-
(typeof seenVersion === "string"
|
|
244
|
-
? compareVersions(entry.version, seenVersion) > 0
|
|
245
|
-
: entry.version === registration.version),
|
|
246
|
-
);
|
|
247
|
-
if (entries.length === 0) {
|
|
248
|
-
errors.push(
|
|
249
|
-
`No ${registration.version} changelog entry found for ${registration.name}`,
|
|
250
|
-
);
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
sections.push(packageMarkdown(registration, entries));
|
|
254
|
-
state[registration.name] = registration.version;
|
|
255
|
-
changed = true;
|
|
256
|
-
} catch (error) {
|
|
257
|
-
errors.push(
|
|
258
|
-
`Could not read the ${registration.name} changelog: ${error instanceof Error ? error.message : String(error)}`,
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (changed) await writeState(path, state);
|
|
263
|
-
return {
|
|
264
|
-
errors,
|
|
265
|
-
...(sections.length > 0 ? { markdown: sections.join("\n\n") } : {}),
|
|
266
|
-
};
|
|
267
|
-
} finally {
|
|
268
|
-
await release();
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function registerCoordinator(
|
|
273
|
-
pi: ExtensionAPI,
|
|
274
|
-
registration: PackageRegistration,
|
|
275
|
-
): void {
|
|
276
|
-
let accepted = false;
|
|
277
|
-
pi.events.emit(REGISTRATION_CHANNEL, {
|
|
278
|
-
registration,
|
|
279
|
-
accept: () => {
|
|
280
|
-
accepted = true;
|
|
281
|
-
},
|
|
282
|
-
} satisfies RegistrationEvent);
|
|
283
|
-
if (accepted) return;
|
|
284
|
-
|
|
285
|
-
const registrations = new Map([[registration.name, registration]]);
|
|
286
|
-
pi.events.on(REGISTRATION_CHANNEL, (value) => {
|
|
287
|
-
if (!isRegistrationEvent(value)) return;
|
|
288
|
-
registrations.set(value.registration.name, value.registration);
|
|
289
|
-
value.accept();
|
|
290
|
-
});
|
|
291
|
-
pi.registerEntryRenderer<ChangelogEntryData>(
|
|
292
|
-
ENTRY_TYPE,
|
|
293
|
-
(entry, _options, theme) => {
|
|
294
|
-
if (!entry.data) return undefined;
|
|
295
|
-
const container = new Container();
|
|
296
|
-
container.addChild(new DynamicBorder());
|
|
297
|
-
container.addChild(
|
|
298
|
-
new Text(theme.bold(theme.fg("accent", "What's New")), 1, 0),
|
|
299
|
-
);
|
|
300
|
-
container.addChild(new Spacer(1));
|
|
301
|
-
container.addChild(
|
|
302
|
-
new Markdown(entry.data.markdown.trim(), 1, 0, getMarkdownTheme()),
|
|
303
|
-
);
|
|
304
|
-
container.addChild(new Spacer(1));
|
|
305
|
-
container.addChild(new DynamicBorder());
|
|
306
|
-
return container;
|
|
307
|
-
},
|
|
308
|
-
);
|
|
309
|
-
pi.on("session_start", async (event, ctx) => {
|
|
310
|
-
if (event.reason === "reload" || ctx.mode !== "tui") return;
|
|
311
|
-
if (
|
|
312
|
-
ctx.sessionManager
|
|
313
|
-
.getEntries()
|
|
314
|
-
.some((entry) =>
|
|
315
|
-
["branch_summary", "compaction", "message"].includes(entry.type),
|
|
316
|
-
)
|
|
317
|
-
)
|
|
318
|
-
return;
|
|
319
|
-
try {
|
|
320
|
-
const update = await claimUpdates(registrations.values());
|
|
321
|
-
if (update.markdown)
|
|
322
|
-
pi.appendEntry<ChangelogEntryData>(ENTRY_TYPE, {
|
|
323
|
-
markdown: update.markdown,
|
|
324
|
-
});
|
|
325
|
-
for (const error of update.errors) ctx.ui.notify(error, "warning");
|
|
326
|
-
} catch (error) {
|
|
327
|
-
ctx.ui.notify(
|
|
328
|
-
`Could not update Howaboua changelog state: ${error instanceof Error ? error.message : String(error)}`,
|
|
329
|
-
"warning",
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
export default function howabouaPackageChangelog(pi: ExtensionAPI): void {
|
|
336
|
-
registerCoordinator(pi, packageRegistration());
|
|
337
|
-
}
|