@myagentroam/agent 0.9.61 → 0.9.64
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/LICENSE +202 -0
- package/NOTICE +4 -0
- package/dist/cli/codex-auth-file.js +268 -3
- package/dist/cli/main.js +427 -2
- package/dist/error.js +66 -2
- package/dist/host/contracts.js +20 -2
- package/dist/host/local-agent-host.js +146 -2
- package/dist/host/local-process-executor.js +39 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +12 -2
- package/dist/model/anthropic-messages.js +474 -2
- package/dist/model/anthropic-thinking.js +33 -2
- package/dist/model/catalog-file.js +88 -2
- package/dist/model/configuration.js +137 -2
- package/dist/model/contracts.d.ts +2 -0
- package/dist/model/contracts.js +1 -2
- package/dist/model/http.js +529 -2
- package/dist/model/openai-responses.d.ts +2 -0
- package/dist/model/openai-responses.js +1602 -2
- package/dist/model/runtime-credential.js +35 -2
- package/dist/model/sse.js +199 -2
- package/dist/prompts/compact.js +25 -2
- package/dist/prompts/core.js +37 -2
- package/dist/prompts/execution-budget.js +10 -2
- package/dist/prompts/index.d.ts +3 -2
- package/dist/prompts/index.js +47 -2
- package/dist/prompts/modes.js +25 -2
- package/dist/prompts/output.js +1 -2
- package/dist/prompts/resources.d.ts +1 -1
- package/dist/prompts/resources.js +31 -2
- package/dist/prompts/subagent.d.ts +6 -0
- package/dist/prompts/subagent.js +29 -2
- package/dist/prompts/title.js +9 -2
- package/dist/prompts/workflow.js +32 -2
- package/dist/runtime/async-queue.js +62 -2
- package/dist/runtime/builtin-skills.js +19 -2
- package/dist/runtime/byted-ark-imagegen-skill.js +18 -2
- package/dist/runtime/codex-imagegen-skill.js +35 -2
- package/dist/runtime/compact.js +225 -2
- package/dist/runtime/context-gc-runtime.d.ts +35 -0
- package/dist/runtime/context-gc-runtime.js +163 -0
- package/dist/runtime/context-gc-tokens.d.ts +24 -0
- package/dist/runtime/context-gc-tokens.js +83 -0
- package/dist/runtime/context-gc.d.ts +66 -0
- package/dist/runtime/context-gc.js +909 -0
- package/dist/runtime/context-projection.d.ts +23 -0
- package/dist/runtime/context-projection.js +203 -0
- package/dist/runtime/environment-context.js +41 -2
- package/dist/runtime/execution-policy.js +13 -2
- package/dist/runtime/instructions.js +298 -2
- package/dist/runtime/normalize.js +40 -2
- package/dist/runtime/plan-output.js +95 -2
- package/dist/runtime/rollout-budget.d.ts +2 -0
- package/dist/runtime/rollout-budget.js +92 -2
- package/dist/runtime/token-budget.d.ts +2 -0
- package/dist/runtime/token-budget.js +44 -2
- package/dist/sdk/agent.js +1804 -2
- package/dist/sdk/types.d.ts +15 -5
- package/dist/sdk/types.js +1 -2
- package/dist/session/catalog.d.ts +5 -4
- package/dist/session/catalog.js +133 -2
- package/dist/session/context-gc-checkpoint.d.ts +148 -0
- package/dist/session/context-gc-checkpoint.js +665 -0
- package/dist/session/context-gc-replacements.d.ts +52 -0
- package/dist/session/context-gc-replacements.js +226 -0
- package/dist/session/context-gc-subagent.d.ts +43 -0
- package/dist/session/context-gc-subagent.js +267 -0
- package/dist/session/jsonl-store.d.ts +42 -4
- package/dist/session/jsonl-store.js +1657 -2
- package/dist/subagent/scheduler.d.ts +35 -2
- package/dist/subagent/scheduler.js +249 -2
- package/dist/subagent/session-controller.d.ts +94 -0
- package/dist/subagent/session-controller.js +675 -0
- package/dist/tools/agent-cancel.js +28 -2
- package/dist/tools/{task.d.ts → agent-message.d.ts} +2 -2
- package/dist/tools/agent-message.js +51 -0
- package/dist/tools/agent-start.js +84 -2
- package/dist/tools/agent-wait.js +53 -2
- package/dist/tools/apply-patch.js +478 -2
- package/dist/tools/code-mode.js +370 -2
- package/dist/tools/codex-image-gen.js +312 -2
- package/dist/tools/exec.js +455 -2
- package/dist/tools/execution-limits.d.ts +1 -0
- package/dist/tools/execution-limits.js +44 -2
- package/dist/tools/local-registry.js +233 -2
- package/dist/tools/local-workspace-tools.js +28 -2
- package/dist/tools/question.js +124 -2
- package/dist/tools/read-context.d.ts +16 -0
- package/dist/tools/read-context.js +85 -0
- package/dist/tools/read.d.ts +13 -0
- package/dist/tools/read.js +294 -2
- package/dist/tools/registry.js +76 -2
- package/dist/tools/runtime-context.d.ts +9 -2
- package/dist/tools/runtime-context.js +1 -2
- package/dist/tools/runtime.js +21 -2
- package/dist/tools/scheduler.js +41 -2
- package/dist/tools/shared/path-resolver.js +133 -2
- package/dist/tools/skill.js +37 -2
- package/dist/tools/todo-read.js +10 -2
- package/dist/tools/todo-write.js +65 -2
- package/dist/tools/todo.js +67 -2
- package/dist/tools/view-image.js +81 -2
- package/dist/tools/web-fetch.js +11 -2
- package/dist/tools/web-search.js +14 -2
- package/package.json +17 -15
- package/README.md +0 -3
- package/dist/runtime/prompt.d.ts +0 -9
- package/dist/runtime/prompt.js +0 -2
- package/dist/tools/shared/secret-redaction.d.ts +0 -4
- package/dist/tools/shared/secret-redaction.js +0 -2
- package/dist/tools/task-cancel.d.ts +0 -7
- package/dist/tools/task-cancel.js +0 -2
- package/dist/tools/task-output.d.ts +0 -7
- package/dist/tools/task-output.js +0 -2
- package/dist/tools/task-tools.d.ts +0 -36
- package/dist/tools/task-tools.js +0 -2
- package/dist/tools/task.js +0 -2
- package/dist/tools/todo-tools.d.ts +0 -25
- package/dist/tools/todo-tools.js +0 -2
|
@@ -1,2 +1,474 @@
|
|
|
1
|
-
|
|
2
|
-
const _0x4ea3c7=_0x5138;(function(_0x5a31f4,_0x736093){const _0x59ba48=_0x5138,_0x44b335=_0x5a31f4();while(!![]){try{const _0x5bf2e4=parseInt(_0x59ba48(0x12b))/(-0x26*0xc+0x1601+-0x1438)+parseInt(_0x59ba48(0x12f))/(0x9a9*0x1+0x1*-0x233+0x9f*-0xc)+parseInt(_0x59ba48(0x128))/(-0x1d*-0xb8+0x2*-0x1a2+0x3*-0x5db)+-parseInt(_0x59ba48(0x19d))/(-0xd*0x178+0xe8d+0x48f)+-parseInt(_0x59ba48(0x15c))/(0x1f00*0x1+-0x694*0x1+-0x1867)+parseInt(_0x59ba48(0x187))/(-0x4*-0x946+-0x3*-0xc67+-0x4a47)*(-parseInt(_0x59ba48(0x18b))/(0x185b+-0x1*-0x1bca+-0x341e))+parseInt(_0x59ba48(0x11a))/(-0x1737*-0x1+-0x85*0x17+-0xb3c)*(-parseInt(_0x59ba48(0x164))/(0x233f+0x2060+-0xd3*0x52));if(_0x5bf2e4===_0x736093)break;else _0x44b335['push'](_0x44b335['shift']());}catch(_0x48fc22){_0x44b335['push'](_0x44b335['shift']());}}}(_0x5528,0x6*0x29706+0x1d381*-0x4+-0x73b1));import{randomUUID}from'node:crypto';import{MarAgentError}from'../error.js';import{resolveModelDefaultReasoningEffort}from'./configuration.js';import{isRetryableUpstreamOverloadCode,number,parseSse,record,text}from'./sse.js';import{fetchModelResponse,modelRetryMaxAttempts,isRetryableModelRequestError,isRetryableModelTransportError,waitForModelRetry}from'./http.js';import{anthropicThinkingBlock,assertAnthropicThinkingBudget}from'./anthropic-thinking.js';export class AnthropicMessagesAdapter{[_0x4ea3c7(0x18f)];constructor(_0x294b97){const _0x2ae04f=_0x4ea3c7;this[_0x2ae04f(0x18f)]=_0x294b97;}async*[_0x4ea3c7(0x152)](_0x8cca5a,_0x11938e){const _0x2609ad=_0x4ea3c7,_0x76e09a=_0x8cca5a[_0x2609ad(0x19f)]??{'attempts':0x0},_0x559df7=modelRetryMaxAttempts(_0x76e09a);while(_0x76e09a[_0x2609ad(0x1ae)]<_0x559df7){const _0x260c7a=_0x76e09a[_0x2609ad(0x1ae)]++,_0x537f00=[];let _0x5d5639=![];try{for await(const _0x14600e of this[_0x2609ad(0x12a)](_0x8cca5a,_0x11938e)){if(!_0x5d5639&&!isSemanticEvent(_0x14600e)){_0x537f00[_0x2609ad(0x191)](_0x14600e);continue;}if(!_0x5d5639){_0x5d5639=!![];for(const _0x413591 of _0x537f00)yield _0x413591;_0x537f00[_0x2609ad(0x154)]=-0x15*0xa3+0x1020+0x2c1*-0x1;}yield _0x14600e;}return;}catch(_0xc37e58){if(_0x11938e[_0x2609ad(0x1aa)]||_0x5d5639||!(isRetryableModelRequestError(_0xc37e58)||isAnthropicRecoverableStreamError(_0xc37e58))||_0x260c7a+(-0x2*0x502+-0x14b2+0xa3d*0x3)>=_0x559df7)throw _0xc37e58;await waitForModelRetry(retryAfter(_0xc37e58),_0x260c7a,_0x11938e);}}}async*[_0x4ea3c7(0x12a)](_0x872048,_0x4f0407){const _0x2596dd=_0x4ea3c7,_0x569e09=this[_0x2596dd(0x18f)][_0x2596dd(0x123)];if(_0x569e09===undefined)throw new MarAgentError(_0x2596dd(0x177),_0x2596dd(0x115));const _0x177ee2=_0x872048[_0x2596dd(0x17e)]===![]?[]:_0x872048[_0x2596dd(0x16a)][_0x2596dd(0x144)](_0x239850=>({'name':_0x239850[_0x2596dd(0x153)],'description':_0x239850[_0x2596dd(0x189)],'input_schema':_0x239850[_0x2596dd(0x171)]})),_0x598f0d=await fetchModelResponse(this[_0x2596dd(0x18f)][_0x2596dd(0x13a)][_0x2596dd(0x124)](/\/$/,'')+_0x2596dd(0x1a6),{'method':_0x2596dd(0x19c),'headers':{'x-api-key':_0x569e09[_0x2596dd(0x163)](),'anthropic-version':_0x2596dd(0x148),'content-type':_0x2596dd(0x150),'accept':_0x2596dd(0x122)},'body':JSON[_0x2596dd(0x113)]({'model':this[_0x2596dd(0x18f)][_0x2596dd(0x186)],'system':[{'type':_0x2596dd(0x13f),'text':_0x872048[_0x2596dd(0x14e)],'cache_control':{'type':_0x2596dd(0x192)}}],'messages':anthropicMessages(_0x872048[_0x2596dd(0x180)],_0x872048[_0x2596dd(0x175)],_0x872048[_0x2596dd(0x16a)],this[_0x2596dd(0x18f)]['id']),'tools':_0x177ee2,'stream':!![],'max_tokens':this[_0x2596dd(0x18f)][_0x2596dd(0x147)],'output_config':{'effort':_0x872048[_0x2596dd(0x14b)]?.[_0x2596dd(0x1a0)]()||resolveModelDefaultReasoningEffort(this[_0x2596dd(0x18f)])}})},_0x4f0407,-0x1*-0x135a+0x1*-0xde2+-0x577*0x1),_0xb57eba=new Map(),_0x4f37af=new Set();let _0x454508=_0x2596dd(0x182)+randomUUID(),_0x2f001f,_0x142548,_0x273982=-0xfa1*-0x2+0x1939*0x1+-0x387b;for await(const _0x3e0971 of parseAnthropicSse(_0x598f0d,_0x4f0407)){const _0x1c2362=record(_0x3e0971),_0x206bbd=text(_0x1c2362[_0x2596dd(0x1b1)])??_0x2596dd(0x155),_0x5ef42f=number(_0x1c2362[_0x2596dd(0x17a)])??-(-0xd*-0xc3+0x3*0xa61+0x37*-0xbf);if(_0x206bbd===_0x2596dd(0x1a4)){const _0xc46b7=record(_0x1c2362[_0x2596dd(0x1a5)]);_0x454508=text(_0xc46b7['id'])??_0x454508,yield{'type':_0x2596dd(0x174),'responseId':_0x454508};const _0x5d2245=record(_0xc46b7[_0x2596dd(0x194)]),_0xc49b9b=number(_0x5d2245[_0x2596dd(0x167)]),_0xb39c35=number(_0x5d2245[_0x2596dd(0x12c)]),_0x24a838=number(_0x5d2245[_0x2596dd(0x13d)]);_0x142548=inputUsage(_0xc49b9b,_0xb39c35,_0x24a838),yield{'type':_0x2596dd(0x194),'usage':{..._0x142548}};}else{if(_0x206bbd===_0x2596dd(0x162)){const _0x44bb48=record(_0x1c2362[_0x2596dd(0x166)]),_0x3847ab=text(_0x44bb48[_0x2596dd(0x1b1)])??_0x2596dd(0x155);if(_0x3847ab===_0x2596dd(0x199)&&(_0x44bb48[_0x2596dd(0x199)]!==undefined&&typeof _0x44bb48[_0x2596dd(0x199)]!==_0x2596dd(0x1a2)||_0x44bb48[_0x2596dd(0x190)]!==undefined&&typeof _0x44bb48[_0x2596dd(0x190)]!==_0x2596dd(0x1a2)))throw new MarAgentError(_0x2596dd(0x17b),_0x2596dd(0x158));if([_0x2596dd(0x133),_0x2596dd(0x17f),_0x2596dd(0x199),_0x2596dd(0x183)][_0x2596dd(0x1ad)](_0x3847ab)){for(const _0x397910 of _0x4f37af){const _0x2b288a=_0xb57eba[_0x2596dd(0x149)](_0x397910);if(_0x2b288a?.[_0x2596dd(0x1b1)]===_0x2596dd(0x13f))yield{'type':_0x2596dd(0x146),'itemId':_0x2b288a['id'],'phase':_0x2596dd(0x14c),'text':_0x2b288a[_0x2596dd(0x13f)]};}_0x4f37af[_0x2596dd(0x188)]();}const _0x1318b2=text(_0x44bb48['id'])??_0x454508+_0x2596dd(0x125)+_0x5ef42f;_0xb57eba[_0x2596dd(0x134)](_0x5ef42f,{'type':_0x3847ab,'id':_0x1318b2,'name':text(_0x44bb48[_0x2596dd(0x153)]),'json':Object[_0x2596dd(0x15d)](record(_0x44bb48[_0x2596dd(0x184)]))[_0x2596dd(0x154)]?JSON[_0x2596dd(0x113)](_0x44bb48[_0x2596dd(0x184)]):'','text':text(_0x44bb48[_0x2596dd(0x13f)])??text(_0x44bb48[_0x2596dd(0x199)])??'','signature':text(_0x44bb48[_0x2596dd(0x190)])??'',..._0x3847ab===_0x2596dd(0x183)?{'data':_0x44bb48[_0x2596dd(0x143)]}:{}});if(_0x3847ab===_0x2596dd(0x13f)&&text(_0x44bb48[_0x2596dd(0x13f)]))yield{'type':_0x2596dd(0x160),'itemId':_0x1318b2,'phase':_0x2596dd(0x14c),'delta':text(_0x44bb48[_0x2596dd(0x13f)])??''};else{if(_0x3847ab===_0x2596dd(0x17f))yield{'type':_0x2596dd(0x172),'itemId':_0x1318b2,'kind':text(_0x44bb48[_0x2596dd(0x153)])===_0x2596dd(0x1a8)?_0x2596dd(0x1a1):_0x2596dd(0x121),'status':_0x2596dd(0x19e),'summary':{}};}}else{if(_0x206bbd===_0x2596dd(0x136)){const _0x20119e=record(_0x1c2362[_0x2596dd(0x156)]),_0x5b1088=_0xb57eba[_0x2596dd(0x149)](_0x5ef42f);if(_0x5b1088===undefined)continue;const _0x26345c=text(_0x20119e[_0x2596dd(0x1b1)]);if(_0x26345c===_0x2596dd(0x178)){const _0x372c05=text(_0x20119e[_0x2596dd(0x13f)])??'';_0x5b1088[_0x2596dd(0x13f)]+=_0x372c05,yield{'type':_0x2596dd(0x160),'itemId':_0x5b1088['id'],'phase':_0x2596dd(0x14c),'delta':_0x372c05};}else{if(_0x26345c===_0x2596dd(0x159))_0x5b1088[_0x2596dd(0x13f)]+=text(_0x20119e[_0x2596dd(0x199)])??'';else{if(_0x26345c===_0x2596dd(0x126)){if(_0x5b1088[_0x2596dd(0x1b1)]!==_0x2596dd(0x199)||typeof _0x20119e[_0x2596dd(0x190)]!==_0x2596dd(0x1a2))throw new MarAgentError(_0x2596dd(0x17b),_0x2596dd(0x11d));_0x5b1088[_0x2596dd(0x190)]+=_0x20119e[_0x2596dd(0x190)];}else{if(_0x26345c===_0x2596dd(0x116))_0x5b1088[_0x2596dd(0x168)]+=text(_0x20119e[_0x2596dd(0x15e)])??'';else{if(_0x26345c===_0x2596dd(0x135)){const _0x28ff67=record(_0x20119e[_0x2596dd(0x11c)]),_0x4fe9e6=text(_0x28ff67[_0x2596dd(0x129)]);if(_0x4fe9e6)yield{'type':_0x2596dd(0x11c),'citation':{'id':_0x5b1088['id']+_0x2596dd(0x118),'url':_0x4fe9e6,'title':text(_0x28ff67[_0x2596dd(0x16b)])}};}}}}}}else{if(_0x206bbd===_0x2596dd(0x140)){const _0x5919c5=_0xb57eba[_0x2596dd(0x149)](_0x5ef42f);if(_0x5919c5?.[_0x2596dd(0x1b1)]===_0x2596dd(0x13f))_0x4f37af[_0x2596dd(0x173)](_0x5ef42f);else{if(_0x5919c5?.[_0x2596dd(0x1b1)]===_0x2596dd(0x199)||_0x5919c5?.[_0x2596dd(0x1b1)]===_0x2596dd(0x183)){if(_0x5919c5[_0x2596dd(0x1b1)]===_0x2596dd(0x183)||_0x5919c5[_0x2596dd(0x190)]){const _0x80f6c3=anthropicThinkingBlock(_0x5919c5[_0x2596dd(0x1b1)]===_0x2596dd(0x199)?{'type':_0x2596dd(0x199),'thinking':_0x5919c5[_0x2596dd(0x13f)],'signature':_0x5919c5[_0x2596dd(0x190)]}:{'type':_0x2596dd(0x183),'data':_0x5919c5[_0x2596dd(0x143)]});if(!_0x80f6c3)throw new MarAgentError(_0x2596dd(0x17b),_0x2596dd(0x114));_0x273982+=Buffer[_0x2596dd(0x1a9)](JSON[_0x2596dd(0x113)](_0x80f6c3)),assertAnthropicThinkingBudget(_0x273982),yield{'type':_0x2596dd(0x16c),'provider':_0x2596dd(0x176),'item':_0x80f6c3};}if(_0x5919c5[_0x2596dd(0x1b1)]===_0x2596dd(0x199)&&_0x5919c5[_0x2596dd(0x13f)])yield{'type':_0x2596dd(0x19a),'itemId':_0x5919c5['id'],'text':_0x5919c5[_0x2596dd(0x13f)]};}else{if(_0x5919c5?.[_0x2596dd(0x1b1)]===_0x2596dd(0x133)){let _0x256147={},_0x55b367;try{_0x256147=JSON[_0x2596dd(0x1ab)](_0x5919c5[_0x2596dd(0x168)]||'{}');}catch{_0x55b367=_0x2596dd(0x1ac);}yield{'type':_0x2596dd(0x12d),'callId':_0x5919c5['id'],'name':_0x5919c5[_0x2596dd(0x153)]??_0x2596dd(0x155),'arguments':_0x256147,..._0x55b367?{'argumentError':_0x55b367}:{}};}}}}else{if(_0x206bbd===_0x2596dd(0x142)){const _0x53d25b=record(_0x1c2362[_0x2596dd(0x156)]),_0x283c19=text(_0x53d25b[_0x2596dd(0x193)]);_0x2f001f=_0x283c19===_0x2596dd(0x133)?_0x2596dd(0x133):_0x283c19===_0x2596dd(0x120)?_0x2596dd(0x15a):_0x283c19===_0x2596dd(0x18a)?_0x2596dd(0x1b2):_0x2596dd(0x1a7);const _0x2ae3c6=_0x2f001f===_0x2596dd(0x1a7)?_0x2596dd(0x185):_0x2596dd(0x14c);for(const _0x6850fa of _0x4f37af){const _0x245115=_0xb57eba[_0x2596dd(0x149)](_0x6850fa);if(_0x245115?.[_0x2596dd(0x1b1)]===_0x2596dd(0x13f))yield{'type':_0x2596dd(0x146),'itemId':_0x245115['id'],'phase':_0x2ae3c6,'text':_0x245115[_0x2596dd(0x13f)]};}_0x4f37af[_0x2596dd(0x188)]();const _0x1696cc=record(_0x1c2362[_0x2596dd(0x194)]),_0x53cc47=number(_0x1696cc[_0x2596dd(0x167)]),_0x43b346=number(_0x1696cc[_0x2596dd(0x12c)]),_0x41f188=number(_0x1696cc[_0x2596dd(0x13d)]),_0x6e604c=inputUsage(_0x53cc47,_0x43b346,_0x41f188);if((_0x6e604c[_0x2596dd(0x14a)]??-0xc*-0x167+0x1*-0x1849+-0x17*-0x53)>0x4d4+0xbea+-0x10be)_0x142548=_0x6e604c;yield{'type':_0x2596dd(0x194),'usage':{..._0x142548,'outputTokens':number(_0x1696cc[_0x2596dd(0x179)])}};}else{if(_0x206bbd===_0x2596dd(0x161))yield{'type':_0x2596dd(0x1a7),'reason':_0x2f001f??_0x2596dd(0x1a7)};else{if(_0x206bbd===_0x2596dd(0x1af)){const _0x460f2f=record(_0x1c2362[_0x2596dd(0x1af)]),_0x4e4159=boundedError(text(_0x460f2f[_0x2596dd(0x13e)])??text(_0x460f2f[_0x2596dd(0x1b1)]),-0x26eb+-0x1*-0x26f6+0x45),_0x54f2f1=boundedError(text(_0x460f2f[_0x2596dd(0x1a5)]),0xe1*0x11+0x1*-0xb+0x2a*-0x43),_0x40ddb4=isRetryableUpstreamOverloadCode(_0x4e4159);throw new MarAgentError(_0x4e4159??_0x2596dd(0x17b),_0x54f2f1??_0x2596dd(0x19b),_0x40ddb4?{'details':{'retryAfter':_0x598f0d[_0x2596dd(0x15f)][_0x2596dd(0x149)](_0x2596dd(0x198))},'retryable':!![]}:{});}else yield{'type':_0x2596dd(0x155),'nativeType':_0x206bbd};}}}}}}}if(_0x2f001f===undefined)throw new MarAgentError(_0x2596dd(0x17b),_0x2596dd(0x130),{'retryable':!![],'details':{'reason':_0x2596dd(0x17d),'retryAfter':_0x598f0d[_0x2596dd(0x15f)][_0x2596dd(0x149)](_0x2596dd(0x198))}});}}async function*parseAnthropicSse(_0x200aa6,_0x1ae20e){const _0xb476f7=_0x4ea3c7;try{yield*parseSse(_0x200aa6,_0x1ae20e);}catch(_0x13705c){if(_0x200aa6['ok']&&!_0x1ae20e[_0xb476f7(0x1aa)]&&(isRetryableModelTransportError(_0x13705c)||_0x13705c instanceof MarAgentError&&_0x13705c[_0xb476f7(0x145)]?.[_0xb476f7(0x132)]===_0xb476f7(0x16e)))throw new MarAgentError(_0xb476f7(0x17b),_0xb476f7(0x181),{'retryable':!![],'details':{'reason':_0xb476f7(0x17d),'retryAfter':_0x200aa6[_0xb476f7(0x15f)][_0xb476f7(0x149)](_0xb476f7(0x198))},'cause':_0x13705c});throw _0x13705c;}}function _0x5138(_0x5ecbbc,_0x368c7e){_0x5ecbbc=_0x5ecbbc-(0x749*-0x5+0x1be0+0x2*0x4d0);const _0x18fa73=_0x5528();let _0x3de5f2=_0x18fa73[_0x5ecbbc];if(_0x5138['gBQTXp']===undefined){var _0x133f87=function(_0x2dccbb){const _0x5bea19='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';let _0x216383='',_0x8d131f='';for(let _0x14a8f1=-0xf4e+0x24ce+0x560*-0x4,_0x32b90b,_0x1e1f66,_0x25c328=0x807*-0x2+-0x6b6+0x16c4;_0x1e1f66=_0x2dccbb['charAt'](_0x25c328++);~_0x1e1f66&&(_0x32b90b=_0x14a8f1%(0x1ef8+0x36*-0x26+-0x8*0x2de)?_0x32b90b*(0x4*0x379+-0x17f*0x2+0x5e*-0x1d)+_0x1e1f66:_0x1e1f66,_0x14a8f1++%(0xece*0x1+-0x1852+0x1e8*0x5))?_0x216383+=String['fromCharCode'](0x3*0xae5+0x25*-0x4a+-0xa7f*0x2&_0x32b90b>>(-(0x2*0x7f+-0x12*0x112+0x18*0xc3)*_0x14a8f1&-0x7c4+-0xc86+-0x41*-0x50)):0x1b5c+-0x1*0x89+0x2fb*-0x9){_0x1e1f66=_0x5bea19['indexOf'](_0x1e1f66);}for(let _0x1522a0=0xf*0x125+-0x5cf*-0x1+-0x16fa*0x1,_0x4458d9=_0x216383['length'];_0x1522a0<_0x4458d9;_0x1522a0++){_0x8d131f+='%'+('00'+_0x216383['charCodeAt'](_0x1522a0)['toString'](0x1*-0x392+-0x9f*-0x9+-0xa7*0x3))['slice'](-(0xc63+0x1e5*0xa+-0x1b*0x129));}return decodeURIComponent(_0x8d131f);};_0x5138['LAJXtT']=_0x133f87,_0x5138['oMPloG']={},_0x5138['gBQTXp']=!![];}const _0x5e6ef0=_0x18fa73[0xc2d*0x1+-0x1795+0xb68];_0x5138['BsWHfc']!==_0x5e6ef0&&(_0x5138['oMPloG']={},_0x5138['BsWHfc']=_0x5e6ef0);const _0x1dc1bf=_0x5138['oMPloG'][_0x5ecbbc];return _0x1dc1bf===undefined?(_0x3de5f2=_0x5138['LAJXtT'](_0x3de5f2),_0x5138['oMPloG'][_0x5ecbbc]=_0x3de5f2):_0x3de5f2=_0x1dc1bf,_0x3de5f2;}function isSemanticEvent(_0x2ff314){const _0x4b194d=_0x4ea3c7;return _0x2ff314[_0x4b194d(0x1b1)]!==_0x4b194d(0x174)&&_0x2ff314[_0x4b194d(0x1b1)]!==_0x4b194d(0x194);}export function isAnthropicRecoverableStreamError(_0x523ba1){const _0x1c15de=_0x4ea3c7;return _0x523ba1 instanceof MarAgentError&&_0x523ba1[_0x1c15de(0x165)]&&(isRetryableUpstreamOverloadCode(_0x523ba1[_0x1c15de(0x13e)])||_0x523ba1[_0x1c15de(0x145)]?.[_0x1c15de(0x132)]===_0x1c15de(0x17d));}function retryAfter(_0x1f534f){const _0x8cb7d5=_0x4ea3c7,_0x4f8639=_0x1f534f[_0x8cb7d5(0x145)]?.[_0x8cb7d5(0x14d)];return typeof _0x4f8639===_0x8cb7d5(0x1a2)?_0x4f8639:null;}function inputUsage(_0x1babc4,_0x333822,_0x6a708d){return{'inputTokens':_0x1babc4,'contextInputTokens':tokenSum(_0x1babc4,_0x333822,_0x6a708d),'cacheReadTokens':_0x333822,'cacheWriteTokens':_0x6a708d};}function tokenSum(..._0x2e1853){const _0x3e975b=_0x4ea3c7;return _0x2e1853[_0x3e975b(0x11f)](_0x4e1fca=>_0x4e1fca!==undefined)?_0x2e1853[_0x3e975b(0x13c)]((_0xc7904e,_0x56e1cb)=>_0xc7904e+(_0x56e1cb??-0x1193*-0x1+-0x1f13+0x2*0x6c0),0x46b+-0x125*-0x2+0x11*-0x65):undefined;}function boundedError(_0x4fe0f2,_0x229ec7){const _0x3cefaf=_0x4ea3c7,_0xe24b73=_0x4fe0f2?.[_0x3cefaf(0x1a0)]();if(!_0xe24b73)return undefined;return[..._0xe24b73][_0x3cefaf(0x1a3)](0x2*-0x31a+0x91+-0x27*-0x25,_0x229ec7)[_0x3cefaf(0x195)]('');}function anthropicMessages(_0xab6f7e,_0x3e5856,_0x3d3036=[],_0x19b6be){const _0xe4f177=_0x4ea3c7,_0x35a0a6=_0xab6f7e[_0xe4f177(0x117)](_0x1c17ba=>_0x1c17ba[_0xe4f177(0x13b)]===_0xe4f177(0x197)),_0x44a602=new Map(_0x3d3036[_0xe4f177(0x144)](_0x39d332=>[_0x39d332[_0xe4f177(0x153)],_0x39d332])),_0x549d2a=[];let _0x2071c5=0x8c9+0x2468+0x1*-0x2d31;for(const [_0x23960d,_0x598769]of _0xab6f7e[_0xe4f177(0x11b)]()){const _0x24fdbe=_0x598769[_0xe4f177(0x13b)]===_0xe4f177(0x197)?uniqueModelImages(_0x598769[_0xe4f177(0x175)],_0x23960d===_0x35a0a6?_0x3e5856:undefined):[];if(_0x598769[_0xe4f177(0x13b)]===_0xe4f177(0x17c)){if(_0x598769[_0xe4f177(0x17c)]!==_0xe4f177(0x176)||_0x598769[_0xe4f177(0x157)]!==_0x19b6be)continue;const _0x2ec0c3=anthropicThinkingBlock(_0x598769[_0xe4f177(0x131)]);if(!_0x2ec0c3)continue;_0x2071c5+=Buffer[_0xe4f177(0x1a9)](JSON[_0xe4f177(0x113)](_0x2ec0c3)),assertAnthropicThinkingBudget(_0x2071c5);const _0x23a5ec=_0x549d2a['at'](-(-0x129b+-0xa*-0x12a+0x6f8));if(_0x23a5ec?.[_0xe4f177(0x13b)]===_0xe4f177(0x169)&&Array[_0xe4f177(0x196)](_0x23a5ec[_0xe4f177(0x138)]))_0x23a5ec[_0xe4f177(0x138)][_0xe4f177(0x191)](_0x2ec0c3);else _0x549d2a[_0xe4f177(0x191)]({'role':_0xe4f177(0x169),'content':[_0x2ec0c3]});continue;}if(_0x598769[_0xe4f177(0x13b)]===_0xe4f177(0x170)){const _0x3a87bb={'type':_0xe4f177(0x133),'id':_0x598769[_0xe4f177(0x127)],'name':_0x598769[_0xe4f177(0x153)],'input':anthropicToolInput(_0x598769,_0x44a602[_0xe4f177(0x149)](_0x598769[_0xe4f177(0x153)]??''))},_0x23461b=_0x549d2a['at'](-(0x243c+-0xbb7+-0x6*0x416));if(_0x23461b?.[_0xe4f177(0x13b)]===_0xe4f177(0x169)&&Array[_0xe4f177(0x196)](_0x23461b[_0xe4f177(0x138)]))_0x23461b[_0xe4f177(0x138)][_0xe4f177(0x191)](_0x3a87bb);else _0x549d2a[_0xe4f177(0x191)]({'role':_0xe4f177(0x169),'content':[_0x3a87bb]});continue;}if(_0x598769[_0xe4f177(0x13b)]===_0xe4f177(0x11e)){const _0x202b49={'type':_0xe4f177(0x139),'tool_use_id':_0x598769[_0xe4f177(0x127)],'content':_0x598769[_0xe4f177(0x175)]?.[_0xe4f177(0x154)]?[{'type':_0xe4f177(0x13f),'text':_0x598769[_0xe4f177(0x138)]},..._0x598769[_0xe4f177(0x175)][_0xe4f177(0x144)](_0x1a8627=>({'type':_0xe4f177(0x18e),'source':{'type':_0xe4f177(0x12e),'media_type':_0x1a8627[_0xe4f177(0x18c)],'data':_0x1a8627[_0xe4f177(0x16d)]}}))]:_0x598769[_0xe4f177(0x138)]},_0xbd27c5=_0x549d2a['at'](-(0x1*-0x822+-0x15c1+-0xef2*-0x2));if(_0xbd27c5?.[_0xe4f177(0x13b)]===_0xe4f177(0x197)&&Array[_0xe4f177(0x196)](_0xbd27c5[_0xe4f177(0x138)]))_0xbd27c5[_0xe4f177(0x138)][_0xe4f177(0x191)](_0x202b49);else _0x549d2a[_0xe4f177(0x191)]({'role':_0xe4f177(0x197),'content':[_0x202b49]});continue;}if(_0x598769[_0xe4f177(0x13b)]===_0xe4f177(0x169)){if(!_0x598769[_0xe4f177(0x138)])continue;const _0x5193db={'type':_0xe4f177(0x13f),'text':_0x598769[_0xe4f177(0x138)]},_0xeec477=_0x549d2a['at'](-(-0x903+0x1a83+0x117f*-0x1));if(_0xeec477?.[_0xe4f177(0x13b)]===_0xe4f177(0x169)&&Array[_0xe4f177(0x196)](_0xeec477[_0xe4f177(0x138)]))_0xeec477[_0xe4f177(0x138)][_0xe4f177(0x191)](_0x5193db);else _0x549d2a[_0xe4f177(0x191)]({'role':_0xe4f177(0x169),'content':[_0x5193db]});continue;}_0x549d2a[_0xe4f177(0x191)]({'role':_0x598769[_0xe4f177(0x13b)],'content':_0x24fdbe[_0xe4f177(0x154)]>0x1e2c+0x1f4f+-0x3d7b?[..._0x24fdbe[_0xe4f177(0x144)](_0x3224f7=>({'type':_0xe4f177(0x18e),'source':{'type':_0xe4f177(0x12e),'media_type':_0x3224f7[_0xe4f177(0x18c)],'data':_0x3224f7[_0xe4f177(0x16d)]}})),{'type':_0xe4f177(0x13f),'text':_0x598769[_0xe4f177(0x138)]}]:_0x598769[_0xe4f177(0x138)]});}for(const _0x35698e of _0x549d2a[_0xe4f177(0x1a3)](-(0x65*-0x49+0x77*-0x4f+0x2bb*0x18)))_0x35698e[_0xe4f177(0x138)]=withCacheBreakpoint(_0x35698e[_0xe4f177(0x138)]);return _0x549d2a;}function uniqueModelImages(..._0x34f338){const _0x5cdbc5=_0x4ea3c7,_0x34d141=new Set();return _0x34f338[_0x5cdbc5(0x119)](_0x3c83db=>(_0x3c83db??[])[_0x5cdbc5(0x119)](_0x4cea41=>{const _0x9d03de=_0x5cdbc5,_0xcc8d8f=_0x4cea41[_0x9d03de(0x18c)]+'\x00'+_0x4cea41[_0x9d03de(0x16d)];if(_0x34d141[_0x9d03de(0x1b0)](_0xcc8d8f))return[];return _0x34d141[_0x9d03de(0x173)](_0xcc8d8f),[_0x4cea41];}));}function anthropicToolInput(_0x46e9d4,_0x46cbb6){const _0x4d6b6d=_0x4ea3c7;if(isObject(_0x46e9d4[_0x4d6b6d(0x15b)]))return _0x46e9d4[_0x4d6b6d(0x15b)];if(_0x46e9d4[_0x4d6b6d(0x16f)]!==_0x4d6b6d(0x151))return{};const _0x213d36=freeformInputProperty(_0x46cbb6?.[_0x4d6b6d(0x171)]);return{[_0x213d36??_0x4d6b6d(0x184)]:String(_0x46e9d4[_0x4d6b6d(0x15b)]??'')};}function freeformInputProperty(_0x571726){const _0x1b2bde=_0x4ea3c7;if(_0x571726?.[_0x1b2bde(0x1b1)]!==_0x1b2bde(0x18d)||!isObject(_0x571726[_0x1b2bde(0x14f)])||!Array[_0x1b2bde(0x196)](_0x571726[_0x1b2bde(0x141)]))return undefined;const _0x124dea=_0x571726[_0x1b2bde(0x141)][_0x1b2bde(0x137)](_0x53bdff=>typeof _0x53bdff===_0x1b2bde(0x1a2));if(_0x124dea[_0x1b2bde(0x154)]!==-0x79d+-0x958+0x10f6)return undefined;const _0x4f4b46=_0x571726[_0x1b2bde(0x14f)][_0x124dea[0xd03+-0x15a7+-0x13c*-0x7]];return isObject(_0x4f4b46)&&_0x4f4b46[_0x1b2bde(0x1b1)]===_0x1b2bde(0x1a2)?_0x124dea[-0x47*0x61+-0x1ea1+0xe62*0x4]:undefined;}function _0x5528(){const _0x1616e5=['z2v0','y29UDgv4DeLUChv0vg9Rzw5Z','CMvHC29UAw5NrwzMB3j0','y29TBwvUDgfYEq','CMv0CNLbzNrLCG','C3LZDgvT','ChjVCgvYDgLLCW','yxbWBgLJyxrPB24VANnVBG','zNjLzwzVCM0','C3rHCNq','BMfTzq','BgvUz3rO','Dw5RBM93BG','zgvSDge','ChjVDMLKzxjnB2rLBeLK','qw50AhjVCgLJihjLDhvYBMvKigfUigLUDMfSAwqGBMf0AxzLihrOAw5RAw5NigjSB2nRlG','DgHPBMTPBMDFzgvSDge','Cgf1C2u','yxjNDw1LBNrZ','mte4mtq0mhb2rK9wyW','A2v5CW','CgfYDgLHBf9QC29U','AgvHzgvYCW','Dgv4Dc5KzwX0yq','BwvZC2fNzv9ZDg9W','y29UDgvUDf9IBg9JA19ZDgfYDa','CMv2zwfS','oun1DNHnBa','CMv0CNLHyMXL','y29UDgvUDf9IBg9JAW','Aw5WDxrFDg9Rzw5Z','ANnVBG','yxnZAxn0yw50','Dg9VBhm','DgL0Bgu','CMvZCg9UC2uUAxrLBq','zgf0yujHC2u2na','tu9eruXFu1rsrufnx0LetevFveLnru9vva','Aw5WDxrnB2rL','Dg9VBf9JywXS','Aw5WDxrty2HLBwe','Ag9ZDgvKx3rVB2W','ywrK','CMvZCg9UC2uUC3rHCNrLza','Aw1Hz2vZ','qu5usfjpueLdx01fu1nbr0vt','tufsx0fhru5ux01prevmx0nptKzjr19jtLzbteLe','Dgv4Df9KzwX0yq','B3v0Chv0x3rVA2vUCW','Aw5KzxG','tufsx0fhru5ux01prevmx1bst1rpq09mx0vsuK9s','ChjVDMLKzxi','qu5usfjpueLdx01fu1nbr0vtx0vbuKXzx0vprG','ywXSB3DuB29SCW','C2vYDMvYx3rVB2XFDxnL','BwvZC2fNzxm','qw50AhjVCgLJie1LC3nHz2vZihn0CMvHBsb3yxmGAw50zxjYDxb0zwqU','yw50AhjVCgLJlq','CMvKywn0zwrFDgHPBMTPBMC','Aw5WDxq','zMLUywXFyw5ZD2vY','Bw9KzwXjza','mtm3otrvDNLevKq','y2XLyxi','zgvZy3jPChrPB24','Bwf4x3rVA2vUCW','mZCXrg1utvzs','BwLTzvr5Cgu','B2jQzwn0','Aw1Hz2u','y29UzMLNDxjHDgLVBG','C2LNBMf0DxjL','ChvZAa','zxbOzw1LCMfS','C3rVCf9YzwfZB24','DxnHz2u','AM9PBG','AxnbCNjHEq','DxnLCG','CMv0CNKTywz0zxi','DgHPBMTPBMC','CMvHC29UAw5NlNn1Bw1HCNK','qw50AhjVCgLJie1LC3nHz2vZihjLCxvLC3qGzMfPBgvKlG','ue9tva','mJC3mZC4ohvewNbwBa','uLvotKLorW','CMv0CNLtDgf0zq','DhjPBq','v0vcx0zfveni','C3rYAw5N','C2XPy2u','BwvZC2fNzv9ZDgfYDa','BwvZC2fNzq','l3yXl21LC3nHz2vZ','y29TCgXLDgvK','D2vIx2zLDgnO','yNL0zuXLBMD0Aa','ywjVCNrLza','CgfYC2u','vg9VBcbHCMD1BwvUDhmGyxjLigLUDMfSAwqGsLnptI4','Aw5JBhvKzxm','yxr0zw1WDhm','zxjYB3i','AgfZ','DhLWzq','Bwf4x291Dhb1Da','C3rYAw5NAwz5','qw50AhjVCgLJihjLDhvYBMvKigfUigLUDMfSAwqGB3iGB3zLCNnPEMvKig5HDgL2zsb0AgLUA2LUzYbIBg9JAY4','qw50AhjVCgLJie1LC3nHz2vZihjLCxvPCMvZigeGC3rHDgLJiefqssbRzxKU','Aw5WDxrFANnVBL9KzwX0yq','zMLUzeXHC3rjBMrLEa','lwnPDgf0Aw9U','zMXHDe1HCa','mJuZmdaXnNrhuNfyEG','zw50CMLLCW','y2L0yxrPB24','qw50AhjVCgLJihjLDhvYBMvKigfUigLUDMfSAwqGDgHPBMTPBMCGC2LNBMf0DxjLigrLBhrHlG','Dg9VBa','C29Tzq','Cgf1C2vFDhvYBG','v0vcx1nfqvjdsa','Dgv4Dc9LDMvUDc1ZDhjLyw0','yxbPs2v5','CMvWBgfJzq','oMnVBNrLBNqT','C2LNBMf0DxjLx2rLBhrH','y2fSBeLK','mJu3otaXnKj4v3Lmqq','DxjS','C3rHCNrbDhrLBxb0','nZG2mtbwwgLvAwC','y2fJAgvFCMvHzf9PBNb1Df90B2TLBNm','y2XPzw50x3rVB2WUy2fSBa','yMfZzty0','mtG3otiZmfLND21jqW','qw50AhjVCgLJie1LC3nHz2vZihn0CMvHBsbLBMrLzcb3AxrOB3v0igeGDgvYBwLUywWGzxzLBNqU','AxrLBq','CMvHC29U','Dg9VBf91C2u','C2v0','y2L0yxrPB25Zx2rLBhrH','y29UDgvUDf9IBg9JA19KzwX0yq','zMLSDgvY','y29UDgvUDa','Dg9VBf9Yzxn1Bhq','yMfZzvvYBa','CM9Szq','CMvKDwnL','y2fJAgvFy3jLyxrPB25FAw5WDxrFDg9Rzw5Z','y29Kzq','Dgv4Da','y29UDgvUDf9IBg9JA19ZDg9W','CMvXDwLYzwq','BwvZC2fNzv9KzwX0yq','zgf0yq','BwfW','zgv0ywLSCW','Dgv4Dc5JB21WBgv0zwq','Bwf4t3v0Chv0vg9Rzw5Z','mJaYmY0WnI0Wmq'];_0x5528=function(){return _0x1616e5;};return _0x5528();}function isObject(_0x3a980e){const _0x4a35ae=_0x4ea3c7;return _0x3a980e!==null&&typeof _0x3a980e===_0x4a35ae(0x18d)&&!Array[_0x4a35ae(0x196)](_0x3a980e);}function withCacheBreakpoint(_0x1cf1c3){const _0x156d30=_0x4ea3c7;if(typeof _0x1cf1c3===_0x156d30(0x1a2))return[{'type':_0x156d30(0x13f),'text':_0x1cf1c3,'cache_control':{'type':_0x156d30(0x192)}}];if(!Array[_0x156d30(0x196)](_0x1cf1c3)||_0x1cf1c3[_0x156d30(0x154)]===-0x24eb+-0x1238+0x3723)return _0x1cf1c3;const _0xb6ce96=[..._0x1cf1c3],_0x17b21a=_0xb6ce96[_0x156d30(0x117)](_0x21fdff=>isObject(_0x21fdff)&&_0x21fdff[_0x156d30(0x1b1)]!==_0x156d30(0x199)&&_0x21fdff[_0x156d30(0x1b1)]!==_0x156d30(0x183)),_0x6c1ef5=_0xb6ce96[_0x17b21a];if(!_0x6c1ef5||typeof _0x6c1ef5!==_0x156d30(0x18d))return _0x1cf1c3;return _0xb6ce96[_0x17b21a]={..._0x6c1ef5,'cache_control':{'type':_0x156d30(0x192)}},_0xb6ce96;}
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { MarAgentError } from '../error.js';
|
|
3
|
+
import { resolveModelDefaultReasoningEffort } from './configuration.js';
|
|
4
|
+
import { isRetryableUpstreamOverloadCode, number, parseSse, record, text } from './sse.js';
|
|
5
|
+
import { fetchModelResponse, modelRetryMaxAttempts, isRetryableModelRequestError, isRetryableModelTransportError, waitForModelRetry } from './http.js';
|
|
6
|
+
import { anthropicThinkingBlock, assertAnthropicThinkingBudget } from './anthropic-thinking.js';
|
|
7
|
+
export class AnthropicMessagesAdapter {
|
|
8
|
+
configuration;
|
|
9
|
+
constructor(configuration) {
|
|
10
|
+
this.configuration = configuration;
|
|
11
|
+
}
|
|
12
|
+
async *start(request, signal) {
|
|
13
|
+
const retryState = request.retryState ?? { attempts: 0 };
|
|
14
|
+
const maxAttempts = modelRetryMaxAttempts(retryState);
|
|
15
|
+
while (retryState.attempts < maxAttempts) {
|
|
16
|
+
const attempt = retryState.attempts++;
|
|
17
|
+
const prelude = [];
|
|
18
|
+
let sawSemanticEvent = false;
|
|
19
|
+
try {
|
|
20
|
+
for await (const event of this.startAttempt(request, signal)) {
|
|
21
|
+
if (!sawSemanticEvent && !isSemanticEvent(event)) {
|
|
22
|
+
prelude.push(event);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (!sawSemanticEvent) {
|
|
26
|
+
sawSemanticEvent = true;
|
|
27
|
+
for (const pending of prelude)
|
|
28
|
+
yield pending;
|
|
29
|
+
prelude.length = 0;
|
|
30
|
+
}
|
|
31
|
+
yield event;
|
|
32
|
+
}
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (signal.aborted ||
|
|
37
|
+
sawSemanticEvent ||
|
|
38
|
+
!(isRetryableModelRequestError(error) || isAnthropicRecoverableStreamError(error)) ||
|
|
39
|
+
attempt + 1 >= maxAttempts)
|
|
40
|
+
throw error;
|
|
41
|
+
await waitForModelRetry(retryAfter(error), attempt, signal);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async *startAttempt(request, signal) {
|
|
46
|
+
const apiKey = this.configuration.apiKey;
|
|
47
|
+
if (apiKey === undefined)
|
|
48
|
+
throw new MarAgentError('MAR_AGENT_MODEL_CONFIG_INVALID', 'Anthropic Messages requires a static API key.');
|
|
49
|
+
const tools = request.allowTools === false
|
|
50
|
+
? []
|
|
51
|
+
: request.tools.map((tool) => ({
|
|
52
|
+
name: tool.name,
|
|
53
|
+
description: tool.description,
|
|
54
|
+
input_schema: tool.inputSchema
|
|
55
|
+
}));
|
|
56
|
+
const response = await fetchModelResponse(`${this.configuration.baseUrl.replace(/\/$/, '')}/v1/messages`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'x-api-key': apiKey.reveal(),
|
|
60
|
+
'anthropic-version': '2023-06-01',
|
|
61
|
+
'content-type': 'application/json',
|
|
62
|
+
accept: 'text/event-stream'
|
|
63
|
+
},
|
|
64
|
+
body: JSON.stringify({
|
|
65
|
+
model: this.configuration.modelId,
|
|
66
|
+
system: [
|
|
67
|
+
{
|
|
68
|
+
type: 'text',
|
|
69
|
+
text: request.system,
|
|
70
|
+
cache_control: { type: 'ephemeral' }
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
messages: anthropicMessages(request.messages, request.images, request.tools, this.configuration.id),
|
|
74
|
+
tools,
|
|
75
|
+
stream: true,
|
|
76
|
+
max_tokens: this.configuration.maxOutputTokens,
|
|
77
|
+
output_config: {
|
|
78
|
+
effort: request.reasoningEffort?.trim() ||
|
|
79
|
+
resolveModelDefaultReasoningEffort(this.configuration)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
}, signal, 1);
|
|
83
|
+
const blocks = new Map();
|
|
84
|
+
const pendingTextBlocks = new Set();
|
|
85
|
+
let responseId = `anthropic-${randomUUID()}`;
|
|
86
|
+
let terminal;
|
|
87
|
+
let latestInputUsage;
|
|
88
|
+
let thinkingBytes = 0;
|
|
89
|
+
for await (const raw of parseAnthropicSse(response, signal)) {
|
|
90
|
+
const event = record(raw);
|
|
91
|
+
const type = text(event.type) ?? 'unknown';
|
|
92
|
+
const index = number(event.index) ?? -1;
|
|
93
|
+
if (type === 'message_start') {
|
|
94
|
+
const message = record(event.message);
|
|
95
|
+
responseId = text(message.id) ?? responseId;
|
|
96
|
+
yield { type: 'response.started', responseId };
|
|
97
|
+
const usage = record(message.usage);
|
|
98
|
+
const inputTokens = number(usage.input_tokens);
|
|
99
|
+
const cacheReadTokens = number(usage.cache_read_input_tokens);
|
|
100
|
+
const cacheWriteTokens = number(usage.cache_creation_input_tokens);
|
|
101
|
+
latestInputUsage = inputUsage(inputTokens, cacheReadTokens, cacheWriteTokens);
|
|
102
|
+
yield {
|
|
103
|
+
type: 'usage',
|
|
104
|
+
usage: {
|
|
105
|
+
...latestInputUsage
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
else if (type === 'content_block_start') {
|
|
110
|
+
const block = record(event.content_block);
|
|
111
|
+
const blockType = text(block.type) ?? 'unknown';
|
|
112
|
+
if (blockType === 'thinking' &&
|
|
113
|
+
((block.thinking !== undefined && typeof block.thinking !== 'string') ||
|
|
114
|
+
(block.signature !== undefined && typeof block.signature !== 'string')))
|
|
115
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Anthropic returned an invalid native thinking block.');
|
|
116
|
+
if (['tool_use', 'server_tool_use', 'thinking', 'redacted_thinking'].includes(blockType)) {
|
|
117
|
+
for (const blockIndex of pendingTextBlocks) {
|
|
118
|
+
const pending = blocks.get(blockIndex);
|
|
119
|
+
if (pending?.type === 'text')
|
|
120
|
+
yield {
|
|
121
|
+
type: 'text.completed',
|
|
122
|
+
itemId: pending.id,
|
|
123
|
+
phase: 'commentary',
|
|
124
|
+
text: pending.text
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
pendingTextBlocks.clear();
|
|
128
|
+
}
|
|
129
|
+
const id = text(block.id) ?? `${responseId}:content-${index}`;
|
|
130
|
+
blocks.set(index, {
|
|
131
|
+
type: blockType,
|
|
132
|
+
id,
|
|
133
|
+
name: text(block.name),
|
|
134
|
+
json: Object.keys(record(block.input)).length ? JSON.stringify(block.input) : '',
|
|
135
|
+
text: text(block.text) ?? text(block.thinking) ?? '',
|
|
136
|
+
signature: text(block.signature) ?? '',
|
|
137
|
+
...(blockType === 'redacted_thinking' ? { data: block.data } : {})
|
|
138
|
+
});
|
|
139
|
+
if (blockType === 'text' && text(block.text))
|
|
140
|
+
yield {
|
|
141
|
+
type: 'text.delta',
|
|
142
|
+
itemId: id,
|
|
143
|
+
phase: 'commentary',
|
|
144
|
+
delta: text(block.text) ?? ''
|
|
145
|
+
};
|
|
146
|
+
else if (blockType === 'server_tool_use')
|
|
147
|
+
yield {
|
|
148
|
+
type: 'hosted_tool',
|
|
149
|
+
itemId: id,
|
|
150
|
+
kind: text(block.name) === 'web_fetch' ? 'WEB_FETCH' : 'WEB_SEARCH',
|
|
151
|
+
status: 'RUNNING',
|
|
152
|
+
summary: {}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
else if (type === 'content_block_delta') {
|
|
156
|
+
const delta = record(event.delta);
|
|
157
|
+
const block = blocks.get(index);
|
|
158
|
+
if (block === undefined)
|
|
159
|
+
continue;
|
|
160
|
+
const deltaType = text(delta.type);
|
|
161
|
+
if (deltaType === 'text_delta') {
|
|
162
|
+
const value = text(delta.text) ?? '';
|
|
163
|
+
block.text += value;
|
|
164
|
+
yield { type: 'text.delta', itemId: block.id, phase: 'commentary', delta: value };
|
|
165
|
+
}
|
|
166
|
+
else if (deltaType === 'thinking_delta')
|
|
167
|
+
block.text += text(delta.thinking) ?? '';
|
|
168
|
+
else if (deltaType === 'signature_delta') {
|
|
169
|
+
if (block.type !== 'thinking' || typeof delta.signature !== 'string')
|
|
170
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Anthropic returned an invalid thinking signature delta.');
|
|
171
|
+
block.signature += delta.signature;
|
|
172
|
+
}
|
|
173
|
+
else if (deltaType === 'input_json_delta')
|
|
174
|
+
block.json += text(delta.partial_json) ?? '';
|
|
175
|
+
else if (deltaType === 'citations_delta') {
|
|
176
|
+
const citation = record(delta.citation);
|
|
177
|
+
const url = text(citation.url);
|
|
178
|
+
if (url)
|
|
179
|
+
yield {
|
|
180
|
+
type: 'citation',
|
|
181
|
+
citation: { id: `${block.id}-citation`, url, title: text(citation.title) }
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (type === 'content_block_stop') {
|
|
186
|
+
const block = blocks.get(index);
|
|
187
|
+
if (block?.type === 'text')
|
|
188
|
+
pendingTextBlocks.add(index);
|
|
189
|
+
else if (block?.type === 'thinking' || block?.type === 'redacted_thinking') {
|
|
190
|
+
if (block.type === 'redacted_thinking' || block.signature) {
|
|
191
|
+
const item = anthropicThinkingBlock(block.type === 'thinking'
|
|
192
|
+
? { type: 'thinking', thinking: block.text, signature: block.signature }
|
|
193
|
+
: { type: 'redacted_thinking', data: block.data });
|
|
194
|
+
if (!item)
|
|
195
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Anthropic returned an invalid or oversized native thinking block.');
|
|
196
|
+
thinkingBytes += Buffer.byteLength(JSON.stringify(item));
|
|
197
|
+
assertAnthropicThinkingBudget(thinkingBytes);
|
|
198
|
+
yield { type: 'response.item', provider: 'ANTHROPIC_MESSAGES', item };
|
|
199
|
+
}
|
|
200
|
+
if (block.type === 'thinking' && block.text)
|
|
201
|
+
yield { type: 'reasoning.summary', itemId: block.id, text: block.text };
|
|
202
|
+
}
|
|
203
|
+
else if (block?.type === 'tool_use') {
|
|
204
|
+
let args = {};
|
|
205
|
+
let argumentError;
|
|
206
|
+
try {
|
|
207
|
+
args = JSON.parse(block.json || '{}');
|
|
208
|
+
}
|
|
209
|
+
catch {
|
|
210
|
+
argumentError = 'Tool arguments are invalid JSON.';
|
|
211
|
+
}
|
|
212
|
+
yield {
|
|
213
|
+
type: 'client_tool.call',
|
|
214
|
+
callId: block.id,
|
|
215
|
+
name: block.name ?? 'unknown',
|
|
216
|
+
arguments: args,
|
|
217
|
+
...(argumentError ? { argumentError } : {})
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
else if (type === 'message_delta') {
|
|
222
|
+
const delta = record(event.delta);
|
|
223
|
+
const reason = text(delta.stop_reason);
|
|
224
|
+
terminal =
|
|
225
|
+
reason === 'tool_use'
|
|
226
|
+
? 'tool_use'
|
|
227
|
+
: reason === 'pause_turn'
|
|
228
|
+
? 'pause'
|
|
229
|
+
: reason === 'max_tokens'
|
|
230
|
+
? 'max_output'
|
|
231
|
+
: 'completed';
|
|
232
|
+
const phase = terminal === 'completed' ? 'final_answer' : 'commentary';
|
|
233
|
+
for (const blockIndex of pendingTextBlocks) {
|
|
234
|
+
const block = blocks.get(blockIndex);
|
|
235
|
+
if (block?.type === 'text')
|
|
236
|
+
yield { type: 'text.completed', itemId: block.id, phase, text: block.text };
|
|
237
|
+
}
|
|
238
|
+
pendingTextBlocks.clear();
|
|
239
|
+
const usage = record(event.usage);
|
|
240
|
+
const inputTokens = number(usage.input_tokens);
|
|
241
|
+
const cacheReadTokens = number(usage.cache_read_input_tokens);
|
|
242
|
+
const cacheWriteTokens = number(usage.cache_creation_input_tokens);
|
|
243
|
+
const deltaInputUsage = inputUsage(inputTokens, cacheReadTokens, cacheWriteTokens);
|
|
244
|
+
if ((deltaInputUsage.contextInputTokens ?? 0) > 0)
|
|
245
|
+
latestInputUsage = deltaInputUsage;
|
|
246
|
+
yield {
|
|
247
|
+
type: 'usage',
|
|
248
|
+
usage: {
|
|
249
|
+
...latestInputUsage,
|
|
250
|
+
outputTokens: number(usage.output_tokens)
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
else if (type === 'message_stop') {
|
|
255
|
+
yield { type: 'completed', reason: terminal ?? 'completed' };
|
|
256
|
+
}
|
|
257
|
+
else if (type === 'error') {
|
|
258
|
+
const upstream = record(event.error);
|
|
259
|
+
const code = boundedError(text(upstream.code) ?? text(upstream.type), 80);
|
|
260
|
+
const message = boundedError(text(upstream.message), 1_000);
|
|
261
|
+
const retryable = isRetryableUpstreamOverloadCode(code);
|
|
262
|
+
throw new MarAgentError(code ?? 'MAR_AGENT_MODEL_PROTOCOL_ERROR', message ?? 'Anthropic Messages request failed.', retryable
|
|
263
|
+
? {
|
|
264
|
+
details: { retryAfter: response.headers.get('retry-after') },
|
|
265
|
+
retryable: true
|
|
266
|
+
}
|
|
267
|
+
: {});
|
|
268
|
+
}
|
|
269
|
+
else
|
|
270
|
+
yield { type: 'unknown', nativeType: type };
|
|
271
|
+
}
|
|
272
|
+
if (terminal === undefined)
|
|
273
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Anthropic Messages stream ended without a terminal event.', {
|
|
274
|
+
retryable: true,
|
|
275
|
+
details: {
|
|
276
|
+
reason: 'ANTHROPIC_MESSAGES_EARLY_EOF',
|
|
277
|
+
retryAfter: response.headers.get('retry-after')
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async function* parseAnthropicSse(response, signal) {
|
|
283
|
+
try {
|
|
284
|
+
yield* parseSse(response, signal);
|
|
285
|
+
}
|
|
286
|
+
catch (cause) {
|
|
287
|
+
if (response.ok &&
|
|
288
|
+
!signal.aborted &&
|
|
289
|
+
(isRetryableModelTransportError(cause) ||
|
|
290
|
+
(cause instanceof MarAgentError && cause.details?.reason === 'MODEL_STREAM_IDLE_TIMEOUT')))
|
|
291
|
+
throw new MarAgentError('MAR_AGENT_MODEL_PROTOCOL_ERROR', 'Anthropic Messages stream was interrupted.', {
|
|
292
|
+
retryable: true,
|
|
293
|
+
details: {
|
|
294
|
+
reason: 'ANTHROPIC_MESSAGES_EARLY_EOF',
|
|
295
|
+
retryAfter: response.headers.get('retry-after')
|
|
296
|
+
},
|
|
297
|
+
cause
|
|
298
|
+
});
|
|
299
|
+
throw cause;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function isSemanticEvent(event) {
|
|
303
|
+
return event.type !== 'response.started' && event.type !== 'usage';
|
|
304
|
+
}
|
|
305
|
+
export function isAnthropicRecoverableStreamError(error) {
|
|
306
|
+
return (error instanceof MarAgentError &&
|
|
307
|
+
error.retryable &&
|
|
308
|
+
(isRetryableUpstreamOverloadCode(error.code) ||
|
|
309
|
+
error.details?.reason === 'ANTHROPIC_MESSAGES_EARLY_EOF'));
|
|
310
|
+
}
|
|
311
|
+
function retryAfter(error) {
|
|
312
|
+
const value = error.details?.retryAfter;
|
|
313
|
+
return typeof value === 'string' ? value : null;
|
|
314
|
+
}
|
|
315
|
+
function inputUsage(inputTokens, cacheReadTokens, cacheWriteTokens) {
|
|
316
|
+
return {
|
|
317
|
+
inputTokens,
|
|
318
|
+
contextInputTokens: tokenSum(inputTokens, cacheReadTokens, cacheWriteTokens),
|
|
319
|
+
cacheReadTokens,
|
|
320
|
+
cacheWriteTokens
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
function tokenSum(...values) {
|
|
324
|
+
return values.some((value) => value !== undefined)
|
|
325
|
+
? values.reduce((sum, value) => sum + (value ?? 0), 0)
|
|
326
|
+
: undefined;
|
|
327
|
+
}
|
|
328
|
+
function boundedError(value, maximum) {
|
|
329
|
+
const normalized = value?.trim();
|
|
330
|
+
if (!normalized)
|
|
331
|
+
return undefined;
|
|
332
|
+
return [...normalized].slice(0, maximum).join('');
|
|
333
|
+
}
|
|
334
|
+
function anthropicMessages(messages, images, tools = [], modelId) {
|
|
335
|
+
const imageMessage = messages.findLastIndex((message) => message.role === 'user');
|
|
336
|
+
const toolDefinitions = new Map(tools.map((tool) => [tool.name, tool]));
|
|
337
|
+
const result = [];
|
|
338
|
+
let thinkingBytes = 0;
|
|
339
|
+
for (const [index, message] of messages.entries()) {
|
|
340
|
+
const userImages = message.role === 'user'
|
|
341
|
+
? uniqueModelImages(message.images, index === imageMessage ? images : undefined)
|
|
342
|
+
: [];
|
|
343
|
+
if (message.role === 'provider') {
|
|
344
|
+
if (message.provider !== 'ANTHROPIC_MESSAGES' || message.providerModelId !== modelId)
|
|
345
|
+
continue;
|
|
346
|
+
const block = anthropicThinkingBlock(message.item);
|
|
347
|
+
if (!block)
|
|
348
|
+
continue;
|
|
349
|
+
thinkingBytes += Buffer.byteLength(JSON.stringify(block));
|
|
350
|
+
assertAnthropicThinkingBudget(thinkingBytes);
|
|
351
|
+
const previous = result.at(-1);
|
|
352
|
+
if (previous?.role === 'assistant' && Array.isArray(previous.content))
|
|
353
|
+
previous.content.push(block);
|
|
354
|
+
else
|
|
355
|
+
result.push({ role: 'assistant', content: [block] });
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
if (message.role === 'tool_call') {
|
|
359
|
+
const block = {
|
|
360
|
+
type: 'tool_use',
|
|
361
|
+
id: message.callId,
|
|
362
|
+
name: message.name,
|
|
363
|
+
input: anthropicToolInput(message, toolDefinitions.get(message.name ?? ''))
|
|
364
|
+
};
|
|
365
|
+
const previous = result.at(-1);
|
|
366
|
+
if (previous?.role === 'assistant' && Array.isArray(previous.content))
|
|
367
|
+
previous.content.push(block);
|
|
368
|
+
else
|
|
369
|
+
result.push({ role: 'assistant', content: [block] });
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (message.role === 'tool') {
|
|
373
|
+
const block = {
|
|
374
|
+
type: 'tool_result',
|
|
375
|
+
tool_use_id: message.callId,
|
|
376
|
+
content: message.images?.length
|
|
377
|
+
? [
|
|
378
|
+
{ type: 'text', text: message.content },
|
|
379
|
+
...message.images.map((image) => ({
|
|
380
|
+
type: 'image',
|
|
381
|
+
source: {
|
|
382
|
+
type: 'base64',
|
|
383
|
+
media_type: image.mimeType,
|
|
384
|
+
data: image.dataBase64
|
|
385
|
+
}
|
|
386
|
+
}))
|
|
387
|
+
]
|
|
388
|
+
: message.content
|
|
389
|
+
};
|
|
390
|
+
const previous = result.at(-1);
|
|
391
|
+
if (previous?.role === 'user' && Array.isArray(previous.content))
|
|
392
|
+
previous.content.push(block);
|
|
393
|
+
else
|
|
394
|
+
result.push({ role: 'user', content: [block] });
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (message.role === 'assistant') {
|
|
398
|
+
if (!message.content)
|
|
399
|
+
continue;
|
|
400
|
+
const block = { type: 'text', text: message.content };
|
|
401
|
+
const previous = result.at(-1);
|
|
402
|
+
if (previous?.role === 'assistant' && Array.isArray(previous.content))
|
|
403
|
+
previous.content.push(block);
|
|
404
|
+
else
|
|
405
|
+
result.push({ role: 'assistant', content: [block] });
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
result.push({
|
|
409
|
+
role: message.role,
|
|
410
|
+
content: userImages.length > 0
|
|
411
|
+
? [
|
|
412
|
+
...userImages.map((image) => ({
|
|
413
|
+
type: 'image',
|
|
414
|
+
source: {
|
|
415
|
+
type: 'base64',
|
|
416
|
+
media_type: image.mimeType,
|
|
417
|
+
data: image.dataBase64
|
|
418
|
+
}
|
|
419
|
+
})),
|
|
420
|
+
{ type: 'text', text: message.content }
|
|
421
|
+
]
|
|
422
|
+
: message.content
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
for (const message of result.slice(-2))
|
|
426
|
+
message.content = withCacheBreakpoint(message.content);
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
function uniqueModelImages(...groups) {
|
|
430
|
+
const seen = new Set();
|
|
431
|
+
return groups.flatMap((group) => (group ?? []).flatMap((image) => {
|
|
432
|
+
const key = `${image.mimeType}\u0000${image.dataBase64}`;
|
|
433
|
+
if (seen.has(key))
|
|
434
|
+
return [];
|
|
435
|
+
seen.add(key);
|
|
436
|
+
return [image];
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
function anthropicToolInput(message, tool) {
|
|
440
|
+
if (isObject(message.arguments))
|
|
441
|
+
return message.arguments;
|
|
442
|
+
if (message.inputMode !== 'freeform')
|
|
443
|
+
return {};
|
|
444
|
+
const property = freeformInputProperty(tool?.inputSchema);
|
|
445
|
+
return { [property ?? 'input']: String(message.arguments ?? '') };
|
|
446
|
+
}
|
|
447
|
+
function freeformInputProperty(schema) {
|
|
448
|
+
if (schema?.type !== 'object' || !isObject(schema.properties) || !Array.isArray(schema.required))
|
|
449
|
+
return undefined;
|
|
450
|
+
const required = schema.required.filter((value) => typeof value === 'string');
|
|
451
|
+
if (required.length !== 1)
|
|
452
|
+
return undefined;
|
|
453
|
+
const property = schema.properties[required[0]];
|
|
454
|
+
return isObject(property) && property.type === 'string' ? required[0] : undefined;
|
|
455
|
+
}
|
|
456
|
+
function isObject(value) {
|
|
457
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
458
|
+
}
|
|
459
|
+
function withCacheBreakpoint(content) {
|
|
460
|
+
if (typeof content === 'string')
|
|
461
|
+
return [{ type: 'text', text: content, cache_control: { type: 'ephemeral' } }];
|
|
462
|
+
if (!Array.isArray(content) || content.length === 0)
|
|
463
|
+
return content;
|
|
464
|
+
const blocks = [...content];
|
|
465
|
+
const index = blocks.findLastIndex((block) => isObject(block) && block.type !== 'thinking' && block.type !== 'redacted_thinking');
|
|
466
|
+
const last = blocks[index];
|
|
467
|
+
if (!last || typeof last !== 'object')
|
|
468
|
+
return content;
|
|
469
|
+
blocks[index] = {
|
|
470
|
+
...last,
|
|
471
|
+
cache_control: { type: 'ephemeral' }
|
|
472
|
+
};
|
|
473
|
+
return blocks;
|
|
474
|
+
}
|
|
@@ -1,2 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { MarAgentError } from '../error.js';
|
|
2
|
+
export const ANTHROPIC_THINKING_LIMITS = {
|
|
3
|
+
blockBytes: 512 * 1024,
|
|
4
|
+
contextBytes: 8 * 1024 * 1024
|
|
5
|
+
};
|
|
6
|
+
/** Whitelist opaque protocol state without interpreting or rewriting signed fields. */
|
|
7
|
+
export function anthropicThinkingBlock(value) {
|
|
8
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
9
|
+
return undefined;
|
|
10
|
+
const block = value;
|
|
11
|
+
const keys = Object.keys(block);
|
|
12
|
+
let result;
|
|
13
|
+
if (block.type === 'thinking' &&
|
|
14
|
+
typeof block.thinking === 'string' &&
|
|
15
|
+
typeof block.signature === 'string' &&
|
|
16
|
+
block.signature.length > 0 &&
|
|
17
|
+
keys.every((key) => ['type', 'thinking', 'signature'].includes(key)))
|
|
18
|
+
result = { type: 'thinking', thinking: block.thinking, signature: block.signature };
|
|
19
|
+
else if (block.type === 'redacted_thinking' &&
|
|
20
|
+
typeof block.data === 'string' &&
|
|
21
|
+
block.data.length > 0 &&
|
|
22
|
+
keys.every((key) => ['type', 'data'].includes(key)))
|
|
23
|
+
result = { type: 'redacted_thinking', data: block.data };
|
|
24
|
+
else
|
|
25
|
+
return undefined;
|
|
26
|
+
return Buffer.byteLength(JSON.stringify(result)) <= ANTHROPIC_THINKING_LIMITS.blockBytes
|
|
27
|
+
? result
|
|
28
|
+
: undefined;
|
|
29
|
+
}
|
|
30
|
+
export function assertAnthropicThinkingBudget(bytes) {
|
|
31
|
+
if (bytes > ANTHROPIC_THINKING_LIMITS.contextBytes)
|
|
32
|
+
throw new MarAgentError('MAR_AGENT_CONTEXT_LIMIT', 'Anthropic native thinking context exceeds its bounded context limit.');
|
|
33
|
+
}
|