@theoremjs/agents 0.3.5-next.951.gc40a24e → 0.3.5-next.953.gf255d6a
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/esm/src/guardrails/error.js +3 -0
- package/esm/src/guardrails/labels.d.ts +1 -1
- package/esm/src/guardrails/labels.js +3 -0
- package/esm/src/guardrails/theorem-error.d.ts +3 -0
- package/esm/src/guardrails/theorem-error.js +3 -0
- package/esm/src/host/client-turn.js +5 -0
- package/esm/src/kernel/engine/runner/refused-media.d.ts +14 -5
- package/esm/src/kernel/engine/runner/refused-media.js +54 -18
- package/esm/src/kernel/engine/runner/state.d.ts +3 -2
- package/esm/src/kernel/engine/runner/steps.js +30 -15
- package/esm/src/kernel/field-docs.js +2 -2
- package/esm/src/kernel/protocols/protocol.d.ts +4 -1
- package/esm/src/kernel/protocols/runtime.js +1 -0
- package/esm/src/kernel/stop.d.ts +2 -1
- package/esm/src/kernel/tools/call-order.d.ts +4 -1
- package/esm/src/kernel/tools/call-order.js +14 -2
- package/esm/src/kernel/tools/execute.js +34 -14
- package/esm/src/kernel/tools/remote.js +2 -1
- package/esm/src/kernel/tools/types.d.ts +4 -2
- package/esm/src/kernel/turn-events.d.ts +2 -0
- package/esm/src/kernel/turn-events.js +1 -0
- package/esm/src/providers/google/api-error.d.ts +1 -1
- package/esm/src/providers/google/api-error.js +14 -7
- package/package.json +1 -1
|
@@ -146,6 +146,9 @@ function toErrorEvent(err) {
|
|
|
146
146
|
errorKind: errorKind(err),
|
|
147
147
|
...(err instanceof TheoremError && err.copy ? { errorCopy: err.copy } : {}),
|
|
148
148
|
errorInternal: describeError(err),
|
|
149
|
+
...(err instanceof TheoremError && err.refusedMedia
|
|
150
|
+
? { refusedMedia: [...err.refusedMedia] }
|
|
151
|
+
: {}),
|
|
149
152
|
};
|
|
150
153
|
}
|
|
151
154
|
/** Wording already set (host copy) is kept. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** Values that fill a label template's `{param}` placeholders. */
|
|
2
2
|
export type LabelParams = Record<string, string | number>;
|
|
3
3
|
/** Keys are stable API. */
|
|
4
|
-
export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "turn.media_refused_image", "turn.media_refused_video", "turn.media_refused_file", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.cut_off_guidance", "repair.reply_cut_off", "repair.call_cut_off", "repair.call_cut_off_guidance", "repair.nothing_written", "repair.nothing_written_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.email", "detect.hint.phone", "detect.hint.bank_account", "detect.hint.injection", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "stop.answer_missing", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_host_fields", "tool.hook_failed", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.unreachable", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.late_result", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
|
|
4
|
+
export declare const LABEL_KEYS: readonly ["provider.decision_failed", "provider.decision_unavailable", "provider.decision_model_missing", "provider.decision_ended", "provider.decision_images_off", "provider.decision_box_model", "provider.budget_exhausted", "provider.adapter_version", "provider.adapter_methods", "provider.adapter_schemas", "provider.fallback_slot", "provider.model_fallback_slot", "provider.host_model", "turn.media_refused_image", "turn.media_refused_video", "turn.media_refused_file", "turn.media_refused_tool", "provider.turn_profile", "provider.model_binding_missing", "provider.session_closed", "provider.settled_call", "provider.session_unavailable", "provider.live_input", "provider.tool_cancel_missing", "provider.kernel_gate", "provider.event_invalid", "provider.response_identity", "provider.credential_resolution", "provider.credential_shape", "provider.stored_continuation", "provider.continuation_mismatch", "provider.continuation_contract", "provider.stateless_history", "provider.events_after_completion", "provider.duplicate_call", "provider.tool_cancel_live", "provider.complete_unavailable", "provider.parallel_tools", "provider.binding_identity", "provider.profile_type", "provider.event_type", "provider.feature", "provider.input_kind", "provider.output_kind", "provider.builtin", "provider.credential_missing", "provider.profile_configuration", "continue.instruction", "canary.bind_note", "user_data.note", "context.note", "taint.blocked", "taint.reason_steered", "taint.reason_tainted", "taint.destination_blocked", "taint.destination_confirm", "advisory.notice_elevated", "advisory.notice_high", "advisory.guidance", "attachments.too_many_files", "attachments.too_many_of_kind", "attachments.file_too_large", "attachments.turn_too_large", "attachments.not_accepted", "attachments.mime_not_allowed", "attachments.limits_unconfigured", "quota.exhausted", "quota.busy", "error.config", "error.request", "error.conflict", "error.not_found", "error.input", "error.action", "error.auth", "error.rate_limit", "error.busy", "error.unsupported", "error.unavailable", "error.bad_response", "error.network", "error.timeout", "error.safety", "error.blocked", "error.declined", "error.failed", "error.cancelled", "error.internal", "repair.default_guidance", "repair.items_guidance", "repair.cut_off_guidance", "repair.reply_cut_off", "repair.call_cut_off", "repair.call_cut_off_guidance", "repair.nothing_written", "repair.nothing_written_guidance", "repair.prompt_header", "repair.prompt_intro", "repair.prompt_instructions", "repair.history_heading", "repair.section_previous_output", "repair.section_validator_rejection", "repair.section_repair_guidance", "repair.section_instructions", "structured.answer_tool", "structured.answer_missing", "structured.cut_off", "structured.not_json", "compaction.request", "compaction.tool_call", "compaction.tool_result", "compaction.tool_files", "decision.box_image", "detect.blocked", "detect.call_blocked", "detect.output_blocked", "detect.shown_blocked", "detect.hint.ids", "detect.hint.financial", "detect.hint.network", "detect.hint.credentials", "detect.hint.email", "detect.hint.phone", "detect.hint.bank_account", "detect.hint.injection", "detect.hint.canary_leak", "detect.hint.prompt_leak", "detect.hint.marker_leak", "detect.hint.ungiven_images", "detect.hint.ungiven_links", "detect.hint.tool_leak", "detect.hint.own", "egress.default_repair_guidance", "egress.refusal", "egress.refusal_cut", "egress.rejection", "egress.rejection_found", "egress.hint_unscannable", "egress.hint_provider_tool_leak", "thought.omitted_image", "thought.omitted_link", "thought.omitted_instructions", "session.abandon_gated", "session.tool_denied", "session.tool_aborted", "session.sign_in", "sign_in.link", "sign_in.pending", "sign_in.done", "sign_in.declined", "sign_in.expired", "sign_in.out_of_scope", "session.gate_expired", "session.provider_state_unowned", "session.conversation_unknown", "session.full", "session.busy", "session.turn_ended", "session.gate_pending", "session.part_skipped", "live.session_ended", "voice.unsupported", "voice.permission", "voice.unavailable", "voice.failed", "voice.empty", "stop.cancelled", "stop.interrupted", "stop.length", "stop.filtered", "stop.provider_error", "stop.stream_incomplete", "stop.step_limit", "stop.answer_missing", "composer.model", "composer.effort", "tool.awaiting_user", "tool.completed_hidden", "tool.t2_loader_needs_snapshot", "tool.t2_loader_shape", "tool.t2_loader_output_invalid", "tool.execution_uncertain", "tool.input_invalid", "tool.input_host_fields", "tool.hook_failed", "tool.input_invalid_after_mutate", "tool.output_invalid_after_mutate", "tool.handler_no_output", "tool.did_not_run", "tool.unreachable", "tool.page_no_answer", "tool.page_timed_out", "tool.output_invalid", "tool.not_loaded_t2", "tool.wait_unknown", "tool.wait_cycle", "tool.after_field", "tool.background_field", "tool.background_started", "tool.late_result", "tool.not_visible", "tool.builtin_not_enabled", "tool.provider_native", "tool.not_registered", "tool.builtin_needs_snapshot", "tool.not_allowed", "tool.not_eligible", "tool.unsupported_type", "tool.agent_call_limit", "tool.agent_failed", "tool.activity_failed", "protocol.not_served", "protocol.profile_type", "protocol.options", "protocol.option_clash", "protocol.base_options", "protocol.id_clash", "protocol.ws_headers", "protocol.builtin_unwritten", "protocol.provider_missing", "protocol.connection", "protocol.part_schema", "protocol.part_invalid", "protocol.key_invalid", "protocol.no_key", "protocol.reply_shape", "protocol.endpoint_url", "protocol.endpoint_host", "protocol.http", "protocol.no_operation", "protocol.definition", "protocol.provider_id_clash", "protocol.history_needed"];
|
|
5
5
|
/** One of {@linkcode LABEL_KEYS}. */
|
|
6
6
|
export type LabelKey = (typeof LABEL_KEYS)[number];
|
|
7
7
|
/**
|
|
@@ -20,6 +20,7 @@ export const LABEL_KEYS = [
|
|
|
20
20
|
'turn.media_refused_image',
|
|
21
21
|
'turn.media_refused_video',
|
|
22
22
|
'turn.media_refused_file',
|
|
23
|
+
'turn.media_refused_tool',
|
|
23
24
|
'provider.turn_profile',
|
|
24
25
|
'provider.model_binding_missing',
|
|
25
26
|
'provider.session_closed',
|
|
@@ -360,6 +361,7 @@ const DEFAULTS = {
|
|
|
360
361
|
'turn.media_refused_image': '[The user sent a photo that could not be opened.]',
|
|
361
362
|
'turn.media_refused_video': '[The user sent a video that could not be opened.]',
|
|
362
363
|
'turn.media_refused_file': '[The user sent a file that could not be opened.]',
|
|
364
|
+
'turn.media_refused_tool': '[The tool returned a file that could not be opened.]',
|
|
363
365
|
'provider.turn_profile': 'Profile does not run model turns',
|
|
364
366
|
'provider.model_binding_missing': 'Unknown model binding',
|
|
365
367
|
'provider.session_closed': 'Provider session closed unexpectedly',
|
|
@@ -624,6 +626,7 @@ export const LABEL_NOTES = {
|
|
|
624
626
|
'turn.media_refused_image': 'Model-visible note where a refused photo was.',
|
|
625
627
|
'turn.media_refused_video': 'Model-visible note where a refused video was.',
|
|
626
628
|
'turn.media_refused_file': 'Model-visible note where a refused file was.',
|
|
629
|
+
'turn.media_refused_tool': 'Model-visible note where a refused file a tool returned was.',
|
|
627
630
|
'provider.turn_profile': 'Provider validation diagnostic.',
|
|
628
631
|
'provider.model_binding_missing': 'Provider validation diagnostic.',
|
|
629
632
|
'provider.session_closed': 'Provider validation diagnostic.',
|
|
@@ -12,6 +12,8 @@ export type ErrorKind = (typeof ERROR_KINDS)[number];
|
|
|
12
12
|
export interface TheoremErrorOptions extends ErrorOptions {
|
|
13
13
|
/** User wording more specific than the kind's: one line, or one per problem found. */
|
|
14
14
|
copy?: ErrorCopies;
|
|
15
|
+
/** The file types the provider said it cannot read, as it named them. */
|
|
16
|
+
refusedMedia?: readonly string[];
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
19
|
* The error Theorem throws; its kind tells your host which status and wording to use.
|
|
@@ -21,5 +23,6 @@ export interface TheoremErrorOptions extends ErrorOptions {
|
|
|
21
23
|
export declare class TheoremError extends Error {
|
|
22
24
|
readonly kind: ErrorKind;
|
|
23
25
|
readonly copy?: ErrorCopies;
|
|
26
|
+
readonly refusedMedia?: readonly string[];
|
|
24
27
|
constructor(kind: ErrorKind, message: string, options?: TheoremErrorOptions);
|
|
25
28
|
}
|
|
@@ -54,11 +54,14 @@ export const ERROR_KINDS = [
|
|
|
54
54
|
export class TheoremError extends Error {
|
|
55
55
|
kind;
|
|
56
56
|
copy;
|
|
57
|
+
refusedMedia;
|
|
57
58
|
constructor(kind, message, options) {
|
|
58
59
|
super(message, options);
|
|
59
60
|
this.name = 'TheoremError';
|
|
60
61
|
this.kind = kind;
|
|
61
62
|
if (options?.copy)
|
|
62
63
|
this.copy = options.copy;
|
|
64
|
+
if (options?.refusedMedia?.length)
|
|
65
|
+
this.refusedMedia = options.refusedMedia;
|
|
63
66
|
}
|
|
64
67
|
}
|
|
@@ -26,6 +26,11 @@ function builderWording(event) {
|
|
|
26
26
|
}
|
|
27
27
|
/** Raw diagnostics ride on error events, an ended session's close, a tool call's failure, and a hook's crash. */
|
|
28
28
|
function stripErrorInternal(event) {
|
|
29
|
+
if (event.type === 'error' && event.refusedMedia !== undefined) {
|
|
30
|
+
// invariant: what the provider refused is the builder's to read, as `done.mediaRefused` is.
|
|
31
|
+
const { errorInternal: _internal, refusedMedia: _refused, ...rest } = event;
|
|
32
|
+
return rest;
|
|
33
|
+
}
|
|
29
34
|
if ((event.type !== 'error' &&
|
|
30
35
|
event.type !== 'session' &&
|
|
31
36
|
event.type !== 'tool' &&
|
|
@@ -6,12 +6,21 @@ export interface RefusedMediaTry {
|
|
|
6
6
|
dropped: RefusedMedia[];
|
|
7
7
|
said: string;
|
|
8
8
|
originals: Map<number, TurnHistoryMessage>;
|
|
9
|
+
/** The types the provider named, each one dropped once; empty when it named none, and only then are `originals` kept. */
|
|
10
|
+
named: Set<string>;
|
|
9
11
|
}
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Whether the next call has a file to go without. Named types are new to this turn's tries and
|
|
14
|
+
* held by a message; with none named, any file will do, once a turn.
|
|
15
|
+
*/
|
|
16
|
+
export declare function canDropMedia(state: StepExecutionState, named: readonly string[] | undefined, tried: RefusedMediaTry | undefined): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Puts a note where each file was, so the model knows what it could not read: the files of each
|
|
19
|
+
* type the provider named, wherever the conversation holds them, or else every file at
|
|
20
|
+
* `refusedAt`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function dropMedia(state: StepExecutionState, said: string, labels: Profile['labels'], named: readonly string[] | undefined, tried: RefusedMediaTry | undefined): RefusedMediaTry;
|
|
23
|
+
/** The retry failed as well, and the files it went without come back. */
|
|
15
24
|
export declare function restoreMedia(state: StepExecutionState, tried: RefusedMediaTry): void;
|
|
16
25
|
/** What the host reads on `done`. */
|
|
17
26
|
export declare function refusedReport(tried: RefusedMediaTry): MediaRefused;
|
|
@@ -5,7 +5,16 @@ function isMedia(part) {
|
|
|
5
5
|
function holdsMedia(message) {
|
|
6
6
|
return message.parts?.some(isMedia) === true;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
/** why: a provider may name a refused type with parameters the message's file does not carry, or without them, so a type matches on its name alone. */
|
|
9
|
+
function typeKey(mimeType) {
|
|
10
|
+
return (mimeType.split(';')[0] ?? '').trim().toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
function isNamed(part, named) {
|
|
13
|
+
return isMedia(part) && 'mimeType' in part && named.has(typeKey(part.mimeType));
|
|
14
|
+
}
|
|
15
|
+
function noteKey(mimeType, fromTool) {
|
|
16
|
+
if (fromTool)
|
|
17
|
+
return 'turn.media_refused_tool';
|
|
9
18
|
if (mimeType.startsWith('image/'))
|
|
10
19
|
return 'turn.media_refused_image';
|
|
11
20
|
if (mimeType.startsWith('video/'))
|
|
@@ -18,32 +27,59 @@ function refusedAt(state) {
|
|
|
18
27
|
const thisTurn = held.filter((at) => at >= state.turnStart);
|
|
19
28
|
return thisTurn.length > 0 ? thisTurn : held;
|
|
20
29
|
}
|
|
21
|
-
/**
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Whether the next call has a file to go without. Named types are new to this turn's tries and
|
|
32
|
+
* held by a message; with none named, any file will do, once a turn.
|
|
33
|
+
*/
|
|
34
|
+
export function canDropMedia(state, named, tried) {
|
|
35
|
+
if (!named?.length)
|
|
36
|
+
return !tried && state.currentHistory.some(holdsMedia);
|
|
37
|
+
if (tried && tried.named.size === 0)
|
|
38
|
+
return false;
|
|
39
|
+
const fresh = new Set(named.map(typeKey).filter((type) => !tried?.named.has(type)));
|
|
40
|
+
return state.currentHistory.some((message) => message.parts?.some((part) => isNamed(part, fresh)));
|
|
24
41
|
}
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Puts a note where each file was, so the model knows what it could not read: the files of each
|
|
44
|
+
* type the provider named, wherever the conversation holds them, or else every file at
|
|
45
|
+
* `refusedAt`.
|
|
46
|
+
*/
|
|
47
|
+
export function dropMedia(state, said, labels, named, tried) {
|
|
48
|
+
const next = tried ?? {
|
|
49
|
+
dropped: [],
|
|
50
|
+
said: '',
|
|
51
|
+
originals: new Map(),
|
|
52
|
+
named: new Set(),
|
|
53
|
+
};
|
|
54
|
+
next.said = next.said ? `${next.said}\n${said}` : said;
|
|
55
|
+
const types = new Set((named ?? []).map(typeKey));
|
|
56
|
+
for (const type of types)
|
|
57
|
+
next.named.add(type);
|
|
58
|
+
const drops = (part) => (types.size > 0 ? isNamed(part, types) : isMedia(part));
|
|
59
|
+
const at = types.size > 0 ? state.currentHistory.map((_, index) => index) : refusedAt(state);
|
|
60
|
+
for (const index of at) {
|
|
61
|
+
const message = state.currentHistory[index];
|
|
62
|
+
if (!message?.parts?.some(drops))
|
|
31
63
|
continue;
|
|
32
|
-
const from =
|
|
33
|
-
|
|
34
|
-
|
|
64
|
+
const from = index < state.turnStart ? 'history' : message.role === 'tool' ? 'tool' : 'input';
|
|
65
|
+
if (types.size === 0)
|
|
66
|
+
next.originals.set(index, message);
|
|
67
|
+
state.currentHistory[index] = {
|
|
35
68
|
...message,
|
|
36
69
|
parts: message.parts.map((part) => {
|
|
37
|
-
if (part.type === 'text')
|
|
70
|
+
if (!drops(part) || part.type === 'text')
|
|
38
71
|
return part;
|
|
39
|
-
|
|
40
|
-
return {
|
|
72
|
+
next.dropped.push({ mimeType: part.mimeType, from });
|
|
73
|
+
return {
|
|
74
|
+
type: 'text',
|
|
75
|
+
text: labelText(noteKey(part.mimeType, message.role === 'tool'), {}, labels),
|
|
76
|
+
};
|
|
41
77
|
}),
|
|
42
78
|
};
|
|
43
79
|
}
|
|
44
|
-
return
|
|
80
|
+
return next;
|
|
45
81
|
}
|
|
46
|
-
/** The retry failed as well,
|
|
82
|
+
/** The retry failed as well, and the files it went without come back. */
|
|
47
83
|
export function restoreMedia(state, tried) {
|
|
48
84
|
for (const [at, message] of tried.originals)
|
|
49
85
|
state.currentHistory[at] = message;
|
|
@@ -11,6 +11,7 @@ import type { ToolRegistry } from '../../tools/registry.js';
|
|
|
11
11
|
import type { SteerQueue } from '../../tools/steer.js';
|
|
12
12
|
import type { MediaRefused } from '../../turn-events.js';
|
|
13
13
|
import type { InteractionPart, LateResultsSource, ModelBinding, Profile, ResolvedGeneration, TurnEvent, TurnHistoryMessage, TurnRequest, TurnStop, TurnToolResult } from '../../types.js';
|
|
14
|
+
import type { RefusedMediaTry } from './refused-media.js';
|
|
14
15
|
import type { ItemFailure } from './schema-validation.js';
|
|
15
16
|
import type { CallUsage } from './usage.js';
|
|
16
17
|
interface TurnTraceState {
|
|
@@ -41,8 +42,8 @@ interface StepExecutionState {
|
|
|
41
42
|
stepCount: number;
|
|
42
43
|
/** Model calls sent again this turn under the profile's resumption (`executeResumedStep`). */
|
|
43
44
|
resumes: number;
|
|
44
|
-
/**
|
|
45
|
-
|
|
45
|
+
/** The files this turn's refused calls went without: each type the provider named once, or every file once. */
|
|
46
|
+
mediaTry?: RefusedMediaTry;
|
|
46
47
|
mediaRefused?: MediaRefused;
|
|
47
48
|
allEmittedEvents: TurnEvent[];
|
|
48
49
|
attemptEvents: TurnEvent[];
|
|
@@ -20,7 +20,7 @@ import { profileToolsParallel } from '../../tools/resolve.js';
|
|
|
20
20
|
import { findLast } from '../../util/find-last.js';
|
|
21
21
|
import { startToolTrace } from '../tool-trace.js';
|
|
22
22
|
import { startCallTrace } from '../turn-trace.js';
|
|
23
|
-
import { canDropMedia, dropMedia, refusedReport, restoreMedia
|
|
23
|
+
import { canDropMedia, dropMedia, refusedReport, restoreMedia } from './refused-media.js';
|
|
24
24
|
import { applyStageInjects, landLateResultsAt, recordReply, steersToLand, unreadLateResultsAt, } from './stages.js';
|
|
25
25
|
import { modelHistory, recordStepEvent } from './state.js';
|
|
26
26
|
import { inStep, runSideBySide, stepOrder } from './step-order.js';
|
|
@@ -251,7 +251,7 @@ function resumeOf(mayResume, profile, tally) {
|
|
|
251
251
|
*/
|
|
252
252
|
function mediaResumeOf(state, profile, tally) {
|
|
253
253
|
const { stop } = tally;
|
|
254
|
-
if (profile.type !== 'text'
|
|
254
|
+
if (profile.type !== 'text')
|
|
255
255
|
return undefined;
|
|
256
256
|
if (tally.replied || tally.calls.length > 0 || !stop || !isFailedStopKind(stop.kind)) {
|
|
257
257
|
return undefined;
|
|
@@ -259,7 +259,9 @@ function mediaResumeOf(state, profile, tally) {
|
|
|
259
259
|
const failure = tally.failures[0];
|
|
260
260
|
if (failure?.errorKind !== 'unsupported' || !resendsFailedCall(profile, stop))
|
|
261
261
|
return undefined;
|
|
262
|
-
return canDropMedia(state
|
|
262
|
+
return canDropMedia(state, failure.refusedMedia, state.mediaTry)
|
|
263
|
+
? { stop, failure, dropMedia: true }
|
|
264
|
+
: undefined;
|
|
263
265
|
}
|
|
264
266
|
/** Carries what the call's stream guards still hold into the turn's next call. */
|
|
265
267
|
function keepStreamCarry(state, control) {
|
|
@@ -372,31 +374,30 @@ async function* executeAutonomousStep(args, state, buffer = {
|
|
|
372
374
|
* lists, having given no reply and no tool call, is sent again in the same turn
|
|
373
375
|
* after a pause, up to `maxContinues` times a turn (once when the profile sets
|
|
374
376
|
* none). The turn's history, its step count, and what it has read stay as they were.
|
|
375
|
-
* A call the provider refused while it carried files goes
|
|
377
|
+
* A call the provider refused while it carried files goes again without the files of each type
|
|
378
|
+
* the provider named, once per type; when it names none, once a turn without every file, and
|
|
376
379
|
* those files are reported as refused only if that call is accepted.
|
|
377
380
|
*/
|
|
378
381
|
async function* executeResumedStep(args, state, buffer) {
|
|
379
382
|
const limit = profileTurnResumption(args.profile)?.maxContinues ?? 1;
|
|
380
|
-
let
|
|
383
|
+
let droppedHere = false;
|
|
381
384
|
for (;;) {
|
|
382
385
|
const mayResume = args.profile.type === 'text' && state.resumes < limit;
|
|
383
386
|
const outcome = yield* executeAutonomousStep({ ...args, mayResume }, state, buffer);
|
|
384
387
|
if (!outcome.resume) {
|
|
385
|
-
if (
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
else if (tried) {
|
|
389
|
-
state.mediaRefused = refusedReport(tried);
|
|
390
|
-
}
|
|
388
|
+
if (droppedHere)
|
|
389
|
+
settleMediaTry(state);
|
|
391
390
|
return outcome;
|
|
392
391
|
}
|
|
393
392
|
if (outcome.resume.dropMedia) {
|
|
394
|
-
const
|
|
395
|
-
|
|
396
|
-
|
|
393
|
+
const failure = outcome.resume.failure;
|
|
394
|
+
const said = failure?.errorInternal ?? failure?.error ?? '';
|
|
395
|
+
const before = state.mediaTry?.dropped.length ?? 0;
|
|
396
|
+
droppedHere = true;
|
|
397
|
+
state.mediaTry = dropMedia(state, said, args.profile.labels, failure?.refusedMedia, state.mediaTry);
|
|
397
398
|
state.trace.root.event('theorem.step.media_refused', {
|
|
398
399
|
step: state.stepCount,
|
|
399
|
-
files:
|
|
400
|
+
files: state.mediaTry.dropped.length - before,
|
|
400
401
|
error: said,
|
|
401
402
|
});
|
|
402
403
|
state.lastStop = undefined;
|
|
@@ -414,6 +415,20 @@ async function* executeResumedStep(args, state, buffer) {
|
|
|
414
415
|
await waitDefault(AUTO_CONTINUE_DELAY_MS, args.signal);
|
|
415
416
|
}
|
|
416
417
|
}
|
|
418
|
+
/**
|
|
419
|
+
* Reports what the turn went without. Files only guessed at come back when the call failed anyway,
|
|
420
|
+
* since nothing proved them at fault; a file whose type the provider named stays out, and is reported.
|
|
421
|
+
*/
|
|
422
|
+
function settleMediaTry(state) {
|
|
423
|
+
const tried = state.mediaTry;
|
|
424
|
+
if (!tried)
|
|
425
|
+
return;
|
|
426
|
+
if (tried.named.size === 0 && state.lastStop && isFailedStopKind(state.lastStop.kind)) {
|
|
427
|
+
restoreMedia(state, tried);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
state.mediaRefused = refusedReport(tried);
|
|
431
|
+
}
|
|
417
432
|
function toolResultMessage(call, result) {
|
|
418
433
|
return {
|
|
419
434
|
role: 'tool',
|
|
@@ -317,7 +317,7 @@ export const FIELD_DOCS = {
|
|
|
317
317
|
'registerTool.conflictsWith': 'The built-in tools a builtin tool cannot run beside.',
|
|
318
318
|
'registerTool.description': 'Tells the model what the tool does (custom tools only).',
|
|
319
319
|
'registerTool.endpoint': 'The URL the tool calls, with {name} placeholders for mapping.pathParams; the scheme and host must be written out.',
|
|
320
|
-
'registerTool.exposeToModel': '
|
|
320
|
+
'registerTool.exposeToModel': 'false: the model can call the tool but reads only `Completed.` (the tool.completed_hidden label); the person still sees the media it sends.',
|
|
321
321
|
'registerTool.fieldText': 'What the calling model reads on a field of an agent tool input, such as text. A field the profile does not take is refused when the tool registers.',
|
|
322
322
|
'registerTool.handler': 'Your function that runs the tool; it can also yield progress as it goes.',
|
|
323
323
|
'registerTool.headers': "Headers sent with every request to the tool's own origin, never once a redirect leaves it.",
|
|
@@ -378,12 +378,12 @@ export const FIELD_DOCS = {
|
|
|
378
378
|
'runTurn.input.repair': 'A reply your host rejected and why, so that the model writes it again.',
|
|
379
379
|
'runTurn.input.text': "The user's message.",
|
|
380
380
|
'runTurn.input.voice': "Recorded speech sent with the turn; each must pass the profile's inputs.voice.",
|
|
381
|
+
'runTurn.lateResults': 'Your function that gives the background tool calls that settled after their turn, as you stored them. A text turn asks it when it opens, after each step whose calls settled, and once the model has answered. The model reads each result as a tool result, and the turn counts it as a remote read when the call was remote. A result lands once: one the conversation already holds is not read again. Each landing is a late_results event, so a history built from events holds it, and the event names the moment it landed. Once the model has answered, a landing runs another step, so the model answers what it read.',
|
|
381
382
|
'runTurn.links': 'Earlier traces this run follows: a resume, a continue, or a retry.',
|
|
382
383
|
'runTurn.metadata': 'Your own values, kept on the trace; Theorem does not read them.',
|
|
383
384
|
'runTurn.model': "The model id the turn or session picks from the profile's models; refused unless the profile sets allowModelSelect.",
|
|
384
385
|
'runTurn.onAgentCall': 'Runs before each agent tool call, to shape the request to the called agent or to refuse the call.',
|
|
385
386
|
'runTurn.onEvent': "Reads each of the turn's events as it leaves runTurn, before the caller gets it. A throw goes to the caller as hook_failed, which this function does not read, and the turn goes on.",
|
|
386
|
-
'runTurn.lateResults': 'Your function that gives the background tool calls that settled after their turn, as you stored them. A text turn asks it when it opens, after each step whose calls settled, and once the model has answered. The model reads each result as a tool result, and the turn counts it as a remote read when the call was remote. A result lands once: one the conversation already holds is not read again. Each landing is a late_results event, so a history built from events holds it, and the event names the moment it landed. Once the model has answered, a landing runs another step, so the model answers what it read.',
|
|
387
387
|
'runTurn.onStage': 'Your function that sees each stage of a text turn or a live session, and can answer with what the stage allows. A throw at pre_tool fails that call. A throw at any other stage is reported as hook_failed, and the turn goes on without its answer.',
|
|
388
388
|
'runTurn.options': "The value the host's code sets for each option in the profile's identity.options but path; an option or a value the profile does not declare is refused.",
|
|
389
389
|
'runTurn.pageExecution': 'Host-issued browser dispatch with a claimed operation and correlated result.',
|
|
@@ -188,7 +188,10 @@ export interface ProtocolDefinition<O = unknown, S = unknown> {
|
|
|
188
188
|
* `id` when the reply names itself in `X-Generation-Id`.
|
|
189
189
|
*/
|
|
190
190
|
framing?(response: Response, stream: boolean): AsyncIterable<JsonObject>;
|
|
191
|
-
/**
|
|
191
|
+
/**
|
|
192
|
+
* Types a failed answer. Absent: the kind follows the HTTP status. A refusal that names the file
|
|
193
|
+
* types the provider cannot read sets them as `refusedMedia`, so the turn goes on without only those.
|
|
194
|
+
*/
|
|
192
195
|
error?(failure: UpstreamFailure): TheoremError;
|
|
193
196
|
/** What the named embedding model takes. Absent: the kernel holds requests to the profile alone. */
|
|
194
197
|
embeddingLimits?(apiId: string): EmbeddingModelLimits;
|
|
@@ -479,6 +479,7 @@ function failureEvent(error) {
|
|
|
479
479
|
// invariant: `error` is the person's wording, added from the profile's labels; the upstream text is internal.
|
|
480
480
|
errorInternal: describeError(error),
|
|
481
481
|
...(error.copy ? { errorCopy: error.copy } : {}),
|
|
482
|
+
...(error.refusedMedia ? { refusedMedia: [...error.refusedMedia] } : {}),
|
|
482
483
|
};
|
|
483
484
|
}
|
|
484
485
|
function cleanRequest(request) {
|
package/esm/src/kernel/stop.d.ts
CHANGED
|
@@ -37,7 +37,8 @@ export interface ProfileTurnResumptionSpec {
|
|
|
37
37
|
* `stream_incomplete`) again itself when this list is set and names that stop, and the call gave
|
|
38
38
|
* no reply and no tool call: the turn keeps its history, its step count and what it has read. A
|
|
39
39
|
* failure the same call would meet again (the provider refused the request) is not sent again,
|
|
40
|
-
* except
|
|
40
|
+
* except without its files when it carried any: once per type the provider named, or once a turn
|
|
41
|
+
* without this turn's files when it named none. Accepted that way, the turn goes on and
|
|
41
42
|
* `done.mediaRefused` names them. Named here, `length` also repairs a call the output limit cut
|
|
42
43
|
* off: the model gets back what it wrote, or that it wrote nothing, and is asked again to fit.
|
|
43
44
|
* Omitted, a turn sends nothing again.
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import type { StepCall } from '../stages.js';
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* `input` with a field the kernel adds for the model. A tool's own field of that name is refused,
|
|
5
|
+
* never replaced. A field the kernel added before stays, so a registered tool registers again.
|
|
6
|
+
*/
|
|
4
7
|
export declare function withModelField<TIn>(name: string, input: z.ZodType<TIn>, field: string, shape: () => z.ZodType): z.ZodType<TIn>;
|
|
5
8
|
/** `input` with the model's `after` field. */
|
|
6
9
|
export declare function withCallOrder<TIn>(name: string, input: z.ZodType<TIn>): z.ZodType<TIn>;
|
|
@@ -6,14 +6,26 @@ const FIELD = 'after';
|
|
|
6
6
|
function orderField() {
|
|
7
7
|
return z.array(z.string()).optional().describe(labelText('tool.after_field'));
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
9
|
+
/** The fields the kernel added for the model, so a registered tool registered again keeps the one it has. */
|
|
10
|
+
const kernelFields = new WeakSet();
|
|
11
|
+
/**
|
|
12
|
+
* `input` with a field the kernel adds for the model. A tool's own field of that name is refused,
|
|
13
|
+
* never replaced. A field the kernel added before stays, so a registered tool registers again.
|
|
14
|
+
*/
|
|
10
15
|
export function withModelField(name, input, field, shape) {
|
|
16
|
+
const added = () => {
|
|
17
|
+
const made = shape();
|
|
18
|
+
kernelFields.add(made);
|
|
19
|
+
return made;
|
|
20
|
+
};
|
|
11
21
|
const ordered = (schema) => {
|
|
12
22
|
if (schema instanceof z.ZodObject) {
|
|
23
|
+
if (field in schema.shape && kernelFields.has(schema.shape[field]))
|
|
24
|
+
return schema;
|
|
13
25
|
if (field in schema.shape) {
|
|
14
26
|
throw new TheoremError('config', `Tool '${name}' sets ${field}, and its input already has a field of that name`);
|
|
15
27
|
}
|
|
16
|
-
return schema.safeExtend({ [field]:
|
|
28
|
+
return schema.safeExtend({ [field]: added() });
|
|
17
29
|
}
|
|
18
30
|
if (schema instanceof z.ZodDiscriminatedUnion) {
|
|
19
31
|
const options = schema.options.map(ordered);
|
|
@@ -94,15 +94,8 @@ async function* runHandler(handler, input, ctx, shown) {
|
|
|
94
94
|
/** What the model is told of a function tool's output: a fixed finding for a hidden tool, a finding plus the output for an awaiting-user result, otherwise the tool's own summary or finding and its data. */
|
|
95
95
|
export function projectForModel(tool, output, labels) {
|
|
96
96
|
const media = checkedToolOutputParts(output);
|
|
97
|
-
if (tool.exposeToModel === false)
|
|
98
|
-
|
|
99
|
-
const sent = media?.filter((part) => part.type !== 'text' && ('uri' in part || part.sent));
|
|
100
|
-
return {
|
|
101
|
-
finding: labelText('tool.completed_hidden', {}, labels),
|
|
102
|
-
hidden: true,
|
|
103
|
-
...(sent?.length ? { media: sent } : {}),
|
|
104
|
-
};
|
|
105
|
-
}
|
|
97
|
+
if (tool.exposeToModel === false)
|
|
98
|
+
return hiddenResult(media, labels);
|
|
106
99
|
const seen = outputForModel(tool.outputSchema, output);
|
|
107
100
|
if (isAwaitingUserInput(output)) {
|
|
108
101
|
return {
|
|
@@ -121,6 +114,25 @@ export function projectForModel(tool, output, labels) {
|
|
|
121
114
|
: modelResultFromOutput(lean, undefined);
|
|
122
115
|
return withMedia(result, media);
|
|
123
116
|
}
|
|
117
|
+
/** Whether the person is shown a part of a hidden tool's media: what it sent, and each reference. */
|
|
118
|
+
const isShownPart = (part) => part.type !== 'text' && ('uri' in part || part.sent === true);
|
|
119
|
+
/**
|
|
120
|
+
* What the model is told of a hidden tool's call (`exposeToModel: false`), whatever its type: only
|
|
121
|
+
* `Completed.`. Of its media, the person is shown what it sent and its references.
|
|
122
|
+
* invariant: every tool type hides through here, so a hidden tool reads the same on every transport.
|
|
123
|
+
*/
|
|
124
|
+
function hiddenResult(media, labels) {
|
|
125
|
+
const shown = media?.filter(isShownPart);
|
|
126
|
+
return {
|
|
127
|
+
finding: labelText('tool.completed_hidden', {}, labels),
|
|
128
|
+
hidden: true,
|
|
129
|
+
...(shown?.length ? { media: shown } : {}),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** A media hook for a hidden tool: only the entries the person is shown, so no picture is fetched for the model. */
|
|
133
|
+
function shownEntries(media) {
|
|
134
|
+
return (output) => media(output).filter((entry) => !('url' in entry) || entry.sent === true);
|
|
135
|
+
}
|
|
124
136
|
/** How the call's `post_tool` stage gets the output as the model reads it; nothing when the model reads none. */
|
|
125
137
|
function forModelOf(tool) {
|
|
126
138
|
if (tool.exposeToModel === false)
|
|
@@ -343,6 +355,10 @@ async function* withNamedMedia(base, result, named, outputRaw) {
|
|
|
343
355
|
yield toolEvent(base, { phase: 'warning', warning: fetched.warning });
|
|
344
356
|
if (fetched.media.length === 0)
|
|
345
357
|
return result;
|
|
358
|
+
if (result.hidden) {
|
|
359
|
+
const shown = [...(result.media ?? []), ...fetched.media.filter(isShownPart)];
|
|
360
|
+
return { ...result, ...(shown.length ? { media: shown } : {}) };
|
|
361
|
+
}
|
|
346
362
|
const withAll = withMedia(result, [...(result.media ?? []), ...fetched.media]);
|
|
347
363
|
if (fetched.urls.length === 0)
|
|
348
364
|
return withAll;
|
|
@@ -746,7 +762,7 @@ export function authorizeToolReplay(args) {
|
|
|
746
762
|
function authorizeSavedDisclosure(args, tool, outcome) {
|
|
747
763
|
const blocked = () => new TheoremError('blocked', labelText('detect.output_blocked', {}, args.profile.labels));
|
|
748
764
|
const result = outcome.replay.modelResult;
|
|
749
|
-
if (tool.type
|
|
765
|
+
if (tool.type !== 'builtin' &&
|
|
750
766
|
tool.exposeToModel === false &&
|
|
751
767
|
result?.finding !== labelText('tool.completed_hidden', {}, args.profile.labels))
|
|
752
768
|
throw blocked();
|
|
@@ -783,6 +799,10 @@ async function* settleBodyOutcome(args) {
|
|
|
783
799
|
...(outcome.denied ? { denied: true } : {}),
|
|
784
800
|
});
|
|
785
801
|
}
|
|
802
|
+
// why: a sign-in message is the kernel's, not the tool's result, so a hidden tool still says it.
|
|
803
|
+
const hidden = tool.exposeToModel === false && !outcome.unauthenticated;
|
|
804
|
+
const seen = (result) => (hidden ? hiddenResult(result.media, labels) : result);
|
|
805
|
+
const named = tool.type !== 'function' && tool.media;
|
|
786
806
|
return yield* settleToolCall({
|
|
787
807
|
base,
|
|
788
808
|
toolName: name,
|
|
@@ -790,18 +810,18 @@ async function* settleBodyOutcome(args) {
|
|
|
790
810
|
input: safeInput,
|
|
791
811
|
stages,
|
|
792
812
|
guard,
|
|
793
|
-
provisional: { outputRaw: outcome.outputRaw, modelResult: outcome.modelResult },
|
|
813
|
+
provisional: { outputRaw: outcome.outputRaw, modelResult: seen(outcome.modelResult) },
|
|
794
814
|
sources: tool.sources,
|
|
795
815
|
...forModelOf(tool),
|
|
796
|
-
...(
|
|
816
|
+
...(named
|
|
797
817
|
? {
|
|
798
|
-
media: (output) => fetchToolMedia(
|
|
818
|
+
media: (output) => fetchToolMedia(hidden ? shownEntries(named) : named, output, ctx, tool.imageLimits),
|
|
799
819
|
}
|
|
800
820
|
: {}),
|
|
801
821
|
labels: tool.labels,
|
|
802
822
|
reproject: makeReproject((v) => parseToolOutput(tool.output, v),
|
|
803
823
|
// why: A hook's edit replaces the value; the media the tool returned stays.
|
|
804
|
-
(data) => withMedia(modelResultFromOutput(data, tool.outputSchema), outcome.modelResult.media), labels),
|
|
824
|
+
(data) => seen(withMedia(modelResultFromOutput(data, tool.outputSchema), outcome.modelResult.media)), labels),
|
|
805
825
|
});
|
|
806
826
|
}
|
|
807
827
|
function turnReadBack({ modelResult }) {
|
|
@@ -321,6 +321,7 @@ export function outcomeFromUnauth(authRes, sent = false) {
|
|
|
321
321
|
kind: 'ok',
|
|
322
322
|
modelResult: { finding: authRes.modelMessage },
|
|
323
323
|
outputRaw: { unauthenticated: true, message: authRes.modelMessage },
|
|
324
|
+
unauthenticated: true,
|
|
324
325
|
};
|
|
325
326
|
}
|
|
326
327
|
if (authRes.failureMessage === undefined) {
|
|
@@ -533,7 +534,7 @@ function withoutSecret(outcome, secret, outputSchema) {
|
|
|
533
534
|
if (outcome.kind === 'ok') {
|
|
534
535
|
const outputRaw = omitSecret(outcome.outputRaw, secret);
|
|
535
536
|
return {
|
|
536
|
-
|
|
537
|
+
...outcome,
|
|
537
538
|
outputRaw,
|
|
538
539
|
modelResult: withMedia(modelResultFromOutput(outputRaw, outputSchema), outcome.modelResult.media),
|
|
539
540
|
};
|
|
@@ -209,7 +209,7 @@ export interface ToolHostHooks<TIn = unknown> {
|
|
|
209
209
|
* @remarks Runs before the host's `pre_tool` `onStage`.
|
|
210
210
|
*/
|
|
211
211
|
preTool?: (input: TIn, ctx: ToolContext) => import('../stages.js').StageResult | undefined | Promise<import('../stages.js').StageResult | undefined>;
|
|
212
|
-
/**
|
|
212
|
+
/** false: the model can call the tool but reads only `Completed.` (the tool.completed_hidden label); the person still sees the media it sends. */
|
|
213
213
|
exposeToModel?: boolean;
|
|
214
214
|
/**
|
|
215
215
|
* Gives the model an after field on this tool: the tools called in the same step that must settle
|
|
@@ -553,7 +553,7 @@ export interface AgentToolDef extends ToolBase {
|
|
|
553
553
|
* argument. A turn with no background service runs the tool inline.
|
|
554
554
|
*/
|
|
555
555
|
background?: ToolHostHooks<AgentToolInput>['background'];
|
|
556
|
-
/**
|
|
556
|
+
/** false: the model can call the tool but reads only `Completed.` (the tool.completed_hidden label); the person still sees the media it sends. */
|
|
557
557
|
exposeToModel?: boolean;
|
|
558
558
|
}
|
|
559
559
|
export interface RegisteredAgentTool extends AgentToolDef, ToolHostHooks<AgentToolInput>, ToolOutputHooks<AgentToolOutput> {
|
|
@@ -807,6 +807,8 @@ export type ToolBodyOutcome = {
|
|
|
807
807
|
kind: 'ok';
|
|
808
808
|
outputRaw: unknown;
|
|
809
809
|
modelResult: ModelToolResult;
|
|
810
|
+
/** The tool is not signed in, and `modelResult` is the kernel's own words saying so; `exposeToModel: false` does not hide them. */
|
|
811
|
+
unauthenticated?: true;
|
|
810
812
|
} | {
|
|
811
813
|
kind: 'gated';
|
|
812
814
|
gate: ToolGate;
|
|
@@ -806,6 +806,8 @@ export type TurnEvent = {
|
|
|
806
806
|
errorCopy?: ErrorCopies;
|
|
807
807
|
/** Raw diagnostic detail for traces/logs; never surface to end users (`forClient` strips it). */
|
|
808
808
|
errorInternal?: string;
|
|
809
|
+
/** The file types the provider said it cannot read; the turn may send again without them. */
|
|
810
|
+
refusedMedia?: string[];
|
|
809
811
|
};
|
|
810
812
|
/** The `type` of a turn event. */
|
|
811
813
|
export type TurnEventType = TurnEvent['type'];
|
|
@@ -6,5 +6,5 @@ import { TheoremError } from '../../guardrails/error.js';
|
|
|
6
6
|
export declare function readGeminiApiError(record: Record<string, unknown>): TheoremError | null;
|
|
7
7
|
/** The kind is the HTTP status's, unless the body names a refused key. */
|
|
8
8
|
export declare function readNonOkError(response: Response): Promise<TheoremError>;
|
|
9
|
-
/** A non-OK reply's error: the body's own when it names a refused key, else the status's with the body's message. */
|
|
9
|
+
/** A non-OK reply's error: the body's own when it names a refused key, else the status's with the body's message and the types it refused. */
|
|
10
10
|
export declare function statedOrStatusError(stated: TheoremError | null, status: number, fallback: string): TheoremError;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { kindOfHttpStatus, TheoremError } from '../../guardrails/error.js';
|
|
1
|
+
import { kindOfHttpStatus, TheoremError, } from '../../guardrails/error.js';
|
|
2
2
|
import { asRecord } from '../../kernel/engine/record.js';
|
|
3
3
|
const HTTP_STATUS_MIN = 100;
|
|
4
4
|
const HTTP_STATUS_MAX = 599;
|
|
@@ -72,7 +72,7 @@ const BLOCKED_CODES = new Set([
|
|
|
72
72
|
* codes (`malformed_function_call`, `no_image`, …) and any code Google adds later
|
|
73
73
|
* are `bad_response`.
|
|
74
74
|
*/
|
|
75
|
-
function namedCodeError(code, message) {
|
|
75
|
+
function namedCodeError(code, message, options) {
|
|
76
76
|
if (code === 'cancelled')
|
|
77
77
|
return new TheoremError('cancelled', message);
|
|
78
78
|
if (BLOCKED_CODES.has(code))
|
|
@@ -80,7 +80,13 @@ function namedCodeError(code, message) {
|
|
|
80
80
|
const status = NAMED_CODE_STATUS[code];
|
|
81
81
|
return status === undefined
|
|
82
82
|
? new TheoremError('bad_response', message)
|
|
83
|
-
: new TheoremError(kindOfHttpStatus(status), message);
|
|
83
|
+
: new TheoremError(kindOfHttpStatus(status), message, options);
|
|
84
|
+
}
|
|
85
|
+
/** probed 14/11/2026: a refused document names its type, and only the first refused one: "Unsupported mime_type 'text/plain' in document content." */
|
|
86
|
+
const REFUSED_DOCUMENT = /Unsupported mime_type '([^']+)' in document content/;
|
|
87
|
+
function refusedMedia(message) {
|
|
88
|
+
const type = typeof message === 'string' ? REFUSED_DOCUMENT.exec(message)?.[1] : undefined;
|
|
89
|
+
return type ? { refusedMedia: [type] } : undefined;
|
|
84
90
|
}
|
|
85
91
|
/**
|
|
86
92
|
* Interactions (SSE `error` rows, non-OK bodies) and Live (error frames) both send
|
|
@@ -100,10 +106,10 @@ export function readGeminiApiError(record) {
|
|
|
100
106
|
if (refusesKey(details))
|
|
101
107
|
return new TheoremError('auth', text);
|
|
102
108
|
if (typeof code === 'number' && code >= HTTP_STATUS_MIN && code <= HTTP_STATUS_MAX) {
|
|
103
|
-
return new TheoremError(kindOfHttpStatus(code), text);
|
|
109
|
+
return new TheoremError(kindOfHttpStatus(code), text, refusedMedia(message));
|
|
104
110
|
}
|
|
105
111
|
return typeof code === 'string'
|
|
106
|
-
? namedCodeError(code, text)
|
|
112
|
+
? namedCodeError(code, text, refusedMedia(message))
|
|
107
113
|
: new TheoremError('bad_response', text);
|
|
108
114
|
}
|
|
109
115
|
/** The kind is the HTTP status's, unless the body names a refused key. */
|
|
@@ -115,11 +121,12 @@ export async function readNonOkError(response) {
|
|
|
115
121
|
const parsed = parseRecord(text);
|
|
116
122
|
return statedOrStatusError(parsed ? readGeminiApiError(parsed) : null, response.status, `Gemini HTTP ${response.status}: ${text}`);
|
|
117
123
|
}
|
|
118
|
-
/** A non-OK reply's error: the body's own when it names a refused key, else the status's with the body's message. */
|
|
124
|
+
/** A non-OK reply's error: the body's own when it names a refused key, else the status's with the body's message and the types it refused. */
|
|
119
125
|
export function statedOrStatusError(stated, status, fallback) {
|
|
120
126
|
if (stated?.kind === 'auth')
|
|
121
127
|
return stated;
|
|
122
|
-
|
|
128
|
+
const refused = stated?.refusedMedia ? { refusedMedia: stated.refusedMedia } : undefined;
|
|
129
|
+
return new TheoremError(kindOfHttpStatus(status), stated?.message ?? fallback, refused);
|
|
123
130
|
}
|
|
124
131
|
function parseRecord(text) {
|
|
125
132
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theoremjs/agents",
|
|
3
|
-
"version": "0.3.5-next.
|
|
3
|
+
"version": "0.3.5-next.953.gf255d6a",
|
|
4
4
|
"description": "A flat TypeScript agent kernel for typed profiles, deterministic turn execution, registered tools, provider adapters, guardrails, and host-injected traces.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agent",
|