@mupt-ai/dari-gateway 1.0.319 → 1.0.321
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 +1 -1
- package/dist/ingress/anthropic_messages.js +14 -2
- package/dist/interaction/capabilities.d.ts +3 -1
- package/dist/interaction/capabilities.js +10 -0
- package/dist/interaction/types.d.ts +5 -3
- package/dist/link/endpoints.js +23 -4
- package/dist/link/self_hosted.js +2 -0
- package/dist/link/service.d.ts +6 -0
- package/dist/link/service.js +3 -0
- package/dist/protocol/input/anthropic_messages.js +18 -1
- package/dist/protocol/input/router_model.js +1 -1
- package/dist/protocol/output/anthropic_messages_output.js +7 -0
- package/dist/protocol/output/anthropic_messages_stream.js +29 -9
- package/dist/providers/anthropic_safeguards.d.ts +1 -0
- package/dist/providers/anthropic_safeguards.js +14 -7
- package/dist/providers/anthropic_web_search.d.ts +16 -0
- package/dist/providers/anthropic_web_search.js +67 -0
- package/dist/providers/pi.js +15 -4
- package/dist/providers/pi_context.js +15 -1
- package/dist/providers/pi_options.js +10 -0
- package/dist/providers/pi_output.js +13 -0
- package/dist/self_host/subscriptions/http.js +53 -7
- package/package.json +2 -2
- package/src/ingress/anthropic_messages.ts +14 -2
- package/src/interaction/capabilities.ts +16 -1
- package/src/interaction/types.ts +10 -3
- package/src/link/endpoints.ts +25 -4
- package/src/link/self_hosted.ts +2 -0
- package/src/link/service.ts +9 -0
- package/src/protocol/input/anthropic_messages.ts +17 -1
- package/src/protocol/input/router_model.ts +1 -1
- package/src/protocol/output/anthropic_messages_output.ts +6 -0
- package/src/protocol/output/anthropic_messages_stream.ts +30 -7
- package/src/providers/anthropic_safeguards.ts +19 -9
- package/src/providers/anthropic_web_search.ts +99 -0
- package/src/providers/pi.ts +23 -4
- package/src/providers/pi_context.ts +14 -1
- package/src/providers/pi_options.ts +9 -0
- package/src/providers/pi_output.ts +13 -0
- package/src/self_host/subscriptions/http.ts +55 -7
|
@@ -200,7 +200,9 @@ export function buildContext(
|
|
|
200
200
|
continue;
|
|
201
201
|
}
|
|
202
202
|
if (item.type === "hosted_tool_call") {
|
|
203
|
-
if (item.tool === "
|
|
203
|
+
if (item.tool === "web_search_result" || (item.tool === "server_tool_use" && item.serverToolName === "web_search")) {
|
|
204
|
+
assistantFor(item.source ?? target).content.push({ type: "text", text: webSearchHistoryText(item) });
|
|
205
|
+
} else if (item.tool === "server_tool_use" || item.tool === "tool_search") {
|
|
204
206
|
// Server tool use and tool search results are Anthropic-specific.
|
|
205
207
|
// Handle them before the generic OpenAI Responses branch to avoid
|
|
206
208
|
// emitting Anthropic-shaped payloads as Responses provider items.
|
|
@@ -407,6 +409,17 @@ function portableReasoningText(
|
|
|
407
409
|
return readable;
|
|
408
410
|
}
|
|
409
411
|
|
|
412
|
+
// Pi cannot replay Anthropic's web search blocks, so a past search goes back
|
|
413
|
+
// as text: its query, then each result's title and URL.
|
|
414
|
+
function webSearchHistoryText(item: Extract<InteractionItem, { type: "hosted_tool_call" }>): string {
|
|
415
|
+
if (item.tool === "server_tool_use") return `[Web search: ${JSON.stringify(item.serverToolInput ?? {})}]`;
|
|
416
|
+
const content = item.payload.content;
|
|
417
|
+
const results = Array.isArray(content)
|
|
418
|
+
? content.filter(isRecord).map((result) => `${String(result.title ?? "")} ${String(result.url ?? "")}`.trim())
|
|
419
|
+
: [JSON.stringify(content)];
|
|
420
|
+
return `[Web search results: ${results.join("; ")}]`;
|
|
421
|
+
}
|
|
422
|
+
|
|
410
423
|
function readableContent(content: InteractionContent[]): string {
|
|
411
424
|
return content.map((part) => (part.type === "text" ? part.text : "(image omitted)")).join("\n");
|
|
412
425
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { BedrockOptions } from "@mupt-ai/pi-ai/api/bedrock-converse-stream";
|
|
2
2
|
import { isReasoningEffort, isRecord, type ReasoningEffort } from "@mupt-ai/dari-router/policy-engine";
|
|
3
|
+
import { isBackgroundRequest } from "../interaction/types.js";
|
|
3
4
|
import type { InteractionRequest, InteractionResponseFormat, InteractionToolChoice } from "../interaction/types.js";
|
|
4
5
|
import { requiredCapabilitiesFor } from "../interaction/capabilities.js";
|
|
5
6
|
import { toolWireName, toolsByWireName } from "../interaction/tool_identity.js";
|
|
@@ -15,6 +16,7 @@ import {
|
|
|
15
16
|
payloadWithContextManagement,
|
|
16
17
|
payloadWithSafeguards,
|
|
17
18
|
} from "./anthropic_safeguards.js";
|
|
19
|
+
import { anthropicWebSearchFor, payloadWithWebSearchTool } from "./anthropic_web_search.js";
|
|
18
20
|
|
|
19
21
|
type PiOptionsSupport = {
|
|
20
22
|
getSupportedThinkingLevels?: (model: Record<string, unknown>) => string[];
|
|
@@ -123,6 +125,13 @@ export function buildOptions(
|
|
|
123
125
|
if (contextManagement !== undefined && forwardsAnthropicContextManagement(request, model)) {
|
|
124
126
|
patches.push((payload) => payloadWithContextManagement(payload, contextManagement));
|
|
125
127
|
}
|
|
128
|
+
const webSearch = anthropicWebSearchFor(request, model);
|
|
129
|
+
if (webSearch !== undefined) {
|
|
130
|
+
patches.push((payload) => payloadWithWebSearchTool(payload, webSearch));
|
|
131
|
+
// A background search is one request whose results are read once. Like
|
|
132
|
+
// Claude Code against Anthropic directly, send it without prompt caching.
|
|
133
|
+
if (isBackgroundRequest(request)) options.cacheRetention = "none";
|
|
134
|
+
}
|
|
126
135
|
|
|
127
136
|
// Pi keeps only the content it models, so native compaction output reaches
|
|
128
137
|
// the client from the provider's own output items.
|
|
@@ -98,6 +98,19 @@ export function piMessageToInteractionOutput(
|
|
|
98
98
|
});
|
|
99
99
|
continue;
|
|
100
100
|
}
|
|
101
|
+
if (block.type === "webSearchResult") {
|
|
102
|
+
const toolUseId = requiredString(block.toolUseId, "web search result tool_use_id");
|
|
103
|
+
items.push({
|
|
104
|
+
type: "hosted_tool_call",
|
|
105
|
+
id: `${toolUseId}_result`,
|
|
106
|
+
tool: "web_search_result",
|
|
107
|
+
providerType: "web_search_tool_result",
|
|
108
|
+
payload: { tool_use_id: toolUseId, content: block.content },
|
|
109
|
+
serverToolUseId: toolUseId,
|
|
110
|
+
source,
|
|
111
|
+
});
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
101
114
|
throw invalidProviderOutput(`content block type '${String(block.type ?? "unknown")}' is not supported.`);
|
|
102
115
|
}
|
|
103
116
|
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
type SubscriptionProvider,
|
|
15
15
|
} from "./oauth.js";
|
|
16
16
|
import { canonicalModelName } from "../../models/canonical.js";
|
|
17
|
+
import type { SubscriptionUsageWindow } from "../../providers/subscription_limits.js";
|
|
17
18
|
import {
|
|
18
19
|
activeModelHolds,
|
|
19
20
|
currentUsage,
|
|
@@ -174,13 +175,60 @@ function summary(store: SubscriptionStore, credential: SubscriptionCredential, n
|
|
|
174
175
|
return credentialBody(credential, store.activeCredential(credential.provider, now)?.id === credential.id, now);
|
|
175
176
|
}
|
|
176
177
|
|
|
177
|
-
|
|
178
|
+
const FIVE_HOURS = 5 * 60;
|
|
179
|
+
const ONE_WEEK = 7 * 24 * 60;
|
|
180
|
+
const ONE_MONTH = 30 * 24 * 60;
|
|
181
|
+
|
|
182
|
+
// A model family's own window is named for the family's catalog models.
|
|
183
|
+
const MODEL_FAMILY_NAMES = { fable: "Claude Fable" } as const;
|
|
184
|
+
|
|
185
|
+
/** What to call a window: 5 Hour, Weekly, Monthly, Claude Fable Weekly. */
|
|
186
|
+
function usageWindowLabel(window: SubscriptionUsageWindow): string {
|
|
187
|
+
const minutes = window.minutes;
|
|
188
|
+
const length = minutes === ONE_WEEK ? "Weekly"
|
|
189
|
+
: minutes === ONE_MONTH ? "Monthly"
|
|
190
|
+
: minutes % (24 * 60) === 0 ? `${minutes / (24 * 60)} Day`
|
|
191
|
+
: minutes % 60 === 0 ? `${minutes / 60} Hour`
|
|
192
|
+
: `${minutes} Minute`;
|
|
193
|
+
return window.modelFamily === undefined ? length : `${MODEL_FAMILY_NAMES[window.modelFamily]} ${length}`;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The usage to show, as the hosted API shows it: every running window, a
|
|
198
|
+
* held plan's window full. A hold ends when the window that ran out resets,
|
|
199
|
+
* so the shared window resetting with it shows full. Usage reported without
|
|
200
|
+
* per-window detail, and holds without a matching window, are named by how
|
|
201
|
+
* far off their reset is: only a weekly or monthly window resets more than
|
|
202
|
+
* five hours out, and only OpenCode Go's monthly one more than a week.
|
|
203
|
+
*/
|
|
178
204
|
function usageBody(credential: SubscriptionCredential, now: Date) {
|
|
205
|
+
const inferred = (resetsAt: Date) => {
|
|
206
|
+
const left = resetsAt.getTime() - now.getTime();
|
|
207
|
+
if (left > ONE_WEEK * 60_000) return ONE_MONTH;
|
|
208
|
+
return left > FIVE_HOURS * 60_000 ? ONE_WEEK : FIVE_HOURS;
|
|
209
|
+
};
|
|
179
210
|
const usage = currentUsage(credential, now);
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
211
|
+
let windows: SubscriptionUsageWindow[] = usage === null ? []
|
|
212
|
+
: usage.windows.length > 0 ? usage.windows
|
|
213
|
+
: [{ minutes: inferred(usage.resetsAt), utilization: usage.utilization, resetsAt: usage.resetsAt }];
|
|
214
|
+
if (isHeld(credential, now)) {
|
|
215
|
+
const held = credential.unavailableUntil!;
|
|
216
|
+
const gap = (window: SubscriptionUsageWindow) => Math.abs(window.resetsAt.getTime() - held.getTime());
|
|
217
|
+
// The hold is the plan's; it never fills a model family's window.
|
|
218
|
+
const closest = windows.filter((window) => window.modelFamily === undefined)
|
|
219
|
+
.reduce<SubscriptionUsageWindow | null>((best, window) => best === null || gap(window) < gap(best) ? window : best, null);
|
|
220
|
+
const minutes = closest !== null && gap(closest) < 15 * 60_000 ? closest.minutes : inferred(held);
|
|
221
|
+
windows = [
|
|
222
|
+
...windows.filter((window) => window.modelFamily !== undefined || window.minutes !== minutes),
|
|
223
|
+
{ minutes, utilization: 1, resetsAt: held },
|
|
224
|
+
].sort((a, b) => a.minutes - b.minutes || (a.modelFamily ?? "").localeCompare(b.modelFamily ?? ""));
|
|
225
|
+
}
|
|
226
|
+
if (windows.length === 0) return null;
|
|
227
|
+
return {
|
|
228
|
+
utilization: usage?.utilization ?? 1,
|
|
229
|
+
resets_at: (usage?.resetsAt ?? windows[0]!.resetsAt).toISOString(),
|
|
230
|
+
windows: windows.map((window) => ({
|
|
231
|
+
label: usageWindowLabel(window),
|
|
184
232
|
...(window.modelFamily === undefined ? {} : { model_family: window.modelFamily }),
|
|
185
233
|
minutes: window.minutes,
|
|
186
234
|
utilization: window.utilization,
|
|
@@ -212,8 +260,8 @@ function credentialBody(credential: SubscriptionCredential, active: boolean, now
|
|
|
212
260
|
reason: hold.reason,
|
|
213
261
|
})),
|
|
214
262
|
subscription_active: active,
|
|
215
|
-
// The share of its tightest window the plan last reported used,
|
|
216
|
-
//
|
|
263
|
+
// The share of its tightest window the plan last reported used, and
|
|
264
|
+
// every window as shown; null when it reports none and is not held.
|
|
217
265
|
usage: usageBody(credential, now),
|
|
218
266
|
// Spending banked limit resets is hosted-only.
|
|
219
267
|
auto_redeem_resets: false,
|