@zergai/ztc 0.26.2-dev.202611210149 → 0.26.4-dev.202611231953
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 +590 -28
- package/bin/zmux-darwin-arm64 +0 -0
- package/bin/zmux-linux-x64 +0 -0
- package/bin/ztc-audio-darwin-arm64 +0 -0
- package/bin/ztc-capture-darwin-arm64 +0 -0
- package/bin/ztc-tui-darwin-arm64 +0 -0
- package/bin/ztc-tui-linux-x64 +0 -0
- package/bin/zterm-darwin-arm64 +2 -2
- package/dist/App.js +18 -1
- package/dist/agent/agent.d.ts +25 -1
- package/dist/agent/agent.js +26 -1
- package/dist/agent/attached_images.d.ts +58 -0
- package/dist/agent/attached_images.js +145 -0
- package/dist/agent/backends/anthropic.d.ts +14 -0
- package/dist/agent/backends/anthropic.js +49 -2
- package/dist/agent/backends/codex_app_server.d.ts +3 -1
- package/dist/agent/backends/codex_app_server.js +9 -6
- package/dist/agent/backends/codex_auth.js +3 -0
- package/dist/agent/backends/gemini.js +3 -0
- package/dist/agent/backends/host_effort_policy.js +14 -2
- package/dist/agent/backends/http_error.d.ts +17 -0
- package/dist/agent/backends/http_error.js +50 -1
- package/dist/agent/backends/index.js +2 -1
- package/dist/agent/backends/model_redirect.d.ts +47 -0
- package/dist/agent/backends/model_redirect.js +97 -0
- package/dist/agent/backends/openai_compatible.js +5 -0
- package/dist/agent/backends/openai_responses.d.ts +9 -1
- package/dist/agent/backends/openai_responses.js +21 -12
- package/dist/agent/backends/prompt_cache.d.ts +8 -0
- package/dist/agent/backends/prompt_cache.js +10 -0
- package/dist/agent/backends/provider_ids.d.ts +1 -1
- package/dist/agent/backends/provider_ids.js +1 -1
- package/dist/agent/backends/types.d.ts +8 -0
- package/dist/agent/backends/zerg-api.js +5 -1
- package/dist/agent/backends/zrouter_stream.d.ts +75 -9
- package/dist/agent/backends/zrouter_stream.js +250 -49
- package/dist/agent/commands/config.js +51 -4
- package/dist/agent/commands/connect.js +20 -0
- package/dist/agent/commands/credential_status.js +5 -3
- package/dist/agent/commands/debug.js +3 -0
- package/dist/agent/commands/diff.js +9 -5
- package/dist/agent/commands/emulation.js +7 -1
- package/dist/agent/commands/inbox.js +4 -3
- package/dist/agent/commands/mcp.js +3 -0
- package/dist/agent/commands/models.js +20 -3
- package/dist/agent/commands/permissions.js +16 -2
- package/dist/agent/commands/provider_selection.js +13 -6
- package/dist/agent/commands/settings.js +15 -4
- package/dist/agent/commands/status.js +23 -1
- package/dist/agent/commands/types.d.ts +34 -3
- package/dist/agent/commands/update.d.ts +2 -1
- package/dist/agent/commands/update.js +8 -3
- package/dist/agent/commands/zerg.d.ts +6 -0
- package/dist/agent/commands/zerg.js +152 -28
- package/dist/agent/commands/ztc_pane.js +8 -5
- package/dist/agent/factory.js +22 -4
- package/dist/agent/request_encoder.js +11 -2
- package/dist/agent/runtime/command_jobs.js +16 -1
- package/dist/agent/runtime/command_panes.js +4 -1
- package/dist/agent/runtime/execution_context.d.ts +8 -1
- package/dist/agent/runtime/execution_context.js +8 -2
- package/dist/agent/runtime/policy.d.ts +66 -5
- package/dist/agent/runtime/policy.js +161 -33
- package/dist/agent/runtime/remote_broker_transport.d.ts +7 -0
- package/dist/agent/runtime/remote_broker_transport.js +33 -3
- package/dist/agent/runtime/remote_resource_errors.d.ts +7 -0
- package/dist/agent/runtime/remote_resource_errors.js +10 -0
- package/dist/agent/runtime/remote_resources.d.ts +1 -1
- package/dist/agent/runtime/remote_resources.js +7 -3
- package/dist/agent/runtime/source_verification.js +15 -12
- package/dist/agent/runtime/tool_runtime.js +165 -58
- package/dist/agent/runtime/verifier_capture.d.ts +1 -1
- package/dist/agent/runtime/verifier_capture.js +6 -0
- package/dist/agent/runtime/work_base.js +46 -14
- package/dist/agent/tools/adaptation.js +29 -12
- package/dist/agent/tools/apply_patch.js +62 -5
- package/dist/agent/tools/background.js +2 -0
- package/dist/agent/tools/benchmark_cpu.js +2 -0
- package/dist/agent/tools/bus_message.d.ts +4 -2
- package/dist/agent/tools/bus_message.js +21 -3
- package/dist/agent/tools/delegate.js +2 -0
- package/dist/agent/tools/file.d.ts +12 -1
- package/dist/agent/tools/file.js +92 -11
- package/dist/agent/tools/lsp.d.ts +12 -0
- package/dist/agent/tools/lsp.js +61 -5
- package/dist/agent/tools/pane.js +20 -45
- package/dist/agent/tools/project_context.js +5 -2
- package/dist/agent/tools/routine.js +20 -4
- package/dist/agent/tools/screenshot.d.ts +8 -0
- package/dist/agent/tools/screenshot.js +39 -25
- package/dist/agent/tools/search.js +31 -3
- package/dist/agent/tools/shell.js +7 -1
- package/dist/agent/tools/supervision.js +6 -0
- package/dist/agent/tools/types.d.ts +18 -0
- package/dist/agent/tools/zerg.js +15 -19
- package/dist/agent/tools/ztc_command.d.ts +15 -4
- package/dist/agent/tools/ztc_command.js +48 -34
- package/dist/app/agent_runner.js +8 -6
- package/dist/app/conversation_compaction.d.ts +6 -3
- package/dist/app/conversation_compaction.js +6 -3
- package/dist/app/tool_callbacks.js +1 -0
- package/dist/audio/sfx.js +6 -0
- package/dist/batch/worktree.js +22 -10
- package/dist/board/client.js +2 -1
- package/dist/cli_main.d.ts +1 -0
- package/dist/cli_main.js +141 -45
- package/dist/cli_ratatui.d.ts +49 -4
- package/dist/cli_ratatui.js +355 -79
- package/dist/conductor_branch.js +4 -1
- package/dist/config/host_credentials.d.ts +157 -0
- package/dist/config/host_credentials.js +589 -0
- package/dist/config/model_pin.d.ts +111 -0
- package/dist/config/model_pin.js +182 -0
- package/dist/config/own_state_files.d.ts +35 -0
- package/dist/config/own_state_files.js +161 -0
- package/dist/config/permission_pin.d.ts +148 -0
- package/dist/config/permission_pin.js +203 -0
- package/dist/config/permissions.d.ts +9 -2
- package/dist/config/permissions.js +14 -3
- package/dist/config/platform_login.d.ts +72 -0
- package/dist/config/platform_login.js +111 -0
- package/dist/config/profile_limits.d.ts +34 -0
- package/dist/config/profile_limits.js +66 -0
- package/dist/config/provider_api_key_env.js +3 -2
- package/dist/config/provider_models.d.ts +6 -1
- package/dist/config/provider_models.js +38 -11
- package/dist/config/repo_config.d.ts +22 -0
- package/dist/config/repo_config.js +77 -3
- package/dist/config/router_runtime_grant.d.ts +53 -0
- package/dist/config/router_runtime_grant.js +131 -0
- package/dist/config/settings_merge.d.ts +22 -0
- package/dist/config/settings_merge.js +75 -0
- package/dist/config/types.d.ts +8 -0
- package/dist/config.d.ts +334 -4
- package/dist/config.js +1024 -41
- package/dist/context/project_intelligence.d.ts +11 -2
- package/dist/context/project_intelligence.js +38 -15
- package/dist/desktop/daily/desktop_wiring.js +8 -6
- package/dist/desktop/daily/native_pane_launcher.js +2 -1
- package/dist/desktop/daily/native_tool.d.ts +1 -1
- package/dist/desktop/daily/native_tool.js +4 -3
- package/dist/desktop/daily/process_identity.js +10 -2
- package/dist/desktop/daily/source_baseline.js +4 -3
- package/dist/diagnostics/native_terminal_benchmark.js +3 -2
- package/dist/diagnostics/session_export.js +27 -14
- package/dist/embedded/anempa_runtime.js +3 -3
- package/dist/embedded/gameboy_runtime.js +5 -4
- package/dist/embedded/zergpod_runtime.js +5 -4
- package/dist/emulation/catalog.js +6 -1
- package/dist/emulation/tool_adapters.js +13 -3
- package/dist/environment/store.js +5 -4
- package/dist/extension_discovery.d.ts +12 -2
- package/dist/extension_discovery.js +52 -7
- package/dist/headless.d.ts +8 -0
- package/dist/headless.js +52 -13
- package/dist/headless_cli.js +16 -1
- package/dist/headless_workflow_trigger.js +1 -1
- package/dist/hooks/dispatcher.d.ts +8 -0
- package/dist/hooks/dispatcher.js +22 -2
- package/dist/hooks/lifecycle.d.ts +6 -0
- package/dist/hooks/lifecycle.js +1 -0
- package/dist/hooks/program_files.d.ts +10 -0
- package/dist/hooks/program_files.js +175 -0
- package/dist/hooks/runner.d.ts +11 -1
- package/dist/hooks/runner.js +112 -11
- package/dist/hooks/types.d.ts +2 -0
- package/dist/host_credentials_startup.d.ts +6 -0
- package/dist/host_credentials_startup.js +17 -0
- package/dist/instance_registry.d.ts +6 -2
- package/dist/instance_registry.js +9 -15
- package/dist/intake/format.d.ts +6 -0
- package/dist/intake/format.js +9 -0
- package/dist/intake/notifications.js +2 -1
- package/dist/lsp/manager.d.ts +1 -1
- package/dist/lsp/manager.js +4 -5
- package/dist/mcp/client.d.ts +2 -0
- package/dist/mcp/client.js +116 -18
- package/dist/mcp/registry.d.ts +12 -0
- package/dist/mcp/registry.js +15 -1
- package/dist/modes/handlers.js +14 -5
- package/dist/panes/agent_cli.d.ts +23 -1
- package/dist/panes/agent_cli.js +96 -1
- package/dist/panes/agent_panes.d.ts +27 -4
- package/dist/panes/agent_panes.js +73 -11
- package/dist/panes/editor_program.d.ts +8 -0
- package/dist/panes/editor_program.js +25 -0
- package/dist/panes/motsi.d.ts +11 -1
- package/dist/panes/motsi.js +14 -1
- package/dist/panes/pane_registry.d.ts +28 -1
- package/dist/panes/pane_registry.js +25 -0
- package/dist/panes/worker_route.js +5 -3
- package/dist/panes/ztc_launch.d.ts +8 -0
- package/dist/panes/ztc_launch.js +26 -1
- package/dist/profile_baseline.js +2 -0
- package/dist/router_auth.d.ts +8 -3
- package/dist/router_auth.js +34 -2
- package/dist/runtime_info.js +56 -38
- package/dist/session.js +12 -2
- package/dist/shannon/runtime.js +13 -1
- package/dist/simulation/domains/loader.d.ts +2 -1
- package/dist/simulation/domains/loader.js +19 -2
- package/dist/simulation/living/context.d.ts +8 -8
- package/dist/simulation/living/types.d.ts +2 -2
- package/dist/simulation/memory/embedding.d.ts +2 -1
- package/dist/simulation/memory/embedding.js +25 -6
- package/dist/terminal_attach.js +2 -1
- package/dist/types.d.ts +21 -0
- package/dist/ui/ratatui/headless_pane_manager.d.ts +12 -6
- package/dist/ui/ratatui/headless_pane_manager.js +65 -21
- package/dist/ui/ratatui/ipc_path.d.ts +41 -2
- package/dist/ui/ratatui/ipc_path.js +164 -5
- package/dist/ui/ratatui/pane_credential_broker.d.ts +17 -4
- package/dist/ui/ratatui/pane_credential_broker.js +23 -5
- package/dist/ui/ratatui/pane_relay.d.ts +74 -0
- package/dist/ui/ratatui/pane_relay.js +188 -13
- package/dist/ui/ratatui/process.d.ts +15 -1
- package/dist/ui/ratatui/process.js +129 -163
- package/dist/ui/ratatui/protocol.d.ts +11 -1
- package/dist/ui/ratatui/protocol.js +6 -0
- package/dist/ui/ratatui/ztc_launch_trust.d.ts +87 -0
- package/dist/ui/ratatui/ztc_launch_trust.js +249 -0
- package/dist/utils/admitted_open.d.ts +40 -0
- package/dist/utils/admitted_open.js +111 -0
- package/dist/utils/clipboard.js +2 -1
- package/dist/utils/computer_native.js +12 -56
- package/dist/utils/dictation.js +15 -12
- package/dist/utils/dictation_native.js +17 -60
- package/dist/utils/git_probe_options.d.ts +28 -0
- package/dist/utils/git_probe_options.js +30 -0
- package/dist/utils/git_program_files.d.ts +145 -0
- package/dist/utils/git_program_files.js +1282 -0
- package/dist/utils/git_repository_programs.d.ts +13 -0
- package/dist/utils/git_repository_programs.js +38 -0
- package/dist/utils/host_shell.js +2 -1
- package/dist/utils/idle_compaction.js +8 -1
- package/dist/utils/image_fit.d.ts +4 -1
- package/dist/utils/image_fit.js +6 -2
- package/dist/utils/image_preview.js +3 -2
- package/dist/utils/mirror_media.d.ts +22 -4
- package/dist/utils/mirror_media.js +87 -86
- package/dist/utils/ml_native.js +11 -56
- package/dist/utils/native_install.d.ts +55 -0
- package/dist/utils/native_install.js +107 -0
- package/dist/utils/open_url.js +2 -0
- package/dist/utils/process_end.d.ts +16 -0
- package/dist/utils/process_end.js +73 -0
- package/dist/utils/router_catalog.js +9 -2
- package/dist/utils/sandbox.d.ts +64 -1
- package/dist/utils/sandbox.js +397 -89
- package/dist/utils/shell.js +31 -12
- package/dist/utils/subprocess_env.d.ts +218 -2
- package/dist/utils/subprocess_env.js +429 -11
- package/dist/utils/system_ps.d.ts +5 -0
- package/dist/utils/system_ps.js +30 -0
- package/dist/utils/tts.js +5 -0
- package/dist/utils/tts_native.js +13 -60
- package/dist/utils/zlicky_overlay.js +17 -50
- package/dist/utils/ztc_own_state.d.ts +58 -0
- package/dist/utils/ztc_own_state.js +236 -0
- package/dist/web/mirror_authenticator.d.ts +67 -6
- package/dist/web/mirror_authenticator.js +131 -22
- package/dist/web/mirror_stream.d.ts +54 -10
- package/dist/web/mirror_stream.js +379 -92
- package/dist/web/observer_query_redaction.js +53 -13
- package/dist/web/observer_secret_redaction.d.ts +59 -0
- package/dist/web/observer_secret_redaction.js +714 -0
- package/dist/web/pane_credentials.d.ts +20 -2
- package/dist/web/pane_credentials.js +58 -5
- package/dist/worker/executor.js +3 -1
- package/dist/workspace_collision.js +19 -15
- package/dist/zerg/local_zerg_launches.d.ts +27 -0
- package/dist/zerg/local_zerg_launches.js +198 -0
- package/dist/zlm-embed.mjs +6257 -2791
- package/npm-shrinkwrap.json +1 -1
- package/package.json +42 -15
package/bin/zmux-darwin-arm64
CHANGED
|
Binary file
|
package/bin/zmux-linux-x64
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/bin/ztc-tui-darwin-arm64
CHANGED
|
Binary file
|
package/bin/ztc-tui-linux-x64
CHANGED
|
Binary file
|
package/bin/zterm-darwin-arm64
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
[diffend] Oversized file quarantined before diffing.
|
|
2
2
|
name: package/bin/zterm-darwin-arm64
|
|
3
|
-
size:
|
|
4
|
-
sha256:
|
|
3
|
+
size: 35046592 bytes
|
|
4
|
+
sha256: 44cfa1ceb078117646ac565d048273d487b5084e255c158866cacfc2718bdcf3
|
package/dist/App.js
CHANGED
|
@@ -4,6 +4,8 @@ import React, { useState, useCallback, useMemo, useRef } from 'react';
|
|
|
4
4
|
import { Box, Text, useApp, useInput, useStdout, Static } from 'ink';
|
|
5
5
|
import { Header, MessageList, SingleMessage, InputArea, StatusBar, FullScreen, ActivityLine, useScreenSize, FormWidget, QueuePanel } from './components/index.js';
|
|
6
6
|
import { AgentError } from './agent/index.js';
|
|
7
|
+
import { withheldAttachedImages } from './agent/attached_images.js';
|
|
8
|
+
import { pinnedRouteCommandRefusal } from './config/model_pin.js';
|
|
7
9
|
import { commands } from './agent/commands/index.js';
|
|
8
10
|
import { goalManager } from './agent/commands/goal.js';
|
|
9
11
|
import { listCommandJobs, subscribeCommandJobs } from './agent/runtime/command_jobs.js';
|
|
@@ -1276,7 +1278,12 @@ export const App = () => {
|
|
|
1276
1278
|
content: `Context ${Math.round(contextStatus.percentage)}% full. Auto-compacting...`
|
|
1277
1279
|
});
|
|
1278
1280
|
const rows = messagesRef.current.length;
|
|
1279
|
-
|
|
1281
|
+
// Between turns nothing hears the agent's events: what the summary's request went without is told here.
|
|
1282
|
+
const report = {
|
|
1283
|
+
onNotice: (message) => addMessage({ role: 'system', content: message, metadata: { localOnly: true } }),
|
|
1284
|
+
onAttachedImagesWithheld: (note, keep) => setMessages(prev => withheldAttachedImages(prev, note, keep)),
|
|
1285
|
+
};
|
|
1286
|
+
compactAtTrigger(history, currentModel, compactionSummarizer(agentHandoffSummarizer(compactor, history, undefined, undefined, report)), decision).then((result) => {
|
|
1280
1287
|
if (result.removed === 0 && result.tokensAfter >= result.tokensBefore) {
|
|
1281
1288
|
addMessage({ role: 'system', content: 'Compaction could not reduce the context. Try /compact, or start a new session.' });
|
|
1282
1289
|
return;
|
|
@@ -1759,6 +1766,12 @@ export const App = () => {
|
|
|
1759
1766
|
return;
|
|
1760
1767
|
}
|
|
1761
1768
|
const command = commands.find(c => c.name === normalizedCommand);
|
|
1769
|
+
// A session its host pinned keeps its route, whoever asks to move it (config/model_pin.ts).
|
|
1770
|
+
const pinned = command ? pinnedRouteCommandRefusal(normalizedCommand, args) : undefined;
|
|
1771
|
+
if (pinned) {
|
|
1772
|
+
addMessage({ role: 'system', content: pinned });
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1762
1775
|
if (command) {
|
|
1763
1776
|
const result = command.handler(args, appContext);
|
|
1764
1777
|
if (result && typeof result.catch === 'function') {
|
|
@@ -2170,6 +2183,10 @@ export const App = () => {
|
|
|
2170
2183
|
// Display-only: the agent sends the model no system line, and a local-only one no recap or summary reads.
|
|
2171
2184
|
addMessage({ role: 'system', content: event.message, metadata: { localOnly: true } });
|
|
2172
2185
|
break;
|
|
2186
|
+
case 'attached_images_withheld':
|
|
2187
|
+
// Zerg Router took the turn only without the images the person attached: the note stands in for them.
|
|
2188
|
+
setMessages(prev => withheldAttachedImages(prev, event.note, event.keep));
|
|
2189
|
+
break;
|
|
2173
2190
|
case 'error':
|
|
2174
2191
|
setAgentState({ status: 'error', error: event.error });
|
|
2175
2192
|
break;
|
package/dist/agent/agent.d.ts
CHANGED
|
@@ -229,6 +229,16 @@ export interface HarnessToolCallOptions {
|
|
|
229
229
|
onActivity?: () => void;
|
|
230
230
|
onUsage?: ModelUsageObserver;
|
|
231
231
|
}
|
|
232
|
+
/** Captured by one owner turn; never looked up through mutable active state. */
|
|
233
|
+
/**
|
|
234
|
+
* How a host's transcript hears what a summary's request went without (Agent.getHandoffContext): a notice to show, and
|
|
235
|
+
* the note that stands in for the person's images that Router took it only without (all but those of a media type in
|
|
236
|
+
* KEEP).
|
|
237
|
+
*/
|
|
238
|
+
export interface HandoffReport {
|
|
239
|
+
onNotice: (message: string) => void;
|
|
240
|
+
onAttachedImagesWithheld: (note: string, keep?: readonly string[]) => void;
|
|
241
|
+
}
|
|
232
242
|
export declare class AgentError extends Error {
|
|
233
243
|
code?: string | undefined;
|
|
234
244
|
constructor(message: string, code?: string | undefined, options?: ErrorOptions);
|
|
@@ -390,7 +400,14 @@ export declare class Agent {
|
|
|
390
400
|
};
|
|
391
401
|
/** Snapshot the effective tool surface after mode/provider filtering. */
|
|
392
402
|
getConfiguredToolNames(): string[];
|
|
393
|
-
|
|
403
|
+
/**
|
|
404
|
+
* The request a summary of MESSAGES (a compaction's handoff) goes as, with this agent's backend and retries. What the
|
|
405
|
+
* backend says of it to the transcript, as of any request (a tool's images, or the person's, that Router took it only
|
|
406
|
+
* without, and the note that stands in for those from then on), goes to REPORT, the host's own, which hears of it
|
|
407
|
+
* between turns too; without one, to this agent's events (notice, attached_images_withheld), which a host hears during
|
|
408
|
+
* a turn.
|
|
409
|
+
*/
|
|
410
|
+
getHandoffContext(messages?: Message[], report?: HandoffReport): BackendRequest & {
|
|
394
411
|
backend: AgentBackend;
|
|
395
412
|
apiRetry: Required<ApiRetrySettings>;
|
|
396
413
|
};
|
|
@@ -555,6 +572,13 @@ export declare class Agent {
|
|
|
555
572
|
private executeHarnessToolCallWithCheckpoint;
|
|
556
573
|
private captureUserInstructions;
|
|
557
574
|
private evidenceSource;
|
|
575
|
+
/**
|
|
576
|
+
* Zerg Router took a request only once it went with NOTE in place of the images the person attached that it can't
|
|
577
|
+
* price under a spending limit (all but those of a media type in KEEP): NOTE stands in for them in this run's
|
|
578
|
+
* conversation, so no later request of the turn carries them, and the host is told to do the same in its transcript
|
|
579
|
+
* and saved session (attached_images_withheld).
|
|
580
|
+
*/
|
|
581
|
+
private withholdAttachedImages;
|
|
558
582
|
private callAPI;
|
|
559
583
|
private parseAPIError;
|
|
560
584
|
}
|
package/dist/agent/agent.js
CHANGED
|
@@ -66,6 +66,7 @@ import { guardRunawayOutput, RunawayOutputError } from './runaway_output.js';
|
|
|
66
66
|
import { isEphemeralToolCall } from '../utils/ephemeral_tools.js';
|
|
67
67
|
import { canReadCopies, COPY_ADVICE, resultCopyNote, saveCopy } from '../utils/tool_output_copies.js';
|
|
68
68
|
import { remoteDeletionReport, NO_VERIFIED_DELETION_REPORT } from './runtime/remote_deletion_receipts.js';
|
|
69
|
+
import { withheldAttachedImages } from './attached_images.js';
|
|
69
70
|
const defaultTools = [
|
|
70
71
|
...baseDefaultTools,
|
|
71
72
|
toolSearchTool,
|
|
@@ -696,10 +697,19 @@ export class Agent {
|
|
|
696
697
|
return [];
|
|
697
698
|
return this.config.tools.map((tool) => tool.definition.name);
|
|
698
699
|
}
|
|
699
|
-
|
|
700
|
+
/**
|
|
701
|
+
* The request a summary of MESSAGES (a compaction's handoff) goes as, with this agent's backend and retries. What the
|
|
702
|
+
* backend says of it to the transcript, as of any request (a tool's images, or the person's, that Router took it only
|
|
703
|
+
* without, and the note that stands in for those from then on), goes to REPORT, the host's own, which hears of it
|
|
704
|
+
* between turns too; without one, to this agent's events (notice, attached_images_withheld), which a host hears during
|
|
705
|
+
* a turn.
|
|
706
|
+
*/
|
|
707
|
+
getHandoffContext(messages = [], report) {
|
|
700
708
|
// A summary asked for mid-run (compaction) goes with that run's snapshot.
|
|
701
709
|
const run = this.activeRun;
|
|
702
710
|
return { ...this.buildRequest([...messages], this.config.tools, run), backend: this.turnBackend(run), apiRetry: this.config.apiRetry,
|
|
711
|
+
onNotice: report?.onNotice ?? (message => this.emit({ type: 'notice', message })),
|
|
712
|
+
onAttachedImagesWithheld: report?.onAttachedImagesWithheld ?? ((note, keep) => this.withholdAttachedImages(note, undefined, keep)),
|
|
703
713
|
onMetrics: metrics => {
|
|
704
714
|
this.emit({ type: 'request_metrics', metrics });
|
|
705
715
|
if (!metrics.usage)
|
|
@@ -3362,6 +3372,19 @@ INVESTIGATION STRATEGY — before making tool calls to explore or debug, follow
|
|
|
3362
3372
|
}
|
|
3363
3373
|
return source;
|
|
3364
3374
|
}
|
|
3375
|
+
/**
|
|
3376
|
+
* Zerg Router took a request only once it went with NOTE in place of the images the person attached that it can't
|
|
3377
|
+
* price under a spending limit (all but those of a media type in KEEP): NOTE stands in for them in this run's
|
|
3378
|
+
* conversation, so no later request of the turn carries them, and the host is told to do the same in its transcript
|
|
3379
|
+
* and saved session (attached_images_withheld).
|
|
3380
|
+
*/
|
|
3381
|
+
withholdAttachedImages(note, context, keep = []) {
|
|
3382
|
+
if (context) {
|
|
3383
|
+
context.messages.splice(0, context.messages.length, ...withheldAttachedImages(context.messages, note, keep));
|
|
3384
|
+
context.turnMessages.splice(0, context.turnMessages.length, ...withheldAttachedImages(context.turnMessages, note, keep));
|
|
3385
|
+
}
|
|
3386
|
+
this.emit({ type: 'attached_images_withheld', note, ...(keep.length > 0 ? { keep: [...keep] } : {}) });
|
|
3387
|
+
}
|
|
3365
3388
|
// API call with retry logic for transient errors. RUN's request goes through its own host policy's snapshot.
|
|
3366
3389
|
async callAPI(request, onProviderObservation, signal, checkpointContext, onReturnedResponse, onUsage, run = this.activeRun) {
|
|
3367
3390
|
const { maxAttempts, recoveryWindowMs } = this.config.apiRetry;
|
|
@@ -3441,6 +3464,8 @@ INVESTIGATION STRATEGY — before making tool calls to explore or debug, follow
|
|
|
3441
3464
|
},
|
|
3442
3465
|
// The transcript shows it once; the model never gets it.
|
|
3443
3466
|
onNotice: message => this.emit({ type: 'notice', message }),
|
|
3467
|
+
// Router took the request once it went without the person's images: the note stands in for them from now on.
|
|
3468
|
+
onAttachedImagesWithheld: (note, keep) => this.withholdAttachedImages(note, checkpointContext, keep),
|
|
3444
3469
|
// A backend reconnect is a sign of life too: the Router is still holding
|
|
3445
3470
|
// the turn, so the inactivity guard must not fire during the backoff.
|
|
3446
3471
|
onTransportRetry: (info) => {
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Images the person attached (a mirror upload, an `[image PATH]` in what they typed) under a ZergRouter spending limit.
|
|
3
|
+
* Router can't price an image for most models (it prices only OpenAI's, as PNG, JPEG or WebP), and a dollar limit
|
|
4
|
+
* refuses a request it can't price (403 router_usage_unpriced). An image the person attached goes in their own message,
|
|
5
|
+
* so every later request of the conversation carried it and was refused too. A backend on any Zerg Router route (a
|
|
6
|
+
* host's Router runtime grant, as ZergCloud's resident runs, a hosted native transport, or the person's own key or
|
|
7
|
+
* sign-in) that meets that refusal sends the request again at once with withheldImageNote in place of each image of a
|
|
8
|
+
* format Router never prices on the route (routerPricedImageTypes), and of every image when Router refuses it again,
|
|
9
|
+
* and once Router takes it, the caller puts the same note in those images' place in its conversation and saved session
|
|
10
|
+
* (withheldAttachedImages), so no later turn, nor a resumed session, sends them again; the person's images Router
|
|
11
|
+
* prices still go. A direct provider's refusal changes nothing. A tool's images are another matter
|
|
12
|
+
* (tool_result_images.ts).
|
|
13
|
+
*/
|
|
14
|
+
import type { Message } from '../types.js';
|
|
15
|
+
import type { LlmMessage, RequestContentBlock } from './backends/types.js';
|
|
16
|
+
/** How a route is named: `provider/model`, once (a Router model id can carry its provider already). */
|
|
17
|
+
export declare function routeName(provider: string, model: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* The media types of the images Router may price on PROVIDER's route under a spending limit: PNG, JPEG and WebP for
|
|
20
|
+
* OpenAI (and only for those of its models whose image tokens it can bound), none for any other provider.
|
|
21
|
+
*/
|
|
22
|
+
export declare function routerPricedImageTypes(provider: string): readonly string[];
|
|
23
|
+
/**
|
|
24
|
+
* The note that stands in for an image the person attached, in the request sent again and in the conversation: for
|
|
25
|
+
* every one of their images, or, with FORMATS, for each of theirs in those formats. It never reads as an `[image PATH]`
|
|
26
|
+
* the request encoder would load (its regex is lowercase), and it says why.
|
|
27
|
+
*/
|
|
28
|
+
export declare function withheldImageNote(provider: string, model: string, formats?: readonly string[]): string;
|
|
29
|
+
/**
|
|
30
|
+
* What the person is told, in the transcript and so to the mirror's controllers, when their images were withheld: every
|
|
31
|
+
* one of them, or, with FORMATS, those in those formats.
|
|
32
|
+
*/
|
|
33
|
+
export declare function withheldImagesNotice(provider: string, model: string, formats?: readonly string[]): string;
|
|
34
|
+
/** Whether MESSAGES carry an image the person attached: an image block of a user message, never one in a tool's result. */
|
|
35
|
+
export declare function carriesAttachedImages(messages: readonly LlmMessage[]): boolean;
|
|
36
|
+
/** The media types of the images the person attached in MESSAGES that KEEP doesn't hold, each once. */
|
|
37
|
+
export declare function attachedImageTypesOutside(messages: readonly LlmMessage[], keep: readonly string[]): string[];
|
|
38
|
+
/**
|
|
39
|
+
* MESSAGES with NOTE as text in place of each image the person attached whose media type KEEP doesn't hold (every one,
|
|
40
|
+
* when KEEP is empty); a tool's images stay as they are.
|
|
41
|
+
*/
|
|
42
|
+
export declare function withoutAttachedImages(messages: readonly LlmMessage[], note: string, keep?: readonly string[]): LlmMessage[];
|
|
43
|
+
/**
|
|
44
|
+
* MESSAGES, a conversation as a renderer or the agent holds it, with NOTE standing in from now on for each image the
|
|
45
|
+
* person attached whose media type KEEP doesn't hold (every one, when KEEP is empty): each message of theirs that
|
|
46
|
+
* carries one (an `[image PATH]` in what they typed, an image attachment, an image in the content frozen for a native
|
|
47
|
+
* replay) is marked with it (metadata.withheldImages, and metadata.keptImageTypes, the types still sent), and the request
|
|
48
|
+
* encoder sends NOTE in each such image's place (withheldImagesIn). A message marked before withholds what either note
|
|
49
|
+
* withholds. What the person typed and attached stays as it was, for them to see, and so does the message's identity.
|
|
50
|
+
* Host context, a compaction's summary and a tool's results are literal text, never the person's images. Messages
|
|
51
|
+
* without such an image are returned as they were.
|
|
52
|
+
*/
|
|
53
|
+
export declare function withheldAttachedImages(messages: readonly Message[], note: string, keep?: readonly string[]): Message[];
|
|
54
|
+
/**
|
|
55
|
+
* CONTENT, a message's request content, with NOTE as text in place of each image in it whose media type KEEP doesn't
|
|
56
|
+
* hold (every one, when KEEP is empty; see withheldAttachedImages).
|
|
57
|
+
*/
|
|
58
|
+
export declare function withheldImagesIn<T extends string | RequestContentBlock[]>(content: T, note: string, keep?: readonly string[]): T;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { isToolResultMessage } from '../utils/tool_result_message.js';
|
|
2
|
+
/** How a route is named: `provider/model`, once (a Router model id can carry its provider already). */
|
|
3
|
+
export function routeName(provider, model) {
|
|
4
|
+
return model.startsWith(`${provider}/`) ? model : `${provider}/${model}`;
|
|
5
|
+
}
|
|
6
|
+
/** The formats Router can price an image in for OpenAI's models (image-usage-bound.ts: an inline PNG, JPEG or WebP). */
|
|
7
|
+
const OPENAI_PRICED_IMAGE_TYPES = Object.freeze(['image/png', 'image/jpeg', 'image/webp']);
|
|
8
|
+
/**
|
|
9
|
+
* The media types of the images Router may price on PROVIDER's route under a spending limit: PNG, JPEG and WebP for
|
|
10
|
+
* OpenAI (and only for those of its models whose image tokens it can bound), none for any other provider.
|
|
11
|
+
*/
|
|
12
|
+
export function routerPricedImageTypes(provider) {
|
|
13
|
+
return provider === 'openai' ? OPENAI_PRICED_IMAGE_TYPES : [];
|
|
14
|
+
}
|
|
15
|
+
/** How the person reads an image's format: GIF for image/gif, SVG for image/svg+xml. */
|
|
16
|
+
function formatName(mediaType) {
|
|
17
|
+
const subtype = mediaType.split('/')[1]?.split(/[+;]/)[0]?.trim();
|
|
18
|
+
return (subtype || mediaType).toUpperCase();
|
|
19
|
+
}
|
|
20
|
+
/** FORMATS (media types) as the person reads them: "GIF", "GIF and BMP", "GIF, BMP and TIFF". */
|
|
21
|
+
function formatsPhrase(formats) {
|
|
22
|
+
const names = [...new Set(formats.map(formatName))];
|
|
23
|
+
return names.length <= 1 ? names.join('') : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
|
|
24
|
+
}
|
|
25
|
+
/** What the route can't take: images, or images of FORMATS when only those are withheld. */
|
|
26
|
+
function imagesOf(formats) {
|
|
27
|
+
return formats && formats.length > 0 ? `${formatsPhrase(formats)} images` : 'images';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The note that stands in for an image the person attached, in the request sent again and in the conversation: for
|
|
31
|
+
* every one of their images, or, with FORMATS, for each of theirs in those formats. It never reads as an `[image PATH]`
|
|
32
|
+
* the request encoder would load (its regex is lowercase), and it says why.
|
|
33
|
+
*/
|
|
34
|
+
export function withheldImageNote(provider, model, formats) {
|
|
35
|
+
return `[Image not sent: ${routeName(provider, model)} can't take ${imagesOf(formats)} under this spending limit.]`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What the person is told, in the transcript and so to the mirror's controllers, when their images were withheld: every
|
|
39
|
+
* one of them, or, with FORMATS, those in those formats.
|
|
40
|
+
*/
|
|
41
|
+
export function withheldImagesNotice(provider, model, formats) {
|
|
42
|
+
const route = routeName(provider, model);
|
|
43
|
+
const image = formats && formats.length > 0 ? `${formatsPhrase(formats)} image` : 'image';
|
|
44
|
+
return `${route} can't take ${imagesOf(formats)} under this spending limit: Zerg Router refused your message with the ${image} you `
|
|
45
|
+
+ 'attached, which it can\'t price. ZTC sent the message again without it, and a note stands in its place in the conversation.';
|
|
46
|
+
}
|
|
47
|
+
/** Whether MESSAGES carry an image the person attached: an image block of a user message, never one in a tool's result. */
|
|
48
|
+
export function carriesAttachedImages(messages) {
|
|
49
|
+
return messages.some(message => message.role === 'user' && Array.isArray(message.content)
|
|
50
|
+
&& message.content.some(block => block.type === 'image'));
|
|
51
|
+
}
|
|
52
|
+
/** Whether BLOCK is an image KEEP doesn't hold: one to withhold. */
|
|
53
|
+
function withheldBlock(block, keep) {
|
|
54
|
+
return block.type === 'image' && !keep.includes(block.mediaType);
|
|
55
|
+
}
|
|
56
|
+
/** The media types of the images the person attached in MESSAGES that KEEP doesn't hold, each once. */
|
|
57
|
+
export function attachedImageTypesOutside(messages, keep) {
|
|
58
|
+
const types = new Set();
|
|
59
|
+
for (const message of messages) {
|
|
60
|
+
if (message.role !== 'user' || !Array.isArray(message.content))
|
|
61
|
+
continue;
|
|
62
|
+
for (const block of message.content)
|
|
63
|
+
if (block.type === 'image' && !keep.includes(block.mediaType))
|
|
64
|
+
types.add(block.mediaType);
|
|
65
|
+
}
|
|
66
|
+
return [...types];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* MESSAGES with NOTE as text in place of each image the person attached whose media type KEEP doesn't hold (every one,
|
|
70
|
+
* when KEEP is empty); a tool's images stay as they are.
|
|
71
|
+
*/
|
|
72
|
+
export function withoutAttachedImages(messages, note, keep = []) {
|
|
73
|
+
return messages.map(message => {
|
|
74
|
+
if (message.role !== 'user' || !Array.isArray(message.content) || !message.content.some(block => withheldBlock(block, keep)))
|
|
75
|
+
return message;
|
|
76
|
+
return { ...message, content: message.content.map((block) => withheldBlock(block, keep) ? { type: 'text', text: note } : block) };
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/** An `[image PATH]` the request encoder loads as an image (request_encoder.ts buildContentBlocks). */
|
|
80
|
+
const IMAGE_REFERENCE = /\[image ([^\]]+)\]/g;
|
|
81
|
+
/** The media type an `[image PATH]`'s extension names, or '' when it names none ZTC knows. */
|
|
82
|
+
function referencedImageType(path) {
|
|
83
|
+
const extension = /\.([a-z0-9]+)\s*$/i.exec(path)?.[1]?.toLowerCase();
|
|
84
|
+
return extension === 'jpg' || extension === 'jpeg' ? 'image/jpeg'
|
|
85
|
+
: extension === 'png' || extension === 'gif' || extension === 'webp' ? `image/${extension}` : '';
|
|
86
|
+
}
|
|
87
|
+
/** The media types of the images MESSAGE carries from the person ('' for one whose type it can't tell). */
|
|
88
|
+
function personImageTypes(message) {
|
|
89
|
+
const types = (message.attachments ?? []).filter(attachment => attachment.kind === 'image').map(attachment => attachment.mediaType);
|
|
90
|
+
for (const reference of message.content.matchAll(IMAGE_REFERENCE))
|
|
91
|
+
types.push(referencedImageType(reference[1]));
|
|
92
|
+
if (Array.isArray(message.nativeContent)) {
|
|
93
|
+
for (const block of message.nativeContent)
|
|
94
|
+
if (block.type === 'image')
|
|
95
|
+
types.push(block.mediaType);
|
|
96
|
+
}
|
|
97
|
+
return types;
|
|
98
|
+
}
|
|
99
|
+
/** The media types both KEPT lists hold; undefined (none kept) when either is. */
|
|
100
|
+
function bothKeep(first, second) {
|
|
101
|
+
if (!first?.length || !second?.length)
|
|
102
|
+
return undefined;
|
|
103
|
+
const both = first.filter(type => second.includes(type));
|
|
104
|
+
return both.length > 0 ? both : undefined;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* MESSAGES, a conversation as a renderer or the agent holds it, with NOTE standing in from now on for each image the
|
|
108
|
+
* person attached whose media type KEEP doesn't hold (every one, when KEEP is empty): each message of theirs that
|
|
109
|
+
* carries one (an `[image PATH]` in what they typed, an image attachment, an image in the content frozen for a native
|
|
110
|
+
* replay) is marked with it (metadata.withheldImages, and metadata.keptImageTypes, the types still sent), and the request
|
|
111
|
+
* encoder sends NOTE in each such image's place (withheldImagesIn). A message marked before withholds what either note
|
|
112
|
+
* withholds. What the person typed and attached stays as it was, for them to see, and so does the message's identity.
|
|
113
|
+
* Host context, a compaction's summary and a tool's results are literal text, never the person's images. Messages
|
|
114
|
+
* without such an image are returned as they were.
|
|
115
|
+
*/
|
|
116
|
+
export function withheldAttachedImages(messages, note, keep = []) {
|
|
117
|
+
return messages.map(message => {
|
|
118
|
+
if (message.role !== 'user' || message.metadata?.hostContext === true || message.metadata?.compactionSummary
|
|
119
|
+
|| isToolResultMessage(message))
|
|
120
|
+
return message;
|
|
121
|
+
if (!personImageTypes(message).some(type => !keep.includes(type)))
|
|
122
|
+
return message;
|
|
123
|
+
const marked = typeof message.metadata?.withheldImages === 'string';
|
|
124
|
+
const before = message.metadata?.keptImageTypes;
|
|
125
|
+
const kept = marked ? bothKeep(before, keep) : bothKeep(keep, keep);
|
|
126
|
+
// Marked before with as much withheld: as it was.
|
|
127
|
+
if (marked && (kept?.length ?? 0) === (before?.length ?? 0))
|
|
128
|
+
return message;
|
|
129
|
+
const metadata = { ...message.metadata, withheldImages: note };
|
|
130
|
+
if (kept)
|
|
131
|
+
metadata.keptImageTypes = kept;
|
|
132
|
+
else
|
|
133
|
+
delete metadata.keptImageTypes;
|
|
134
|
+
return { ...message, metadata };
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* CONTENT, a message's request content, with NOTE as text in place of each image in it whose media type KEEP doesn't
|
|
139
|
+
* hold (every one, when KEEP is empty; see withheldAttachedImages).
|
|
140
|
+
*/
|
|
141
|
+
export function withheldImagesIn(content, note, keep = []) {
|
|
142
|
+
if (typeof content === 'string' || !content.some(block => withheldBlock(block, keep)))
|
|
143
|
+
return content;
|
|
144
|
+
return content.map((block) => withheldBlock(block, keep) ? { type: 'text', text: note } : block);
|
|
145
|
+
}
|
|
@@ -6,6 +6,11 @@ interface AnthropicBackendConfig {
|
|
|
6
6
|
apiEndpoint?: string;
|
|
7
7
|
validateStrictTools?: boolean;
|
|
8
8
|
preserveNativeHistory?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* The endpoint is Zerg Router's native Messages route: a request Router refuses as one it can't price under a
|
|
11
|
+
* spending limit while it carries images the person attached goes again without them (agent/attached_images.ts).
|
|
12
|
+
*/
|
|
13
|
+
zergRouter?: boolean;
|
|
9
14
|
}
|
|
10
15
|
export interface NativeAnthropicMessagesRouterConfig {
|
|
11
16
|
baseUrl: string;
|
|
@@ -22,9 +27,18 @@ export declare class AnthropicBackend implements AgentBackend {
|
|
|
22
27
|
private apiEndpoint;
|
|
23
28
|
private cacheControl;
|
|
24
29
|
private readonly validateStrictTools;
|
|
30
|
+
private readonly zergRouter;
|
|
25
31
|
constructor(config: AnthropicBackendConfig);
|
|
26
32
|
isAuthenticationConfigured(): boolean;
|
|
27
33
|
allowsBufferedFallbackAfterStreamFailure(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* On Zerg Router's native route, REQUEST to send again when Router refused it (STATUS, BODY) as one it can't price
|
|
36
|
+
* under a spending limit while it carried images the person attached: the same request with a note in each one's
|
|
37
|
+
* place, and the note. Undefined for any other failure, another endpoint, or a request without such images.
|
|
38
|
+
*/
|
|
39
|
+
private withoutRefusedAttachedImages;
|
|
40
|
+
/** Router took REQUEST, sent again with NOTE in place of the person's images: the transcript says why, and the caller replaces them. */
|
|
41
|
+
private tellAttachedImagesWithheld;
|
|
28
42
|
private buildHeaders;
|
|
29
43
|
private buildBody;
|
|
30
44
|
generate(request: BackendRequest): Promise<BackendResponse>;
|
|
@@ -2,8 +2,13 @@ import { IncompleteStreamError } from './transport_errors.js';
|
|
|
2
2
|
import { modelContract } from '@zerg/model-contracts';
|
|
3
3
|
import { parseSSEStream } from './sse.js';
|
|
4
4
|
import { readBoundedProviderError } from './openai_responses.js';
|
|
5
|
+
import { MODEL_REQUEST_REDIRECT, refuseModelRedirect } from './model_redirect.js';
|
|
5
6
|
import { CLAUDE_CODE_TOKEN_NOT_SENT, isClaudeCodeToken } from '../../config/retired_routes.js';
|
|
6
7
|
import { ANTHROPIC_IMAGE_LIMITS, planToolImages, withToolImagesInResults } from './tool_result_images.js';
|
|
8
|
+
import { readRouterRefusal } from './router_refusal.js';
|
|
9
|
+
import { carriesAttachedImages, routeName, withheldImageNote, withheldImagesNotice, withoutAttachedImages } from '../attached_images.js';
|
|
10
|
+
import { runtimeDiag } from '../../utils/debug_log.js';
|
|
11
|
+
import { promptCacheTtl } from './prompt_cache.js';
|
|
7
12
|
/**
|
|
8
13
|
* A credential this backend may send, or undefined for a Claude Code sign-in
|
|
9
14
|
* token: Anthropic reserves subscription sign-in for Claude Code itself, so
|
|
@@ -27,6 +32,7 @@ export function createNativeAnthropicMessagesRouterBackend(config) {
|
|
|
27
32
|
authTokenProvider: config.authTokenProvider,
|
|
28
33
|
validateStrictTools: true,
|
|
29
34
|
preserveNativeHistory: true,
|
|
35
|
+
zergRouter: true,
|
|
30
36
|
});
|
|
31
37
|
}
|
|
32
38
|
/** Reject unsupported strict constraints; never silently narrow an open map. */
|
|
@@ -80,6 +86,7 @@ export class AnthropicBackend {
|
|
|
80
86
|
apiEndpoint;
|
|
81
87
|
cacheControl;
|
|
82
88
|
validateStrictTools;
|
|
89
|
+
zergRouter;
|
|
83
90
|
constructor(config) {
|
|
84
91
|
this.apiKey = config.apiKey;
|
|
85
92
|
this.authToken = config.authToken;
|
|
@@ -87,13 +94,32 @@ export class AnthropicBackend {
|
|
|
87
94
|
this.apiEndpoint = config.apiEndpoint || 'https://api.anthropic.com/v1/messages';
|
|
88
95
|
this.validateStrictTools = config.validateStrictTools ?? false;
|
|
89
96
|
this.preservesNativeHistory = config.preserveNativeHistory ?? false;
|
|
90
|
-
|
|
97
|
+
this.zergRouter = config.zergRouter === true;
|
|
98
|
+
const ttl = promptCacheTtl();
|
|
91
99
|
this.cacheControl = ttl === 'off' ? null : ttl === '5m' ? { type: 'ephemeral' } : { type: 'ephemeral', ttl: '1h' };
|
|
92
100
|
}
|
|
93
101
|
isAuthenticationConfigured() {
|
|
94
102
|
return Boolean(sendable(this.apiKey) || sendable(this.authToken) || this.authTokenProvider);
|
|
95
103
|
}
|
|
96
104
|
allowsBufferedFallbackAfterStreamFailure() { return false; }
|
|
105
|
+
/**
|
|
106
|
+
* On Zerg Router's native route, REQUEST to send again when Router refused it (STATUS, BODY) as one it can't price
|
|
107
|
+
* under a spending limit while it carried images the person attached: the same request with a note in each one's
|
|
108
|
+
* place, and the note. Undefined for any other failure, another endpoint, or a request without such images.
|
|
109
|
+
*/
|
|
110
|
+
withoutRefusedAttachedImages(request, status, body) {
|
|
111
|
+
if (!this.zergRouter || readRouterRefusal(status, body)?.code !== 'router_usage_unpriced' || !carriesAttachedImages(request.messages))
|
|
112
|
+
return undefined;
|
|
113
|
+
const note = withheldImageNote('anthropic', request.model);
|
|
114
|
+
runtimeDiag('warn', `[ztc] Zerg Router refused a request to ${routeName('anthropic', request.model)} that carried images the person `
|
|
115
|
+
+ 'attached (router_usage_unpriced), so ZTC sent it again without them.');
|
|
116
|
+
return { request: { ...request, messages: withoutAttachedImages(request.messages, note) }, note };
|
|
117
|
+
}
|
|
118
|
+
/** Router took REQUEST, sent again with NOTE in place of the person's images: the transcript says why, and the caller replaces them. */
|
|
119
|
+
tellAttachedImagesWithheld(request, note) {
|
|
120
|
+
request.onNotice?.(withheldImagesNotice('anthropic', request.model));
|
|
121
|
+
request.onAttachedImagesWithheld?.(note);
|
|
122
|
+
}
|
|
97
123
|
async buildHeaders() {
|
|
98
124
|
const headers = {
|
|
99
125
|
'Content-Type': 'application/json',
|
|
@@ -229,9 +255,17 @@ export class AnthropicBackend {
|
|
|
229
255
|
headers: await this.buildHeaders(),
|
|
230
256
|
body: JSON.stringify(body),
|
|
231
257
|
signal: request.signal,
|
|
258
|
+
redirect: MODEL_REQUEST_REDIRECT,
|
|
232
259
|
});
|
|
260
|
+
await refuseModelRedirect(response, this.apiEndpoint);
|
|
233
261
|
if (!response.ok) {
|
|
234
262
|
const errorText = await readBoundedProviderError(response);
|
|
263
|
+
const attached = this.withoutRefusedAttachedImages(request, response.status, errorText);
|
|
264
|
+
if (attached) {
|
|
265
|
+
const answer = await this.generate(attached.request);
|
|
266
|
+
this.tellAttachedImagesWithheld(request, attached.note);
|
|
267
|
+
return answer;
|
|
268
|
+
}
|
|
235
269
|
throw new AnthropicApiError(response.status, errorText);
|
|
236
270
|
}
|
|
237
271
|
const data = await response.json();
|
|
@@ -255,10 +289,23 @@ export class AnthropicBackend {
|
|
|
255
289
|
headers: await this.buildHeaders(),
|
|
256
290
|
body: JSON.stringify(body),
|
|
257
291
|
signal: request.signal,
|
|
292
|
+
redirect: MODEL_REQUEST_REDIRECT,
|
|
258
293
|
});
|
|
294
|
+
await refuseModelRedirect(response, this.apiEndpoint);
|
|
259
295
|
if (!response.ok) {
|
|
260
296
|
const errorText = await readBoundedProviderError(response);
|
|
261
|
-
|
|
297
|
+
const attached = this.withoutRefusedAttachedImages(request, response.status, errorText);
|
|
298
|
+
if (!attached)
|
|
299
|
+
throw new AnthropicApiError(response.status, errorText);
|
|
300
|
+
let told = false;
|
|
301
|
+
for await (const event of this.generateStream(attached.request)) {
|
|
302
|
+
if (!told) {
|
|
303
|
+
told = true;
|
|
304
|
+
this.tellAttachedImagesWithheld(request, attached.note);
|
|
305
|
+
}
|
|
306
|
+
yield event;
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
262
309
|
}
|
|
263
310
|
if (!response.body) {
|
|
264
311
|
throw new Error('API error: no response body for streaming request');
|
|
@@ -53,7 +53,9 @@ export interface CodexAppServerClientOptions {
|
|
|
53
53
|
* codex CLI would bill that API key instead of the account's ChatGPT
|
|
54
54
|
* subscription (auth.json under CODEX_HOME). It applies even with no
|
|
55
55
|
* per-account home so an ambient key never hijacks the default ChatGPT login.
|
|
56
|
-
*
|
|
56
|
+
* The app-server runs the commands Codex's model asks for with this
|
|
57
|
+
* environment, so it starts from a user subprocess's (buildUserSubprocessEnv):
|
|
58
|
+
* the person's own, and none of ZTC's credentials or its host's effort policy.
|
|
57
59
|
*/
|
|
58
60
|
export declare function buildCodexAppServerEnv(codexHome?: string, base?: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|
|
59
61
|
export interface CodexTurnOptions {
|
|
@@ -6,7 +6,7 @@ import { parseCodexModelRow } from '../../utils/codex_models.js';
|
|
|
6
6
|
import { ModelNotServedError } from './model_not_served.js';
|
|
7
7
|
import { getVersion } from '../../utils/version.js';
|
|
8
8
|
import { NativeObservationLimitError } from '../runtime/provider_native_evidence.js';
|
|
9
|
-
import {
|
|
9
|
+
import { buildUserSubprocessEnv } from '../../utils/subprocess_env.js';
|
|
10
10
|
export const MAX_CODEX_APP_SERVER_FRAME_BYTES = 1_048_576;
|
|
11
11
|
export const MAX_CODEX_APP_SERVER_QUEUED_FRAMES = 1_024;
|
|
12
12
|
/** Keep managed profiles on their own auth.json and ChatGPT subscription. */
|
|
@@ -33,10 +33,12 @@ const MAX_CODEX_APP_SERVER_MODELS = 2_000;
|
|
|
33
33
|
* codex CLI would bill that API key instead of the account's ChatGPT
|
|
34
34
|
* subscription (auth.json under CODEX_HOME). It applies even with no
|
|
35
35
|
* per-account home so an ambient key never hijacks the default ChatGPT login.
|
|
36
|
-
*
|
|
36
|
+
* The app-server runs the commands Codex's model asks for with this
|
|
37
|
+
* environment, so it starts from a user subprocess's (buildUserSubprocessEnv):
|
|
38
|
+
* the person's own, and none of ZTC's credentials or its host's effort policy.
|
|
37
39
|
*/
|
|
38
40
|
export function buildCodexAppServerEnv(codexHome, base = process.env) {
|
|
39
|
-
const env =
|
|
41
|
+
const env = buildUserSubprocessEnv(base);
|
|
40
42
|
if (codexHome)
|
|
41
43
|
env.CODEX_HOME = codexHome;
|
|
42
44
|
for (const name of [
|
|
@@ -306,9 +308,10 @@ export class ChildProcessTransport {
|
|
|
306
308
|
this.close();
|
|
307
309
|
}
|
|
308
310
|
constructor(command, args, env) {
|
|
309
|
-
//
|
|
310
|
-
//
|
|
311
|
-
|
|
311
|
+
// ENV is the child's whole environment, a per-account CODEX_HOME included (buildCodexAppServerEnv, which every
|
|
312
|
+
// backend passes). A caller that gives none gets a user subprocess's (buildUserSubprocessEnv), never all of
|
|
313
|
+
// process.env: the app-server runs the commands Codex's model asks for with it, so none of ZTC's credentials.
|
|
314
|
+
this.child = spawn(command, args, { stdio: ['pipe', 'pipe', 'pipe'], env: env ?? buildUserSubprocessEnv() });
|
|
312
315
|
process.once('exit', this.reapOnExit);
|
|
313
316
|
this.child.stdout.on('data', (chunk) => {
|
|
314
317
|
if (this.stopping)
|
|
@@ -16,6 +16,7 @@ import { responsesReasoning } from './responses_reasoning.js';
|
|
|
16
16
|
import { RateLimitError, parseResetAt } from './rate_limit.js';
|
|
17
17
|
import { CODEX_LEGACY_PROVIDER } from './provider_ids.js';
|
|
18
18
|
import { CodexBufferedRecovery } from './codex_buffered_recovery.js';
|
|
19
|
+
import { MODEL_REQUEST_REDIRECT, refuseModelRedirect } from './model_redirect.js';
|
|
19
20
|
let fileLock;
|
|
20
21
|
// Loaded on first refresh: the headless package shares this module but does
|
|
21
22
|
// not ship proper-lockfile. Without it, refreshes are still serialized within
|
|
@@ -526,7 +527,9 @@ export class CodexAuthBackend {
|
|
|
526
527
|
headers,
|
|
527
528
|
body: JSON.stringify(body),
|
|
528
529
|
signal: request.signal,
|
|
530
|
+
redirect: MODEL_REQUEST_REDIRECT,
|
|
529
531
|
});
|
|
532
|
+
await refuseModelRedirect(response, `${CODEX_API_BASE}/responses`);
|
|
530
533
|
if (!response.ok) {
|
|
531
534
|
if (response.status === 401 && !_retried && (!recovery || recovery.canPost())) {
|
|
532
535
|
await response.body?.cancel().catch(() => undefined);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { reasoningRequestParams } from '@zerg/model-contracts';
|
|
2
2
|
import { BaseBackend } from './base.js';
|
|
3
|
+
import { MODEL_REQUEST_REDIRECT, refuseModelRedirect } from './model_redirect.js';
|
|
3
4
|
import { fittedBody, GEMINI_IMAGE_LIMITS, olderToolImages, olderToolImagesLine, planToolImages, toolImageCaption } from './tool_result_images.js';
|
|
4
5
|
export class GeminiBackend extends BaseBackend {
|
|
5
6
|
apiKey;
|
|
@@ -20,7 +21,9 @@ export class GeminiBackend extends BaseBackend {
|
|
|
20
21
|
headers: { 'Content-Type': 'application/json' },
|
|
21
22
|
body,
|
|
22
23
|
signal: request.signal,
|
|
24
|
+
redirect: MODEL_REQUEST_REDIRECT,
|
|
23
25
|
});
|
|
26
|
+
await refuseModelRedirect(response, url);
|
|
24
27
|
if (!response.ok) {
|
|
25
28
|
const errorText = await response.text();
|
|
26
29
|
throw new Error(`API error (${response.status}): ${errorText}`);
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
import { hostCredential, hostEnvironmentValue } from '../../config/host_credentials.js';
|
|
2
3
|
import { runtimeDiag } from '../../utils/debug_log.js';
|
|
4
|
+
import { routeChangeRefusal } from '../../config/model_pin.js';
|
|
3
5
|
const LEVELS = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max', 'ultra'];
|
|
4
6
|
const POLICIES = ['inherit', 'maximum', ...LEVELS];
|
|
5
7
|
/** Host-owned policy is opt-in. Ordinary terminal clients retain their settings. */
|
|
6
8
|
export async function applyHostEffortPolicy(request, resolve) {
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
+
// Both through the store, which holds the pair outside process.env (config/host_credentials.ts).
|
|
10
|
+
const endpoint = hostEnvironmentValue('ZTC_EFFORT_POLICY_URL');
|
|
11
|
+
const token = hostCredential('ZTC_EFFORT_POLICY_TOKEN');
|
|
9
12
|
if (!endpoint && !token)
|
|
10
13
|
return request;
|
|
11
14
|
let url;
|
|
@@ -89,6 +92,15 @@ export async function applyHostEffortPolicy(request, resolve) {
|
|
|
89
92
|
throw new Error('Invalid host model policy');
|
|
90
93
|
}
|
|
91
94
|
model = { provider: selection.provider, model: selection.model };
|
|
95
|
+
// A session its host pinned keeps its route (config/model_pin.ts): the policy's model is refused, and its effort
|
|
96
|
+
// applies to the pinned model. The policy's model runs through Zerg Router, as this backend does, so routing the
|
|
97
|
+
// environment pins to zrouter lets it move, and routing pinned to direct would refuse it.
|
|
98
|
+
const refused = routeChangeRefusal({ ...model, routing: 'zrouter' });
|
|
99
|
+
if (refused) {
|
|
100
|
+
runtimeDiag('warn', `[ztc] host model policy ${policy.revision} would move this session to ${model.provider}/${model.model}; `
|
|
101
|
+
+ `refused: ${refused}`);
|
|
102
|
+
model = null;
|
|
103
|
+
}
|
|
92
104
|
}
|
|
93
105
|
else if (policy.model != null) {
|
|
94
106
|
throw new Error('Invalid host model policy version');
|