@oai404iao/pi-codex-runtime 0.2.0 → 0.3.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 +6 -1
- package/package.json +6 -6
- package/src/broker.ts +3 -0
- package/src/code-mode-contributions.ts +63 -0
- package/src/code-mode-owner.ts +45 -0
- package/src/providers/responses/grammar.ts +52 -0
- package/src/providers/responses/messages.ts +10 -3
- package/src/providers/responses/sampling.ts +41 -0
- package/src/providers/responses/stream.ts +14 -11
- package/src/providers/responses/text.ts +4 -2
- package/src/providers/responses/tools.ts +12 -7
- package/src/providers/responses/types.ts +5 -0
- package/src/tool-activation.ts +33 -4
package/README.md
CHANGED
|
@@ -4,12 +4,17 @@ Alpha bootstrap candidate; initial npm publication is still pending.
|
|
|
4
4
|
It has no Pi extension entry
|
|
5
5
|
and does not automatically register tools or providers.
|
|
6
6
|
|
|
7
|
-
Peer floor: Pi 0.
|
|
7
|
+
Peer floor: Pi 0.86.1; tested against 0.86.1.
|
|
8
8
|
|
|
9
9
|
Owns shared Codex authentication/headers, wire identity, settings/catalog,
|
|
10
10
|
Responses replay contracts and the session-scoped composition broker.
|
|
11
11
|
Capability-specific HTTP clients, storage and transports live in other packages.
|
|
12
12
|
|
|
13
|
+
Responses helpers encode/decode generic grammar tools and replay calls/results
|
|
14
|
+
according to the current declaration, preserving legacy custom patch behavior.
|
|
15
|
+
An internal structural event adapter lets owners offer optional Code Mode
|
|
16
|
+
contributions without importing or depending on the private Code Mode package.
|
|
17
|
+
|
|
13
18
|
Configuration paths remain
|
|
14
19
|
`<agentDir>/extensions/pi-codex-minimal-tools/{config,models}.json`.
|
|
15
20
|
This package ships the canonical schemas and default catalog, including the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oai404iao/pi-codex-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Unpublished pi-codex-runtime workspace for the staged Codex split",
|
|
6
6
|
"type": "module",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"./subagent-inline": "./src/subagent-inline.ts"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@earendil-works/pi-ai": ">=0.
|
|
27
|
-
"@earendil-works/pi-coding-agent": ">=0.
|
|
26
|
+
"@earendil-works/pi-ai": ">=0.86.1",
|
|
27
|
+
"@earendil-works/pi-coding-agent": ">=0.86.1"
|
|
28
28
|
},
|
|
29
29
|
"peerDependenciesMeta": {
|
|
30
30
|
"@earendil-works/pi-ai": {
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@earendil-works/pi-ai": "0.
|
|
40
|
-
"@earendil-works/pi-coding-agent": "0.
|
|
39
|
+
"@earendil-works/pi-ai": "0.86.1",
|
|
40
|
+
"@earendil-works/pi-coding-agent": "0.86.1",
|
|
41
41
|
"@types/node": "^26.2.0",
|
|
42
42
|
"tsx": "^4.20.6",
|
|
43
43
|
"typescript": "^7.0.2"
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">=22.19.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "b02484cecad9081886797bf9b4438135aaae1e07"
|
|
68
68
|
}
|
package/src/broker.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import type { PackageToolName } from "./capabilities.js";
|
|
4
4
|
import type { ProviderPresentation } from "./extension/provider-presentation.js";
|
|
5
|
+
import type { OwnerState } from "./code-mode-owner.js";
|
|
5
6
|
|
|
6
7
|
export const CODEX_BROKER_CHANNEL = "@oai404iao/pi-codex:broker";
|
|
7
8
|
export const CODEX_RUNTIME_VERSION: string = createRequire(import.meta.url)("../package.json").version;
|
|
@@ -10,6 +11,7 @@ const CACHE = Symbol.for("@oai404iao/pi-codex/broker/v1");
|
|
|
10
11
|
export interface InstalledTool {
|
|
11
12
|
register(): void;
|
|
12
13
|
registered: boolean;
|
|
14
|
+
codeModeOwner?: OwnerState;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export interface CodexBroker {
|
|
@@ -18,6 +20,7 @@ export interface CodexBroker {
|
|
|
18
20
|
readonly closed: boolean;
|
|
19
21
|
readonly tools: Map<PackageToolName, InstalledTool>;
|
|
20
22
|
coreEnabled: boolean;
|
|
23
|
+
codeModeDefinitions?: Map<string, { parameters: unknown; description: string }>;
|
|
21
24
|
claim(name: string): boolean;
|
|
22
25
|
addPresentation(name: string, presentation: ProviderPresentation): void;
|
|
23
26
|
presentation: ProviderPresentation;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { getCodexBroker } from "./broker.js";
|
|
3
|
+
import { codeModeOwner, type DirectBinding } from "./code-mode-owner.js";
|
|
4
|
+
import type { PackageToolName } from "./capabilities.js";
|
|
5
|
+
|
|
6
|
+
/** Unique public schema reference proves which registration won Pi's registry.
|
|
7
|
+
* If a future Pi clones metadata, cooperation fails closed instead of claiming
|
|
8
|
+
* a name/source belonging to another extension. */
|
|
9
|
+
export function registerCodeModeOwnedTool(pi: ExtensionAPI, definition: Record<string, unknown>): void {
|
|
10
|
+
if (typeof definition.name !== "string" || typeof definition.description !== "string"
|
|
11
|
+
|| !definition.parameters || typeof definition.parameters !== "object") throw new Error("Invalid owned Code Mode tool definition");
|
|
12
|
+
const broker = getCodexBroker(pi);
|
|
13
|
+
const owned = { ...definition, name: definition.name, description: definition.description, parameters: { ...definition.parameters } };
|
|
14
|
+
(broker.codeModeDefinitions ??= new Map()).set(owned.name, owned);
|
|
15
|
+
pi.registerTool(owned as never);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface NestedCodeModeContext {
|
|
19
|
+
readonly cellId: string;
|
|
20
|
+
readonly toolCallId: string;
|
|
21
|
+
readonly cwd: string;
|
|
22
|
+
readonly signal: AbortSignal;
|
|
23
|
+
readonly pi?: ExtensionContext;
|
|
24
|
+
}
|
|
25
|
+
export interface OwnedCodeModeTool {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
parameters: unknown;
|
|
29
|
+
effect: "read" | "write";
|
|
30
|
+
parallel?: boolean;
|
|
31
|
+
direct?: DirectBinding;
|
|
32
|
+
invoke(input: unknown, context: NestedCodeModeContext): Promise<{ value: unknown }>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Structural client for the optional v1 bus contract. Neither side installs
|
|
36
|
+
* the other package; all authority still comes from Code Mode's exact grants. */
|
|
37
|
+
export function registerCodeModeContribution(pi: ExtensionAPI, id: string,
|
|
38
|
+
tools: (context: ExtensionContext) => readonly OwnedCodeModeTool[]): void {
|
|
39
|
+
let context: ExtensionContext | undefined;
|
|
40
|
+
let disposed = false;
|
|
41
|
+
const changed = () => pi.events.emit("@oai404iao/pi-code-mode:changed/v1", { version: 1 });
|
|
42
|
+
const off = pi.events.on("@oai404iao/pi-code-mode:discover/v1", (value) => {
|
|
43
|
+
const discovery = value as { version?: number; provider?: (provider: unknown) => void } | undefined;
|
|
44
|
+
if (!disposed && context && discovery?.version === 1 && typeof discovery.provider === "function") {
|
|
45
|
+
const broker = getCodexBroker(pi);
|
|
46
|
+
discovery.provider({ id, tools: tools(context).map((tool) => {
|
|
47
|
+
const owned = broker.tools.get(tool.name as PackageToolName);
|
|
48
|
+
return { ...tool, direct: owned ? codeModeOwner(pi, tool.name, owned, broker.codeModeDefinitions?.get(tool.name))?.binding : undefined };
|
|
49
|
+
}) });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const update = (_event: unknown, ctx: ExtensionContext) => {
|
|
53
|
+
if (disposed) return;
|
|
54
|
+
context = ctx;
|
|
55
|
+
changed();
|
|
56
|
+
};
|
|
57
|
+
pi.on("session_start", update);
|
|
58
|
+
pi.on("model_select", update);
|
|
59
|
+
pi.on("session_shutdown", () => {
|
|
60
|
+
if (disposed) return;
|
|
61
|
+
disposed = true; context = undefined; off(); changed();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export interface DirectBinding {
|
|
4
|
+
readonly version: 1; readonly name: string;
|
|
5
|
+
acquire(): { reconcile(): boolean; release(): void } | undefined;
|
|
6
|
+
}
|
|
7
|
+
interface Control {
|
|
8
|
+
binding: DirectBinding; readonly activeIntent: boolean | undefined;
|
|
9
|
+
projectActive(active: boolean): boolean | undefined;
|
|
10
|
+
reconcile(): boolean; dispose(): void;
|
|
11
|
+
}
|
|
12
|
+
interface Factory {
|
|
13
|
+
version: 1; create(pi: ExtensionAPI, options: { name: string; sourcePath: string }): Control;
|
|
14
|
+
}
|
|
15
|
+
export interface OwnerState { identity: string; replaced?: boolean; factory?: Factory; control?: Control }
|
|
16
|
+
export const OWNER_CHANGED = "@oai404iao/pi-code-mode:direct-owner-changed/v1";
|
|
17
|
+
/** Optional v1 bus client. No private-package dependency or global registry. */
|
|
18
|
+
export function codeModeOwner(pi: ExtensionAPI, name: string, tool: { registered: boolean; codeModeOwner?: OwnerState },
|
|
19
|
+
expected?: { parameters: unknown; description: string }): Control | undefined {
|
|
20
|
+
if (!["apply_patch", "web_search"].includes(name) || !tool.registered) return;
|
|
21
|
+
const info = pi.getAllTools?.().find((item) => item.name === name);
|
|
22
|
+
if (!info?.sourceInfo || ["builtin", "sdk"].includes(info.sourceInfo.source)) return tool.codeModeOwner?.control;
|
|
23
|
+
if (!expected || info.parameters !== expected.parameters || info.description !== expected.description) {
|
|
24
|
+
if (expected) (tool.codeModeOwner ??= { identity: "" }).replaced = true;
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const identity = JSON.stringify([info.sourceInfo, info.description, info.parameters, info.promptGuidelines]);
|
|
28
|
+
const state = tool.codeModeOwner ??= { identity };
|
|
29
|
+
// Never rebind a replaced registration, even if the optional factory reloads.
|
|
30
|
+
if (state.identity !== identity) { state.replaced = true; return state.control; }
|
|
31
|
+
if (state.replaced) return state.control;
|
|
32
|
+
const factories: Factory[] = [];
|
|
33
|
+
pi.events.emit("@oai404iao/pi-code-mode:direct-owner/v1", { version: 1, accept(value: Factory) {
|
|
34
|
+
if (factories.length < 2) factories.push(value);
|
|
35
|
+
} });
|
|
36
|
+
const factory = factories.length === 1 && factories[0]?.version === 1
|
|
37
|
+
&& typeof factories[0].create === "function" ? factories[0] : undefined;
|
|
38
|
+
if (factory !== state.factory) {
|
|
39
|
+
const old = state.control;
|
|
40
|
+
state.control = undefined; state.factory = factory;
|
|
41
|
+
old?.dispose();
|
|
42
|
+
if (factory) state.control = factory.create(pi, { name, sourcePath: info.sourceInfo.path });
|
|
43
|
+
}
|
|
44
|
+
return state.control;
|
|
45
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Api, AssistantMessage, AssistantMessageEventStream, Model, Tool } from "@earendil-works/pi-ai";
|
|
2
|
+
import { grammarInputDelta, resolveGrammarSampling } from "./sampling.js";
|
|
3
|
+
import type { CustomToolCallState, OpenAIResponsesStreamOptions } from "./types.js";
|
|
4
|
+
import { localToolName } from "./text.js";
|
|
5
|
+
|
|
6
|
+
export function supportsGrammar(model: Model<Api>): boolean {
|
|
7
|
+
return (model.compat as { supportsOpenAIGrammarTools?: boolean } | undefined)?.supportsOpenAIGrammarTools === true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Capture the actual request's custom declarations, including Lite namespaces.
|
|
11
|
+
* Legacy custom tools without sampling metadata keep their established {input}. */
|
|
12
|
+
export function responseGrammarProperties(body: { tools?: unknown; input?: unknown }, tools?: Tool[], includeLegacy = false): ReadonlyMap<string, string> {
|
|
13
|
+
const properties = new Map<string, string>();
|
|
14
|
+
const visit = (items: unknown, namespace?: string): void => {
|
|
15
|
+
if (!Array.isArray(items)) return;
|
|
16
|
+
for (const item of items) {
|
|
17
|
+
if (!item || typeof item !== "object") continue;
|
|
18
|
+
if (item.type === "namespace" && typeof item.name === "string") { visit(item.tools, item.name); continue; }
|
|
19
|
+
if (item.type !== "custom" || typeof item.name !== "string") continue;
|
|
20
|
+
const name = localToolName(namespace, item.name);
|
|
21
|
+
const tool = tools?.find((candidate) => candidate.name === name);
|
|
22
|
+
const grammar = tool && resolveGrammarSampling(tool, true);
|
|
23
|
+
if (grammar) properties.set(name, grammar.inputProperty);
|
|
24
|
+
else if (includeLegacy) properties.set(name, "input");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
visit(body.tools);
|
|
28
|
+
if (Array.isArray(body.input)) for (const item of body.input) {
|
|
29
|
+
if (item?.type === "additional_tools") visit(item.tools);
|
|
30
|
+
}
|
|
31
|
+
return properties;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function customArguments(name: string, input: string, options?: OpenAIResponsesStreamOptions): Record<string, string> {
|
|
35
|
+
return { [options?.grammarToolInputProperties?.get(name) ?? "input"]: input };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function updateCustomInput(state: CustomToolCallState, input: string, close: boolean,
|
|
39
|
+
output: AssistantMessage, stream: AssistantMessageEventStream, options?: OpenAIResponsesStreamOptions): void {
|
|
40
|
+
const property = options?.grammarToolInputProperties?.get(state.block.name);
|
|
41
|
+
const previous = state.input;
|
|
42
|
+
state.input = input;
|
|
43
|
+
state.block.partialInput = input;
|
|
44
|
+
state.block.arguments = customArguments(state.block.name, input, options);
|
|
45
|
+
if (property !== undefined) {
|
|
46
|
+
state.inputJson ??= { input: "", started: false, closed: false };
|
|
47
|
+
const delta = grammarInputDelta(state.inputJson, property, input, close);
|
|
48
|
+
if (delta) stream.push({ type: "toolcall_delta", contentIndex: state.blockIndex, delta, partial: output });
|
|
49
|
+
} else if (!close && input.startsWith(previous) && input.length > previous.length) {
|
|
50
|
+
stream.push({ type: "toolcall_delta", contentIndex: state.blockIndex, delta: input.slice(previous.length), partial: output });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -6,6 +6,7 @@ import { decodeWebSearchActivityTextSignature, isWebSearchActivityTextSignature,
|
|
|
6
6
|
import { sanitizeSurrogates, shortHash } from "./text.js";
|
|
7
7
|
import { wireToolIdentity } from "./tool-identity.js";
|
|
8
8
|
import { type ConvertResponsesMessagesOptions, type InternalAssistantContent, type Message } from "./types.js";
|
|
9
|
+
import { grammarToolInput } from "./sampling.js";
|
|
9
10
|
|
|
10
11
|
export function convertResponsesMessages<TApi extends Api>(
|
|
11
12
|
model: Model<TApi>,
|
|
@@ -14,6 +15,7 @@ export function convertResponsesMessages<TApi extends Api>(
|
|
|
14
15
|
options?: ConvertResponsesMessagesOptions,
|
|
15
16
|
): ResponseInput {
|
|
16
17
|
const messages: ResponseInput = [];
|
|
18
|
+
const customCalls = new Map<string, boolean>();
|
|
17
19
|
const normalizeIdPart = (part: string) => {
|
|
18
20
|
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
19
21
|
const normalized = sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
|
@@ -92,9 +94,14 @@ export function convertResponsesMessages<TApi extends Api>(
|
|
|
92
94
|
assistantBlockIndex++;
|
|
93
95
|
} else if (block.type === "toolCall") {
|
|
94
96
|
const [callId, itemIdRaw] = block.id.split("|");
|
|
95
|
-
const
|
|
97
|
+
const property = options?.grammarToolInputProperties?.get(block.name);
|
|
98
|
+
const declared = options?.grammarToolInputProperties !== undefined && context.tools?.some((tool) => tool.name === block.name);
|
|
99
|
+
const custom = property !== undefined || (!declared && itemIdRaw?.startsWith("ctc_") === true && typeof block.arguments.input === "string");
|
|
100
|
+
customCalls.set(callId, custom);
|
|
96
101
|
let itemId: string | undefined = itemIdRaw;
|
|
97
102
|
if (isDifferentModel && (itemId?.startsWith("fc_") || itemId?.startsWith("ctc_"))) itemId = undefined;
|
|
103
|
+
if (!custom && itemId?.startsWith("ctc_")) itemId = undefined;
|
|
104
|
+
if (custom && itemId?.startsWith("fc_")) itemId = undefined;
|
|
98
105
|
const wireIdentity = wireToolIdentity(block.name, block.thoughtSignature);
|
|
99
106
|
if (custom) {
|
|
100
107
|
output.push({
|
|
@@ -103,7 +110,7 @@ export function convertResponsesMessages<TApi extends Api>(
|
|
|
103
110
|
call_id: callId,
|
|
104
111
|
name: wireIdentity.name,
|
|
105
112
|
...(wireIdentity.namespace ? { namespace: wireIdentity.namespace } : {}),
|
|
106
|
-
input:
|
|
113
|
+
input: sanitizeSurrogates(grammarToolInput(block.name, block.arguments, property ?? "input")),
|
|
107
114
|
} as ResponseInput[number]);
|
|
108
115
|
} else {
|
|
109
116
|
output.push({
|
|
@@ -157,7 +164,7 @@ export function convertResponsesMessages<TApi extends Api>(
|
|
|
157
164
|
]
|
|
158
165
|
: sanitizeSurrogates(hasText ? textResult : "(see attached image)");
|
|
159
166
|
messages.push({
|
|
160
|
-
type: itemId?.startsWith("ctc_") ? "custom_tool_call_output" : "function_call_output",
|
|
167
|
+
type: (customCalls.get(callId) ?? (options?.grammarToolInputProperties?.has(msg.toolName) || itemId?.startsWith("ctc_"))) ? "custom_tool_call_output" : "function_call_output",
|
|
161
168
|
call_id: callId,
|
|
162
169
|
output,
|
|
163
170
|
} as ResponseInput[number]);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Tool } from "@earendil-works/pi-ai";
|
|
2
|
+
|
|
3
|
+
export interface GrammarInputBuffer { input: string; started: boolean; closed: boolean }
|
|
4
|
+
export interface GrammarSampling { format: "lark" | "regex"; definition: string; inputProperty: string }
|
|
5
|
+
|
|
6
|
+
// Pi's extension loader aliases SDK roots to files, so api/* subpath imports
|
|
7
|
+
// do not resolve in production extensions. Implement the small wire contract
|
|
8
|
+
// locally rather than resolving private SDK dist paths.
|
|
9
|
+
export function resolveGrammarSampling(tool: Tool, supported: boolean): GrammarSampling | undefined {
|
|
10
|
+
const sampling = tool.constrainedSampling;
|
|
11
|
+
if (!supported || !sampling || sampling.type !== "grammar") return undefined;
|
|
12
|
+
const schema = tool.parameters as { type?: unknown; required?: unknown; properties?: Record<string, { type?: unknown }> };
|
|
13
|
+
const required = schema.required;
|
|
14
|
+
if (schema.type !== "object" || !Array.isArray(required) || required.length !== 1
|
|
15
|
+
|| typeof required[0] !== "string" || schema.properties?.[required[0]]?.type !== "string") {
|
|
16
|
+
throw new Error(`Tool ${tool.name} grammar requires exactly one required string property`);
|
|
17
|
+
}
|
|
18
|
+
for (const [variant, format] of [["openai_lark", "lark"], ["openai_regex", "regex"]] as const) {
|
|
19
|
+
const definition = sampling.variants[variant];
|
|
20
|
+
if (typeof definition === "string" && definition.trim()) return { format, definition, inputProperty: required[0] };
|
|
21
|
+
}
|
|
22
|
+
throw new Error(`Tool ${tool.name} has no supported grammar variant`);
|
|
23
|
+
}
|
|
24
|
+
export function grammarToolInput(name: string, args: Record<string, unknown>, property: string): string {
|
|
25
|
+
const value = args[property];
|
|
26
|
+
if (typeof value !== "string") throw new Error(`Grammar tool ${name} requires string argument ${property}`);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
export function grammarInputDelta(buffer: GrammarInputBuffer, property: string, next: string, close: boolean): string | undefined {
|
|
30
|
+
if (buffer.closed) {
|
|
31
|
+
if (next !== buffer.input) throw new Error("Grammar input changed after it was closed");
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
if (!next.startsWith(buffer.input)) throw new Error("Grammar input is not monotonic");
|
|
35
|
+
const suffix = JSON.stringify(next.slice(buffer.input.length)).slice(1, -1);
|
|
36
|
+
const delta = (buffer.started ? "" : `{${JSON.stringify(property)}:"`) + suffix + (close ? '"}' : "");
|
|
37
|
+
buffer.input = next;
|
|
38
|
+
buffer.started = true;
|
|
39
|
+
buffer.closed = close;
|
|
40
|
+
return delta || undefined;
|
|
41
|
+
}
|
|
@@ -5,7 +5,8 @@ import { createResponsesStreamState } from "./stream-state.js";
|
|
|
5
5
|
import { createResponseTextRenderer } from "./text-renderer.js";
|
|
6
6
|
import { localToolName, parseStreamingJson } from "./text.js";
|
|
7
7
|
import { encodeToolNamespaceSignature } from "./tool-identity.js";
|
|
8
|
-
import type { TextBlock, ThinkingBlock, ToolCallBlock } from "./types.js";
|
|
8
|
+
import type { CustomToolCallState, TextBlock, ThinkingBlock, ToolCallBlock } from "./types.js";
|
|
9
|
+
import { customArguments, updateCustomInput } from "./grammar.js";
|
|
9
10
|
import { type OpenAIResponsesStreamOptions } from "./types.js";
|
|
10
11
|
import { finalizeResponseUsage } from "./usage.js";
|
|
11
12
|
|
|
@@ -97,12 +98,12 @@ export async function processResponsesStream<TApi extends Api>(
|
|
|
97
98
|
type: "toolCall",
|
|
98
99
|
id: `${customItem.call_id}|${itemId}`,
|
|
99
100
|
name: localToolName(customItem.namespace, customItem.name),
|
|
100
|
-
arguments:
|
|
101
|
+
arguments: customArguments(localToolName(customItem.namespace, customItem.name), input, options),
|
|
101
102
|
...(thoughtSignature ? { thoughtSignature } : {}),
|
|
102
103
|
partialInput: input,
|
|
103
104
|
};
|
|
104
105
|
pushResponseBlock(currentBlock, event.output_index);
|
|
105
|
-
|
|
106
|
+
const state: CustomToolCallState = {
|
|
106
107
|
kind: "custom_tool_call",
|
|
107
108
|
blockIndex: blockIndex(),
|
|
108
109
|
block: currentBlock,
|
|
@@ -110,8 +111,10 @@ export async function processResponsesStream<TApi extends Api>(
|
|
|
110
111
|
sourceItemId: customItem.id,
|
|
111
112
|
callId: customItem.call_id,
|
|
112
113
|
input,
|
|
113
|
-
}
|
|
114
|
+
};
|
|
115
|
+
outputStates.set(event.output_index, state);
|
|
114
116
|
stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
|
|
117
|
+
if (options?.grammarToolInputProperties?.has(currentBlock.name)) updateCustomInput(state, input, false, output, stream, options);
|
|
115
118
|
}
|
|
116
119
|
} else if (event.type === "response.reasoning_summary_part.added") {
|
|
117
120
|
const state = outputStates.get(event.output_index);
|
|
@@ -193,11 +196,12 @@ export async function processResponsesStream<TApi extends Api>(
|
|
|
193
196
|
const customEvent = event as unknown as { output_index?: number; item_id?: string; call_id?: string; delta?: string };
|
|
194
197
|
const state = findCustomToolCallState(customEvent);
|
|
195
198
|
if (state && typeof customEvent.delta === "string") {
|
|
196
|
-
state.input
|
|
197
|
-
state.block.partialInput = state.input;
|
|
198
|
-
state.block.arguments = { input: state.input };
|
|
199
|
-
stream.push({ type: "toolcall_delta", contentIndex: state.blockIndex, delta: customEvent.delta, partial: output });
|
|
199
|
+
updateCustomInput(state, state.input + customEvent.delta, false, output, stream, options);
|
|
200
200
|
}
|
|
201
|
+
} else if ((event as { type?: string }).type === "response.custom_tool_call_input.done") {
|
|
202
|
+
const customEvent = event as unknown as { output_index?: number; item_id?: string; call_id?: string; input?: string };
|
|
203
|
+
const state = findCustomToolCallState(customEvent);
|
|
204
|
+
if (state && typeof customEvent.input === "string") updateCustomInput(state, customEvent.input, true, output, stream, options);
|
|
201
205
|
} else if (event.type === "response.function_call_arguments.delta") {
|
|
202
206
|
const state = outputStates.get(event.output_index);
|
|
203
207
|
if (state?.kind === "function_call") {
|
|
@@ -309,9 +313,8 @@ export async function processResponsesStream<TApi extends Api>(
|
|
|
309
313
|
const thoughtSignature = encodeToolNamespaceSignature(customItem.namespace, customItem.name);
|
|
310
314
|
const toolCall = state
|
|
311
315
|
? (() => {
|
|
312
|
-
state.input = input;
|
|
313
316
|
state.block.name = localToolName(customItem.namespace, customItem.name);
|
|
314
|
-
state
|
|
317
|
+
updateCustomInput(state, input, true, output, stream, options);
|
|
315
318
|
if (thoughtSignature) state.block.thoughtSignature = thoughtSignature;
|
|
316
319
|
delete state.block.partialInput;
|
|
317
320
|
return state.block;
|
|
@@ -321,7 +324,7 @@ export async function processResponsesStream<TApi extends Api>(
|
|
|
321
324
|
type: "toolCall",
|
|
322
325
|
id: `${customItem.call_id}|${customItemId(customItem.id, customItem.call_id)}`,
|
|
323
326
|
name: localToolName(customItem.namespace, customItem.name),
|
|
324
|
-
arguments:
|
|
327
|
+
arguments: customArguments(localToolName(customItem.namespace, customItem.name), input, options),
|
|
325
328
|
...(thoughtSignature ? { thoughtSignature } : {}),
|
|
326
329
|
};
|
|
327
330
|
pushResponseBlock(fallbackToolCall, event.output_index);
|
|
@@ -11,10 +11,11 @@ export function shortHash(str: string): string {
|
|
|
11
11
|
return (h2 >>> 0).toString(36) + (h1 >>> 0).toString(36);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
export function parseStreamingJson(partialJson: string):
|
|
14
|
+
export function parseStreamingJson(partialJson: string): JsonObject {
|
|
15
15
|
if (!partialJson || partialJson.trim() === "") return {};
|
|
16
16
|
try {
|
|
17
|
-
|
|
17
|
+
const value: unknown = JSON.parse(partialJson);
|
|
18
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value as JsonObject : {};
|
|
18
19
|
} catch {
|
|
19
20
|
return {};
|
|
20
21
|
}
|
|
@@ -30,3 +31,4 @@ export function localToolName(namespace: unknown, name: string): string {
|
|
|
30
31
|
if (namespace === "image_gen" && name === "imagegen") return "image_generation";
|
|
31
32
|
return name;
|
|
32
33
|
}
|
|
34
|
+
import type { JsonObject } from "@earendil-works/pi-ai";
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { type Tool } from "@earendil-works/pi-ai";
|
|
2
2
|
import { type Tool as OpenAITool } from "openai/resources/responses/responses.js";
|
|
3
3
|
import { type ConvertResponsesToolsOptions } from "./types.js";
|
|
4
|
+
import { resolveGrammarSampling } from "./sampling.js";
|
|
4
5
|
|
|
5
6
|
export function convertResponsesTools(tools: Tool[], options?: ConvertResponsesToolsOptions): OpenAITool[] {
|
|
6
7
|
const strict = options?.strict === undefined ? false : options.strict;
|
|
7
|
-
return tools.map((tool) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
return tools.map((tool) => {
|
|
9
|
+
const grammar = resolveGrammarSampling(tool, options?.supportsOpenAIGrammarTools === true);
|
|
10
|
+
if (grammar) return {
|
|
11
|
+
type: "custom", name: tool.name, description: tool.description,
|
|
12
|
+
format: { type: "grammar", syntax: grammar.format, definition: grammar.definition },
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
type: "function", name: tool.name, description: tool.description,
|
|
16
|
+
parameters: tool.parameters as unknown as Record<string, unknown>, strict,
|
|
17
|
+
};
|
|
18
|
+
});
|
|
14
19
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
2
2
|
import { type Context, type Usage } from "@earendil-works/pi-ai";
|
|
3
3
|
import { type ResponseCreateParamsStreaming } from "openai/resources/responses/responses.js";
|
|
4
|
+
import type { GrammarInputBuffer } from "./sampling.js";
|
|
4
5
|
|
|
5
6
|
type MessageRole = Context["messages"][number]["role"];
|
|
6
7
|
|
|
@@ -52,6 +53,7 @@ export interface ReplayableResponseMessageItem {
|
|
|
52
53
|
export type InternalAssistantContent = Extract<Message, { role: "assistant" }>["content"][number] | ImageGenerationCallBlock;
|
|
53
54
|
|
|
54
55
|
export interface OpenAIResponsesStreamOptions {
|
|
56
|
+
grammarToolInputProperties?: ReadonlyMap<string, string>;
|
|
55
57
|
serviceTier?: ResponseCreateParamsStreaming["service_tier"];
|
|
56
58
|
resolveServiceTier?: (
|
|
57
59
|
responseServiceTier: ResponseCreateParamsStreaming["service_tier"] | undefined,
|
|
@@ -66,10 +68,12 @@ export type TextSignaturePhase = "commentary" | "final_answer";
|
|
|
66
68
|
|
|
67
69
|
export interface ConvertResponsesMessagesOptions {
|
|
68
70
|
includeSystemPrompt?: boolean;
|
|
71
|
+
grammarToolInputProperties?: ReadonlyMap<string, string>;
|
|
69
72
|
}
|
|
70
73
|
|
|
71
74
|
export interface ConvertResponsesToolsOptions {
|
|
72
75
|
strict?: boolean | null;
|
|
76
|
+
supportsOpenAIGrammarTools?: boolean;
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
export type ThinkingBlock = Extract<AssistantMessage["content"][number], { type: "thinking" }>;
|
|
@@ -108,6 +112,7 @@ export type CustomToolCallState = {
|
|
|
108
112
|
sourceItemId?: string;
|
|
109
113
|
callId: string;
|
|
110
114
|
input: string;
|
|
115
|
+
inputJson?: GrammarInputBuffer;
|
|
111
116
|
};
|
|
112
117
|
|
|
113
118
|
export type OutputState = ReasoningState | MessageState | FunctionCallState | CustomToolCallState;
|
package/src/tool-activation.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import { installCodexIdentityLifecycle } from "./codex-identity-extension.js";
|
|
9
9
|
import { loadModelSettings } from "./model-catalog/runtime.js";
|
|
10
10
|
import { loadSettings } from "./settings.js";
|
|
11
|
+
import { codeModeOwner, OWNER_CHANGED } from "./code-mode-owner.js";
|
|
11
12
|
|
|
12
13
|
function enableDefinitions(broker: CodexBroker) {
|
|
13
14
|
for (const tool of broker.tools.values()) {
|
|
@@ -40,7 +41,13 @@ export function ensureCodexServices(pi: ExtensionAPI): CodexBroker {
|
|
|
40
41
|
suppressed.clear();
|
|
41
42
|
return active;
|
|
42
43
|
};
|
|
44
|
+
let latest: ExtensionContext | undefined;
|
|
45
|
+
let syncing = false;
|
|
43
46
|
const sync = (ctx: ExtensionContext) => {
|
|
47
|
+
latest = ctx;
|
|
48
|
+
if (syncing) return;
|
|
49
|
+
syncing = true;
|
|
50
|
+
try {
|
|
44
51
|
const settings = loadSettings(ctx.cwd);
|
|
45
52
|
const available = settings.enabled && hasConfiguredModelsLoaded(ctx, settings);
|
|
46
53
|
if (available) enableDefinitions(broker);
|
|
@@ -48,9 +55,17 @@ export function ensureCodexServices(pi: ExtensionAPI): CodexBroker {
|
|
|
48
55
|
const active = new Set(current);
|
|
49
56
|
const capabilities = computeToolCapabilities(ctx.model as ModelLike | undefined, settings);
|
|
50
57
|
const model = loadModelSettings(ctx.model as ModelLike | undefined, ctx.cwd, settings);
|
|
58
|
+
const controls = new Map<string, NonNullable<ReturnType<typeof codeModeOwner>>>();
|
|
51
59
|
for (const name of PACKAGE_TOOL_NAMES) {
|
|
52
60
|
const owned = broker.tools.get(name);
|
|
53
61
|
if (!owned) continue; // Other extensions retain ownership of uninstalled names.
|
|
62
|
+
const control = codeModeOwner(pi, name, owned, broker.codeModeDefinitions?.get(name));
|
|
63
|
+
if (owned.codeModeOwner?.replaced) continue;
|
|
64
|
+
if (control) {
|
|
65
|
+
controls.set(name, control);
|
|
66
|
+
if (control.activeIntent === undefined) continue; // foreign replacement
|
|
67
|
+
if (control.activeIntent) active.add(name); else active.delete(name);
|
|
68
|
+
}
|
|
54
69
|
const hostedWithoutCore = !broker.coreEnabled && (
|
|
55
70
|
(name === "web_search" && model.webSearchImplementation === "hosted")
|
|
56
71
|
|| (name === "image_generation" && model.imageGenerationImplementation === "hosted" && !settings.directImageApiFallback)
|
|
@@ -59,16 +74,28 @@ export function ensureCodexServices(pi: ExtensionAPI): CodexBroker {
|
|
|
59
74
|
if (!desired) active.delete(name);
|
|
60
75
|
else if (settings.autoEnable) active.add(name);
|
|
61
76
|
}
|
|
62
|
-
|
|
77
|
+
const ownsPatch = broker.tools.has("apply_patch") && !broker.tools.get("apply_patch")?.codeModeOwner?.replaced;
|
|
78
|
+
if (ownsPatch && active.has("apply_patch")) {
|
|
63
79
|
for (const name of NATIVE_MUTATION_TOOL_NAMES) {
|
|
64
80
|
if (active.delete(name) && !suppressed.has(name)) suppressed.set(name, current.indexOf(name));
|
|
65
81
|
}
|
|
66
82
|
}
|
|
67
|
-
const
|
|
68
|
-
for (const name of
|
|
69
|
-
|
|
83
|
+
const physical = new Set(active);
|
|
84
|
+
for (const [name, control] of controls) {
|
|
85
|
+
const projected = control.projectActive(active.has(name));
|
|
86
|
+
if (projected === undefined) { if (current.includes(name)) physical.add(name); else physical.delete(name); }
|
|
87
|
+
else if (projected) physical.add(name); else physical.delete(name);
|
|
88
|
+
}
|
|
89
|
+
const next = current.filter(name => physical.has(name));
|
|
90
|
+
for (const name of physical) if (!next.includes(name)) next.push(name);
|
|
91
|
+
if (!ownsPatch || !active.has("apply_patch")) restore(next);
|
|
70
92
|
if (next.join("\0") !== current.join("\0")) pi.setActiveTools(next);
|
|
93
|
+
for (const control of controls.values()) control.reconcile();
|
|
94
|
+
} finally { syncing = false; }
|
|
71
95
|
};
|
|
96
|
+
const offOwner = pi.events.on(OWNER_CHANGED, (message) => {
|
|
97
|
+
if ((message as { version?: number })?.version === 1 && latest && !broker.closed) sync(latest);
|
|
98
|
+
});
|
|
72
99
|
pi.on("session_start", (_event, ctx) => {
|
|
73
100
|
broker.presentation.clear();
|
|
74
101
|
sync(ctx);
|
|
@@ -77,6 +104,8 @@ export function ensureCodexServices(pi: ExtensionAPI): CodexBroker {
|
|
|
77
104
|
pi.on("thinking_level_select", (_event, ctx) => sync(ctx));
|
|
78
105
|
pi.on("agent_end", () => broker.presentation.scheduleFlush());
|
|
79
106
|
pi.on("session_shutdown", () => {
|
|
107
|
+
offOwner();
|
|
108
|
+
for (const tool of broker.tools.values()) tool.codeModeOwner?.control?.dispose();
|
|
80
109
|
try { broker.presentation.flush(); }
|
|
81
110
|
finally {
|
|
82
111
|
broker.presentation.clear();
|