pi-openai-toolkit 0.14.4 → 0.14.6
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 +119 -147
- package/README.zh.md +119 -151
- package/docs/internals.md +42 -21
- package/package.json +127 -127
- package/src/auto-mode/classifier.ts +165 -165
- package/src/auto-mode/policy.ts +146 -146
- package/src/auto-mode/prompt.ts +279 -279
- package/src/auto-mode/reviewer.ts +325 -325
- package/src/auto-mode/transcript.ts +133 -133
- package/src/auto-mode/types.ts +137 -137
- package/src/codex-astra/effort-planner.ts +175 -175
- package/src/codex-astra/extension.ts +122 -122
- package/src/config.ts +621 -592
- package/src/context-management/codex-provider.ts +260 -260
- package/src/context-management/history-notes.ts +357 -325
- package/src/context-management/messages.ts +112 -106
- package/src/context-management/namespace-tools.ts +426 -426
- package/src/context-management/tool-contract.ts +23 -23
- package/src/context-management/tools.ts +306 -311
- package/src/context-management/types.ts +168 -161
- package/src/context-management/window-budget.ts +83 -83
- package/src/context-management/window-manager.ts +60 -51
- package/src/context-management/window-request.ts +125 -125
- package/src/debug.ts +1 -1
- package/src/extension-runtime.ts +34 -9
- package/src/image-generation/artifacts.ts +237 -237
- package/src/image-generation/client.ts +248 -248
- package/src/image-generation/eligibility.ts +18 -18
- package/src/image-generation/extension.ts +19 -2
- package/src/image-generation/protocol.ts +473 -405
- package/src/image-generation/references.ts +194 -194
- package/src/image-generation/render.ts +252 -252
- package/src/image-generation/service.ts +249 -243
- package/src/image-generation/types.ts +189 -184
- package/src/model-scope.ts +17 -17
- package/src/provider-headers.ts +23 -23
- package/src/responses-headers.ts +173 -108
- package/src/runtime.ts +513 -477
- package/src/types.ts +11 -0
- package/src/web-search/extension.ts +80 -80
- package/src/web-search/payload.ts +96 -96
- package/src/web-search/prompt.ts +20 -20
- package/src/web-search/types.ts +45 -45
|
@@ -1,161 +1,168 @@
|
|
|
1
|
-
import type { ContextManagementMode } from "../types";
|
|
2
|
-
|
|
3
|
-
export type { ContextManagementMode };
|
|
4
|
-
|
|
5
|
-
export const CONTEXT_MANAGEMENT_PROTOCOL = 1 as const;
|
|
6
|
-
export const CODEX_CONTEXT_WINDOW_MESSAGE_TYPE = "codex-context-window" as const;
|
|
7
|
-
export const CONTEXT_WINDOW_COMPACTION_STRATEGY = "codex-context-window" as const;
|
|
8
|
-
export const CONTEXT_WINDOW_REMINDER_THRESHOLD = 6_144;
|
|
9
|
-
export const CONTEXT_WINDOW_FALLBACK_BUFFER = 16_384;
|
|
10
|
-
|
|
11
|
-
export type ContextManagementMessageKind = "window" | "reminder" | "fallback";
|
|
12
|
-
|
|
13
|
-
export type ContextWindowIdentity = {
|
|
14
|
-
firstWindowId: string;
|
|
15
|
-
currentWindowId: string;
|
|
16
|
-
previousWindowId?: string;
|
|
17
|
-
windowNumber: number;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export type
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
| "
|
|
101
|
-
| "
|
|
102
|
-
| "
|
|
103
|
-
| "
|
|
104
|
-
|
|
105
|
-
export type
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
|
109
|
-
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
1
|
+
import type { ContextManagementMode } from "../types";
|
|
2
|
+
|
|
3
|
+
export type { ContextManagementMode };
|
|
4
|
+
|
|
5
|
+
export const CONTEXT_MANAGEMENT_PROTOCOL = 1 as const;
|
|
6
|
+
export const CODEX_CONTEXT_WINDOW_MESSAGE_TYPE = "codex-context-window" as const;
|
|
7
|
+
export const CONTEXT_WINDOW_COMPACTION_STRATEGY = "codex-context-window" as const;
|
|
8
|
+
export const CONTEXT_WINDOW_REMINDER_THRESHOLD = 6_144;
|
|
9
|
+
export const CONTEXT_WINDOW_FALLBACK_BUFFER = 16_384;
|
|
10
|
+
|
|
11
|
+
export type ContextManagementMessageKind = "window" | "reminder" | "fallback";
|
|
12
|
+
|
|
13
|
+
export type ContextWindowIdentity = {
|
|
14
|
+
firstWindowId: string;
|
|
15
|
+
currentWindowId: string;
|
|
16
|
+
previousWindowId?: string;
|
|
17
|
+
windowNumber: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** Persisted-session evidence identifying the note that can restore a rollover. */
|
|
21
|
+
export type NotesCheckpointReceipt = {
|
|
22
|
+
path: string;
|
|
23
|
+
toolCallId: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type CodexContextManagementMessageDetails = {
|
|
27
|
+
protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
|
|
28
|
+
id: string;
|
|
29
|
+
sessionId?: string;
|
|
30
|
+
contextManagement: ContextWindowIdentity & {
|
|
31
|
+
protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
|
|
32
|
+
kind: ContextManagementMessageKind;
|
|
33
|
+
trimPreviousWindow?: true;
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ContextWindowCompactionDetails = {
|
|
38
|
+
protocol: typeof CONTEXT_MANAGEMENT_PROTOCOL;
|
|
39
|
+
strategy: typeof CONTEXT_WINDOW_COMPACTION_STRATEGY;
|
|
40
|
+
windowId?: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type RemoteContextFailureReason =
|
|
44
|
+
| "context-management-inactive"
|
|
45
|
+
| "unsupported-model"
|
|
46
|
+
| "unsupported-api"
|
|
47
|
+
| "unsupported-backend"
|
|
48
|
+
| "auth-resolution-failed"
|
|
49
|
+
| "missing-token"
|
|
50
|
+
| "missing-api-key"
|
|
51
|
+
| "missing-account-id"
|
|
52
|
+
| "missing-base-url"
|
|
53
|
+
| "missing-session-id"
|
|
54
|
+
| "invalid-account-token"
|
|
55
|
+
| "backend-timeout"
|
|
56
|
+
| "aborted"
|
|
57
|
+
| "http-error"
|
|
58
|
+
| "invalid-json"
|
|
59
|
+
| "invalid-response"
|
|
60
|
+
| "protocol-error";
|
|
61
|
+
|
|
62
|
+
export type NativeCodexContextProvider = {
|
|
63
|
+
kind: "native-codex";
|
|
64
|
+
route: "openai-codex";
|
|
65
|
+
provider: "openai-codex";
|
|
66
|
+
api: "openai-codex-responses";
|
|
67
|
+
model: string;
|
|
68
|
+
baseUrl: string;
|
|
69
|
+
token: string;
|
|
70
|
+
accountId: string;
|
|
71
|
+
headers: Record<string, string>;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export type GatewayCodexContextProvider = {
|
|
75
|
+
kind: "codex-gateway";
|
|
76
|
+
route: "codex-gateway";
|
|
77
|
+
/** Operator-defined provider name; matched via compaction.gatewayContextModels. */
|
|
78
|
+
provider: string;
|
|
79
|
+
api: "openai-responses";
|
|
80
|
+
model: string;
|
|
81
|
+
baseUrl: string;
|
|
82
|
+
apiKey: string;
|
|
83
|
+
headers: Record<string, string>;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export type CodexContextProvider = NativeCodexContextProvider | GatewayCodexContextProvider;
|
|
87
|
+
|
|
88
|
+
export type CodexContextProviderResolution =
|
|
89
|
+
| { ok: true; provider: CodexContextProvider }
|
|
90
|
+
| {
|
|
91
|
+
ok: false;
|
|
92
|
+
reason: RemoteContextFailureReason;
|
|
93
|
+
provider?: string;
|
|
94
|
+
api?: string;
|
|
95
|
+
model?: string;
|
|
96
|
+
baseUrl?: string;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export type HistoryAction =
|
|
100
|
+
| "list_windows"
|
|
101
|
+
| "list_items"
|
|
102
|
+
| "read_item"
|
|
103
|
+
| "search_contents";
|
|
104
|
+
|
|
105
|
+
export type NotesAction =
|
|
106
|
+
| "list_files_by_prefix"
|
|
107
|
+
| "read_file"
|
|
108
|
+
| "search_contents"
|
|
109
|
+
| "append_to_file"
|
|
110
|
+
| "write_file";
|
|
111
|
+
|
|
112
|
+
export type HistoryNotesResponse = Record<string, unknown>;
|
|
113
|
+
|
|
114
|
+
export type HistoryNotesResult =
|
|
115
|
+
| { ok: true; value: HistoryNotesResponse }
|
|
116
|
+
| {
|
|
117
|
+
ok: false;
|
|
118
|
+
reason: Exclude<RemoteContextFailureReason, "context-management-inactive" | "unsupported-model" | "unsupported-api">;
|
|
119
|
+
status?: number;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
123
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function isNonEmptyString(value: unknown): value is string {
|
|
127
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function isContextWindowIdentity(value: unknown): value is ContextWindowIdentity {
|
|
131
|
+
if (!isRecord(value)) return false;
|
|
132
|
+
return (
|
|
133
|
+
isNonEmptyString(value.firstWindowId) &&
|
|
134
|
+
isNonEmptyString(value.currentWindowId) &&
|
|
135
|
+
(value.previousWindowId === undefined || isNonEmptyString(value.previousWindowId)) &&
|
|
136
|
+
Number.isInteger(value.windowNumber) &&
|
|
137
|
+
(value.windowNumber as number) >= 0
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function isCodexContextManagementMessageDetails(
|
|
142
|
+
value: unknown,
|
|
143
|
+
): value is CodexContextManagementMessageDetails {
|
|
144
|
+
if (!isRecord(value) || value.protocol !== CONTEXT_MANAGEMENT_PROTOCOL || !isNonEmptyString(value.id)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (value.sessionId !== undefined && !isNonEmptyString(value.sessionId)) return false;
|
|
148
|
+
if (!isRecord(value.contextManagement) || value.contextManagement.protocol !== CONTEXT_MANAGEMENT_PROTOCOL) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
const context = value.contextManagement;
|
|
152
|
+
if (!isRecord(context)) return false;
|
|
153
|
+
const trimPreviousWindow = context.trimPreviousWindow;
|
|
154
|
+
return (
|
|
155
|
+
(context.kind === "window" || context.kind === "reminder" || context.kind === "fallback") &&
|
|
156
|
+
isContextWindowIdentity(context) &&
|
|
157
|
+
(trimPreviousWindow === undefined || trimPreviousWindow === true)
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function isContextWindowCompactionDetails(value: unknown): value is ContextWindowCompactionDetails {
|
|
162
|
+
return (
|
|
163
|
+
isRecord(value) &&
|
|
164
|
+
value.protocol === CONTEXT_MANAGEMENT_PROTOCOL &&
|
|
165
|
+
value.strategy === CONTEXT_WINDOW_COMPACTION_STRATEGY &&
|
|
166
|
+
(value.windowId === undefined || isNonEmptyString(value.windowId))
|
|
167
|
+
);
|
|
168
|
+
}
|
|
@@ -1,83 +1,83 @@
|
|
|
1
|
-
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import {
|
|
3
|
-
CONTEXT_WINDOW_FALLBACK_BUFFER,
|
|
4
|
-
CONTEXT_WINDOW_FALLBACK_MESSAGE,
|
|
5
|
-
renderContextWindowReminder,
|
|
6
|
-
type ContextManagementMessageKind,
|
|
7
|
-
type ContextWindowIdentity,
|
|
8
|
-
} from "./messages";
|
|
9
|
-
|
|
10
|
-
export interface ContextRemaining {
|
|
11
|
-
remainingTokens: number | undefined;
|
|
12
|
-
windowId: string | undefined;
|
|
13
|
-
contextWindow: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class ContextWindowBudget {
|
|
17
|
-
private readonly remindedWindows = new Set<string>();
|
|
18
|
-
private readonly exhaustedWindows = new Set<string>();
|
|
19
|
-
|
|
20
|
-
reset(): void {
|
|
21
|
-
this.remindedWindows.clear();
|
|
22
|
-
this.exhaustedWindows.clear();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
restore(kind: ContextManagementMessageKind, windowId: string): void {
|
|
26
|
-
if (kind === "reminder") this.remindedWindows.add(windowId);
|
|
27
|
-
if (kind === "fallback") this.exhaustedWindows.add(windowId);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
record(
|
|
31
|
-
ctx: ExtensionContext,
|
|
32
|
-
identity: ContextWindowIdentity,
|
|
33
|
-
contextTokens: number | undefined,
|
|
34
|
-
contextReminderThresholdPercent: number,
|
|
35
|
-
): { content: string; kind: "fallback" | "reminder" } | undefined {
|
|
36
|
-
const remaining = this.remaining(ctx, identity, contextTokens);
|
|
37
|
-
if (remaining.remainingTokens === undefined) return undefined;
|
|
38
|
-
const windowId = identity.currentWindowId;
|
|
39
|
-
if (remaining.remainingTokens <= 0 && !this.exhaustedWindows.has(windowId)) {
|
|
40
|
-
this.exhaustedWindows.add(windowId);
|
|
41
|
-
return { content: CONTEXT_WINDOW_FALLBACK_MESSAGE, kind: "fallback" };
|
|
42
|
-
}
|
|
43
|
-
const reminderThreshold =
|
|
44
|
-
contextReminderThresholdPercent > 0
|
|
45
|
-
? Math.floor((remaining.contextWindow * contextReminderThresholdPercent) / 100)
|
|
46
|
-
: 0;
|
|
47
|
-
if (
|
|
48
|
-
contextReminderThresholdPercent > 0 &&
|
|
49
|
-
remaining.remainingTokens <= reminderThreshold &&
|
|
50
|
-
!this.remindedWindows.has(windowId)
|
|
51
|
-
) {
|
|
52
|
-
this.remindedWindows.add(windowId);
|
|
53
|
-
return { content: renderContextWindowReminder(remaining.remainingTokens), kind: "reminder" };
|
|
54
|
-
}
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
remaining(
|
|
59
|
-
ctx: ExtensionContext,
|
|
60
|
-
identity: ContextWindowIdentity | undefined,
|
|
61
|
-
contextTokens?: number,
|
|
62
|
-
): ContextRemaining {
|
|
63
|
-
const usage = ctx.getContextUsage();
|
|
64
|
-
const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow ?? 0;
|
|
65
|
-
const limit = Math.max(0, contextWindow - CONTEXT_WINDOW_FALLBACK_BUFFER);
|
|
66
|
-
if (usage === undefined && contextTokens === undefined) {
|
|
67
|
-
return {
|
|
68
|
-
remainingTokens: undefined,
|
|
69
|
-
windowId: identity?.currentWindowId,
|
|
70
|
-
contextWindow: limit,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
const used = contextTokens ?? usage?.tokens;
|
|
74
|
-
return {
|
|
75
|
-
remainingTokens:
|
|
76
|
-
used === null || used === undefined
|
|
77
|
-
? undefined
|
|
78
|
-
: Math.max(0, limit - used),
|
|
79
|
-
windowId: identity?.currentWindowId,
|
|
80
|
-
contextWindow: limit,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
CONTEXT_WINDOW_FALLBACK_BUFFER,
|
|
4
|
+
CONTEXT_WINDOW_FALLBACK_MESSAGE,
|
|
5
|
+
renderContextWindowReminder,
|
|
6
|
+
type ContextManagementMessageKind,
|
|
7
|
+
type ContextWindowIdentity,
|
|
8
|
+
} from "./messages";
|
|
9
|
+
|
|
10
|
+
export interface ContextRemaining {
|
|
11
|
+
remainingTokens: number | undefined;
|
|
12
|
+
windowId: string | undefined;
|
|
13
|
+
contextWindow: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export class ContextWindowBudget {
|
|
17
|
+
private readonly remindedWindows = new Set<string>();
|
|
18
|
+
private readonly exhaustedWindows = new Set<string>();
|
|
19
|
+
|
|
20
|
+
reset(): void {
|
|
21
|
+
this.remindedWindows.clear();
|
|
22
|
+
this.exhaustedWindows.clear();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
restore(kind: ContextManagementMessageKind, windowId: string): void {
|
|
26
|
+
if (kind === "reminder") this.remindedWindows.add(windowId);
|
|
27
|
+
if (kind === "fallback") this.exhaustedWindows.add(windowId);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
record(
|
|
31
|
+
ctx: ExtensionContext,
|
|
32
|
+
identity: ContextWindowIdentity,
|
|
33
|
+
contextTokens: number | undefined,
|
|
34
|
+
contextReminderThresholdPercent: number,
|
|
35
|
+
): { content: string; kind: "fallback" | "reminder" } | undefined {
|
|
36
|
+
const remaining = this.remaining(ctx, identity, contextTokens);
|
|
37
|
+
if (remaining.remainingTokens === undefined) return undefined;
|
|
38
|
+
const windowId = identity.currentWindowId;
|
|
39
|
+
if (remaining.remainingTokens <= 0 && !this.exhaustedWindows.has(windowId)) {
|
|
40
|
+
this.exhaustedWindows.add(windowId);
|
|
41
|
+
return { content: CONTEXT_WINDOW_FALLBACK_MESSAGE, kind: "fallback" };
|
|
42
|
+
}
|
|
43
|
+
const reminderThreshold =
|
|
44
|
+
contextReminderThresholdPercent > 0
|
|
45
|
+
? Math.floor((remaining.contextWindow * contextReminderThresholdPercent) / 100)
|
|
46
|
+
: 0;
|
|
47
|
+
if (
|
|
48
|
+
contextReminderThresholdPercent > 0 &&
|
|
49
|
+
remaining.remainingTokens <= reminderThreshold &&
|
|
50
|
+
!this.remindedWindows.has(windowId)
|
|
51
|
+
) {
|
|
52
|
+
this.remindedWindows.add(windowId);
|
|
53
|
+
return { content: renderContextWindowReminder(remaining.remainingTokens), kind: "reminder" };
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
remaining(
|
|
59
|
+
ctx: ExtensionContext,
|
|
60
|
+
identity: ContextWindowIdentity | undefined,
|
|
61
|
+
contextTokens?: number,
|
|
62
|
+
): ContextRemaining {
|
|
63
|
+
const usage = ctx.getContextUsage();
|
|
64
|
+
const contextWindow = usage?.contextWindow ?? ctx.model?.contextWindow ?? 0;
|
|
65
|
+
const limit = Math.max(0, contextWindow - CONTEXT_WINDOW_FALLBACK_BUFFER);
|
|
66
|
+
if (usage === undefined && contextTokens === undefined) {
|
|
67
|
+
return {
|
|
68
|
+
remainingTokens: undefined,
|
|
69
|
+
windowId: identity?.currentWindowId,
|
|
70
|
+
contextWindow: limit,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const used = contextTokens ?? usage?.tokens;
|
|
74
|
+
return {
|
|
75
|
+
remainingTokens:
|
|
76
|
+
used === null || used === undefined
|
|
77
|
+
? undefined
|
|
78
|
+
: Math.max(0, limit - used),
|
|
79
|
+
windowId: identity?.currentWindowId,
|
|
80
|
+
contextWindow: limit,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|