pi-openai-toolkit 0.14.13 → 0.15.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/README.md +250 -241
- package/README.zh.md +250 -241
- package/config.schema.json +352 -0
- package/docs/configuration.md +176 -0
- package/docs/internals.md +139 -128
- package/package.json +135 -127
- package/src/auto-mode/extension.ts +35 -13
- package/src/config/legacy.ts +717 -0
- package/src/config/migration.ts +100 -0
- package/src/config/notifications.ts +28 -0
- package/src/config/policy.ts +122 -0
- package/src/config/v2.ts +1347 -0
- package/src/config-command.ts +73 -0
- package/src/config.ts +318 -739
- package/src/context-management/tools.ts +353 -347
- package/src/context-management/window-manager.ts +696 -693
- package/src/debug.ts +5 -5
- package/src/deferred-tool-carryover.ts +3 -1
- package/src/extension-runtime.ts +1375 -1329
- package/src/image-generation/extension.ts +14 -18
- package/src/image-generation/protocol.ts +10 -4
- package/src/image-generation/service.ts +9 -3
- package/src/request-context-cache.ts +2 -1
- package/src/types.ts +641 -628
- package/src/web-search/alpha-client.ts +876 -875
- package/src/web-search/extension.ts +529 -523
- package/src/web-search/payload.ts +299 -295
- package/src/web-search/prompt.ts +60 -59
- package/src/web-search/types.ts +144 -144
package/src/web-search/prompt.ts
CHANGED
|
@@ -1,59 +1,60 @@
|
|
|
1
|
-
import type { WebSearchConfig } from "../types";
|
|
2
|
-
import {
|
|
3
|
-
resolveWebSearchRoute,
|
|
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
|
-
const
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
1
|
+
import type { WebSearchConfig } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
resolveWebSearchRoute,
|
|
4
|
+
WEB_RUN_TOOL_NAME,
|
|
5
|
+
WEB_SEARCH_PROMPT_MARKER,
|
|
6
|
+
type WebSearchModel,
|
|
7
|
+
} from "./types";
|
|
8
|
+
|
|
9
|
+
export const WEB_SEARCH_PROMPT_SECTION = `${WEB_SEARCH_PROMPT_MARKER}
|
|
10
|
+
## Web Search
|
|
11
|
+
|
|
12
|
+
The hosted \`web_search\` tool is part of your tool list for this model. The API executes it server-side: it is a real callable tool even though no client-side function schema or description accompanies it. Call it whenever current or online information matters instead of guessing, cite the returned sources in the answer, and never claim that web search or internet access is unavailable while this section is present.`;
|
|
13
|
+
|
|
14
|
+
export const LOCAL_WEB_SEARCH_PROMPT_SECTION = `${WEB_SEARCH_PROMPT_MARKER}
|
|
15
|
+
## Web Search
|
|
16
|
+
|
|
17
|
+
The local \`web_search\` tool is available for this model. Use it when current or online information matters, and cite the sources returned by that tool. Do not claim that the provider API executes this local tool server-side.`;
|
|
18
|
+
|
|
19
|
+
export const STANDALONE_ALPHA_WEB_SEARCH_PROMPT_SECTION = `${WEB_SEARCH_PROMPT_MARKER}
|
|
20
|
+
## Web Search
|
|
21
|
+
|
|
22
|
+
The \`${WEB_RUN_TOOL_NAME}\` tool is available for this model. It is a sequential local wrapper backed by CPA/Codex standalone search at the provider's \`/alpha/search\` endpoint. It supports \`search_query\`, \`image_query\`, \`open\`, \`click\`, \`find\`, \`screenshot\`, \`finance\`, \`weather\`, \`sports\`, and \`time\`. Use it for current or online information, preserve reference ids when following results, and cite the returned sources.`;
|
|
23
|
+
|
|
24
|
+
function removeToolkitWebSearchPrompt(systemPrompt: string): string {
|
|
25
|
+
const markerIndex = systemPrompt.indexOf(WEB_SEARCH_PROMPT_MARKER);
|
|
26
|
+
if (markerIndex < 0) return systemPrompt;
|
|
27
|
+
return systemPrompt.slice(0, markerIndex).trimEnd();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function promptSectionForRoute(route: "local" | "hosted" | "standalone-alpha"): string {
|
|
31
|
+
switch (route) {
|
|
32
|
+
case "local":
|
|
33
|
+
return LOCAL_WEB_SEARCH_PROMPT_SECTION;
|
|
34
|
+
case "hosted":
|
|
35
|
+
return WEB_SEARCH_PROMPT_SECTION;
|
|
36
|
+
case "standalone-alpha":
|
|
37
|
+
return STANDALONE_ALPHA_WEB_SEARCH_PROMPT_SECTION;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function appendWebSearchPrompt(args: {
|
|
42
|
+
model: WebSearchModel | undefined;
|
|
43
|
+
config: WebSearchConfig;
|
|
44
|
+
systemPrompt: string;
|
|
45
|
+
/** Lifecycle registration can override this when the selected local wrapper is unavailable. */
|
|
46
|
+
routeAvailable?: boolean;
|
|
47
|
+
}): string {
|
|
48
|
+
const { model, config, systemPrompt } = args;
|
|
49
|
+
const basePrompt = removeToolkitWebSearchPrompt(systemPrompt);
|
|
50
|
+
const resolution = resolveWebSearchRoute({ model, config });
|
|
51
|
+
if (args.routeAvailable === false || resolution.route === "none" || !resolution.available) return basePrompt;
|
|
52
|
+
|
|
53
|
+
const section = promptSectionForRoute(resolution.route);
|
|
54
|
+
return `${basePrompt.trimEnd()}\n\n${section}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export const _promptTest = {
|
|
58
|
+
removeToolkitWebSearchPrompt,
|
|
59
|
+
promptSectionForRoute,
|
|
60
|
+
};
|
package/src/web-search/types.ts
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import { getExactModelKey, isExactModelAllowed } from "../model-scope";
|
|
2
|
-
import type { WebSearchConfig, WebSearchRoute } from "../types";
|
|
3
|
-
|
|
4
|
-
export const WEB_SEARCH_CAPABLE_APIS = ["openai-responses", "openai-codex-responses"] as const;
|
|
5
|
-
export const WEB_SEARCH_SOURCE_INCLUDE = "web_search_call.action.sources";
|
|
6
|
-
export const WEB_SEARCH_PROMPT_MARKER = "<!-- pi-openai-toolkit:web-search -->";
|
|
7
|
-
export const WEB_RUN_TOOL_NAME = "
|
|
8
|
-
export const LOCAL_WEB_SEARCH_TOOL_NAME = "web_search";
|
|
9
|
-
|
|
10
|
-
export type WebSearchCapableApi = (typeof WEB_SEARCH_CAPABLE_APIS)[number];
|
|
11
|
-
export type WebSearchRouteSource = "exact" | "default" | "legacy" | "none";
|
|
12
|
-
export type WebSearchRouteUnavailableReason = "missing-model" | "unsupported-api" | "invalid-route";
|
|
13
|
-
|
|
14
|
-
export type WebSearchModel = {
|
|
15
|
-
api?: string;
|
|
16
|
-
provider?: string;
|
|
17
|
-
id?: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type WebSearchRouteResolution =
|
|
21
|
-
| {
|
|
22
|
-
route: "none";
|
|
23
|
-
source: "none";
|
|
24
|
-
modelKey?: string;
|
|
25
|
-
reason: "disabled" | "unconfigured";
|
|
26
|
-
}
|
|
27
|
-
| {
|
|
28
|
-
route: WebSearchRoute;
|
|
29
|
-
source: Exclude<WebSearchRouteSource, "none">;
|
|
30
|
-
modelKey?: string;
|
|
31
|
-
available: boolean;
|
|
32
|
-
reason?: WebSearchRouteUnavailableReason;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export type WebSearchPayloadOutcome =
|
|
36
|
-
| "disabled"
|
|
37
|
-
| "unsupported-model"
|
|
38
|
-
| "non-object-payload"
|
|
39
|
-
| "invalid-tools"
|
|
40
|
-
| "invalid-include"
|
|
41
|
-
| "existing-native-tool"
|
|
42
|
-
| "injected-native-tool"
|
|
43
|
-
| "local-route"
|
|
44
|
-
| "standalone-route"
|
|
45
|
-
| "removed-conflicting-tools"
|
|
46
|
-
| "unavailable-route";
|
|
47
|
-
|
|
48
|
-
export type WebSearchPayloadTransform = {
|
|
49
|
-
payload: unknown;
|
|
50
|
-
outcome: WebSearchPayloadOutcome;
|
|
51
|
-
changed: boolean;
|
|
52
|
-
/** Explicit route failures must abort rather than silently select another search path. */
|
|
53
|
-
fatal?: boolean;
|
|
54
|
-
errorMessage?: string;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export function getWebSearchModelKey(model: WebSearchModel | undefined): string | undefined {
|
|
58
|
-
return getExactModelKey(model);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function isWebSearchRoute(value: unknown): value is WebSearchRoute {
|
|
62
|
-
return value === "local" || value === "hosted" || value === "standalone-alpha";
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function selectedRoute(args: {
|
|
66
|
-
route: WebSearchRoute;
|
|
67
|
-
source: Exclude<WebSearchRouteSource, "none">;
|
|
68
|
-
model: WebSearchModel | undefined;
|
|
69
|
-
modelKey: string | undefined;
|
|
70
|
-
}): WebSearchRouteResolution {
|
|
71
|
-
if (!args.modelKey) {
|
|
72
|
-
return {
|
|
73
|
-
route: args.route,
|
|
74
|
-
source: args.source,
|
|
75
|
-
available: false,
|
|
76
|
-
reason: "missing-model",
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
if (args.route !== "local" && !isWebSearchCapableApi(args.model?.api)) {
|
|
80
|
-
return {
|
|
81
|
-
route: args.route,
|
|
82
|
-
source: args.source,
|
|
83
|
-
modelKey: args.modelKey,
|
|
84
|
-
available: false,
|
|
85
|
-
reason: "unsupported-api",
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
return {
|
|
89
|
-
route: args.route,
|
|
90
|
-
source: args.source,
|
|
91
|
-
modelKey: args.modelKey,
|
|
92
|
-
available: true,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function isWebSearchCapableApi(api: unknown): api is WebSearchCapableApi {
|
|
97
|
-
return typeof api === "string" && (WEB_SEARCH_CAPABLE_APIS as readonly string[]).includes(api);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Resolve exactly one configured Web Search route. The legacy model allowlist is
|
|
102
|
-
* deliberately consulted only after explicit route fields so old configs retain
|
|
103
|
-
* hosted behavior without becoming an implicit alpha-search opt-in.
|
|
104
|
-
*/
|
|
105
|
-
export function resolveWebSearchRoute(args: {
|
|
106
|
-
model: WebSearchModel | undefined;
|
|
107
|
-
config: WebSearchConfig;
|
|
108
|
-
}): WebSearchRouteResolution {
|
|
109
|
-
const { model, config } = args;
|
|
110
|
-
const modelKey = getWebSearchModelKey(model);
|
|
111
|
-
if (!config.enabled) {
|
|
112
|
-
return { route: "none", source: "none", modelKey, reason: "disabled" };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const exactRoute = modelKey ? config.routes?.[modelKey] : undefined;
|
|
116
|
-
if (exactRoute !== undefined) {
|
|
117
|
-
if (!isWebSearchRoute(exactRoute)) {
|
|
118
|
-
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
119
|
-
}
|
|
120
|
-
return selectedRoute({ route: exactRoute, source: "exact", model, modelKey });
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (config.defaultRoute !== undefined) {
|
|
124
|
-
if (!isWebSearchRoute(config.defaultRoute)) {
|
|
125
|
-
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
126
|
-
}
|
|
127
|
-
return selectedRoute({ route: config.defaultRoute, source: "default", model, modelKey });
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (modelKey && isExactModelAllowed(model, config.models) && isWebSearchCapableApi(model?.api)) {
|
|
131
|
-
return selectedRoute({ route: "hosted", source: "legacy", model, modelKey });
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/** Compatibility predicate for callers that specifically need the hosted path. */
|
|
138
|
-
export function isWebSearchEnabledForModel(
|
|
139
|
-
model: WebSearchModel | undefined,
|
|
140
|
-
config: WebSearchConfig,
|
|
141
|
-
): boolean {
|
|
142
|
-
const resolution = resolveWebSearchRoute({ model, config });
|
|
143
|
-
return resolution.route === "hosted" && resolution.available;
|
|
144
|
-
}
|
|
1
|
+
import { getExactModelKey, isExactModelAllowed } from "../model-scope";
|
|
2
|
+
import type { WebSearchConfig, WebSearchRoute } from "../types";
|
|
3
|
+
|
|
4
|
+
export const WEB_SEARCH_CAPABLE_APIS = ["openai-responses", "openai-codex-responses"] as const;
|
|
5
|
+
export const WEB_SEARCH_SOURCE_INCLUDE = "web_search_call.action.sources";
|
|
6
|
+
export const WEB_SEARCH_PROMPT_MARKER = "<!-- pi-openai-toolkit:web-search -->";
|
|
7
|
+
export const WEB_RUN_TOOL_NAME = "web_run";
|
|
8
|
+
export const LOCAL_WEB_SEARCH_TOOL_NAME = "web_search";
|
|
9
|
+
|
|
10
|
+
export type WebSearchCapableApi = (typeof WEB_SEARCH_CAPABLE_APIS)[number];
|
|
11
|
+
export type WebSearchRouteSource = "exact" | "default" | "legacy" | "none";
|
|
12
|
+
export type WebSearchRouteUnavailableReason = "missing-model" | "unsupported-api" | "invalid-route";
|
|
13
|
+
|
|
14
|
+
export type WebSearchModel = {
|
|
15
|
+
api?: string;
|
|
16
|
+
provider?: string;
|
|
17
|
+
id?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type WebSearchRouteResolution =
|
|
21
|
+
| {
|
|
22
|
+
route: "none";
|
|
23
|
+
source: "none";
|
|
24
|
+
modelKey?: string;
|
|
25
|
+
reason: "disabled" | "unconfigured";
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
route: WebSearchRoute;
|
|
29
|
+
source: Exclude<WebSearchRouteSource, "none">;
|
|
30
|
+
modelKey?: string;
|
|
31
|
+
available: boolean;
|
|
32
|
+
reason?: WebSearchRouteUnavailableReason;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type WebSearchPayloadOutcome =
|
|
36
|
+
| "disabled"
|
|
37
|
+
| "unsupported-model"
|
|
38
|
+
| "non-object-payload"
|
|
39
|
+
| "invalid-tools"
|
|
40
|
+
| "invalid-include"
|
|
41
|
+
| "existing-native-tool"
|
|
42
|
+
| "injected-native-tool"
|
|
43
|
+
| "local-route"
|
|
44
|
+
| "standalone-route"
|
|
45
|
+
| "removed-conflicting-tools"
|
|
46
|
+
| "unavailable-route";
|
|
47
|
+
|
|
48
|
+
export type WebSearchPayloadTransform = {
|
|
49
|
+
payload: unknown;
|
|
50
|
+
outcome: WebSearchPayloadOutcome;
|
|
51
|
+
changed: boolean;
|
|
52
|
+
/** Explicit route failures must abort rather than silently select another search path. */
|
|
53
|
+
fatal?: boolean;
|
|
54
|
+
errorMessage?: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export function getWebSearchModelKey(model: WebSearchModel | undefined): string | undefined {
|
|
58
|
+
return getExactModelKey(model);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function isWebSearchRoute(value: unknown): value is WebSearchRoute {
|
|
62
|
+
return value === "local" || value === "hosted" || value === "standalone-alpha";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function selectedRoute(args: {
|
|
66
|
+
route: WebSearchRoute;
|
|
67
|
+
source: Exclude<WebSearchRouteSource, "none">;
|
|
68
|
+
model: WebSearchModel | undefined;
|
|
69
|
+
modelKey: string | undefined;
|
|
70
|
+
}): WebSearchRouteResolution {
|
|
71
|
+
if (!args.modelKey) {
|
|
72
|
+
return {
|
|
73
|
+
route: args.route,
|
|
74
|
+
source: args.source,
|
|
75
|
+
available: false,
|
|
76
|
+
reason: "missing-model",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (args.route !== "local" && !isWebSearchCapableApi(args.model?.api)) {
|
|
80
|
+
return {
|
|
81
|
+
route: args.route,
|
|
82
|
+
source: args.source,
|
|
83
|
+
modelKey: args.modelKey,
|
|
84
|
+
available: false,
|
|
85
|
+
reason: "unsupported-api",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
route: args.route,
|
|
90
|
+
source: args.source,
|
|
91
|
+
modelKey: args.modelKey,
|
|
92
|
+
available: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function isWebSearchCapableApi(api: unknown): api is WebSearchCapableApi {
|
|
97
|
+
return typeof api === "string" && (WEB_SEARCH_CAPABLE_APIS as readonly string[]).includes(api);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Resolve exactly one configured Web Search route. The legacy model allowlist is
|
|
102
|
+
* deliberately consulted only after explicit route fields so old configs retain
|
|
103
|
+
* hosted behavior without becoming an implicit alpha-search opt-in.
|
|
104
|
+
*/
|
|
105
|
+
export function resolveWebSearchRoute(args: {
|
|
106
|
+
model: WebSearchModel | undefined;
|
|
107
|
+
config: WebSearchConfig;
|
|
108
|
+
}): WebSearchRouteResolution {
|
|
109
|
+
const { model, config } = args;
|
|
110
|
+
const modelKey = getWebSearchModelKey(model);
|
|
111
|
+
if (!config.enabled) {
|
|
112
|
+
return { route: "none", source: "none", modelKey, reason: "disabled" };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const exactRoute = modelKey ? config.routes?.[modelKey] : undefined;
|
|
116
|
+
if (exactRoute !== undefined) {
|
|
117
|
+
if (!isWebSearchRoute(exactRoute)) {
|
|
118
|
+
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
119
|
+
}
|
|
120
|
+
return selectedRoute({ route: exactRoute, source: "exact", model, modelKey });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (config.defaultRoute !== undefined) {
|
|
124
|
+
if (!isWebSearchRoute(config.defaultRoute)) {
|
|
125
|
+
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
126
|
+
}
|
|
127
|
+
return selectedRoute({ route: config.defaultRoute, source: "default", model, modelKey });
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (modelKey && isExactModelAllowed(model, config.models) && isWebSearchCapableApi(model?.api)) {
|
|
131
|
+
return selectedRoute({ route: "hosted", source: "legacy", model, modelKey });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return { route: "none", source: "none", modelKey, reason: "unconfigured" };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Compatibility predicate for callers that specifically need the hosted path. */
|
|
138
|
+
export function isWebSearchEnabledForModel(
|
|
139
|
+
model: WebSearchModel | undefined,
|
|
140
|
+
config: WebSearchConfig,
|
|
141
|
+
): boolean {
|
|
142
|
+
const resolution = resolveWebSearchRoute({ model, config });
|
|
143
|
+
return resolution.route === "hosted" && resolution.available;
|
|
144
|
+
}
|