@hop-top/kit 0.5.0-alpha.0 → 0.5.0-alpha.10
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/CHANGELOG.md +262 -0
- package/LICENSE +1 -1
- package/LICENSE-cobra.txt +174 -0
- package/NOTICE +15 -0
- package/README.md +55 -259
- package/dist/aim-DZHwwiIc.d.ts +139 -0
- package/dist/aim.d.ts +1 -116
- package/dist/aim.js +13 -414
- package/dist/alias-D9_qFlyP.d.ts +174 -0
- package/dist/alias.d.ts +2 -73
- package/dist/alias.js +27 -207
- package/dist/api-DAexsu2H.d.ts +77 -0
- package/dist/api.d.ts +1 -62
- package/dist/api.js +10 -164
- package/dist/auth-2-jfgiHn.d.ts +34 -0
- package/dist/auth.d.ts +1 -26
- package/dist/auth.js +6 -40
- package/dist/chunk-2JV3A7OP.js +1894 -0
- package/dist/chunk-3FIKGFKI.js +54 -0
- package/dist/chunk-4UE5TCMQ.js +104 -0
- package/dist/chunk-6HFAEZ52.js +29 -0
- package/dist/chunk-6SVOHNIC.js +17 -0
- package/dist/chunk-72XZWQX7.js +504 -0
- package/dist/chunk-ABQSE7AI.js +513 -0
- package/dist/chunk-BDNNPRX6.js +41 -0
- package/dist/chunk-BLS2OBIQ.js +263 -0
- package/dist/chunk-BRNEKPWX.js +37 -0
- package/dist/chunk-CSJWB6D3.js +151 -0
- package/dist/chunk-GGWJRWWA.js +309 -0
- package/dist/chunk-GHPJJV5L.js +613 -0
- package/dist/chunk-IXVQ62NZ.js +334 -0
- package/dist/chunk-JKMQSAJP.js +174 -0
- package/dist/chunk-JZ22ZUFA.js +1131 -0
- package/dist/chunk-K7VCKJ27.js +814 -0
- package/dist/chunk-KVG7YCTS.js +115 -0
- package/dist/chunk-L23O2FFE.js +51 -0
- package/dist/chunk-MQXYDYRD.js +968 -0
- package/dist/chunk-MUIQVU6E.js +251 -0
- package/dist/chunk-NLMS7HGP.js +178 -0
- package/dist/chunk-POR5L2DG.js +247 -0
- package/dist/chunk-PSFYJGDG.js +1445 -0
- package/dist/chunk-QJVSKKRQ.js +369 -0
- package/dist/chunk-QPUCXQXT.js +67 -0
- package/dist/chunk-QQQYCWSC.js +240 -0
- package/dist/chunk-R5U7XKVJ.js +16 -0
- package/dist/chunk-SNVGDYX7.js +187 -0
- package/dist/chunk-T46YPHKN.js +669 -0
- package/dist/chunk-UKZIAQA7.js +26 -0
- package/dist/chunk-WBVO43HU.js +355 -0
- package/dist/chunk-WUOAQVOA.js +86 -0
- package/dist/chunk-WWUKGJB5.js +90 -0
- package/dist/chunk-XJTRY453.js +868 -0
- package/dist/chunk-YGYQUFGY.js +219 -0
- package/dist/chunk-YOBAUCVN.js +583 -0
- package/dist/cli-C5B5tnpA.d.ts +400 -0
- package/dist/cli.d.ts +7 -309
- package/dist/cli.js +70 -885
- package/dist/completion-csVun6SP.d.ts +112 -0
- package/dist/completion-values-By-mnjNt.d.ts +142 -0
- package/dist/completion-values.d.ts +1 -0
- package/dist/completion-values.js +29 -0
- package/dist/completion.d.ts +3 -0
- package/dist/completion.js +34 -0
- package/dist/compliance-AxFf-uMy.d.ts +60 -0
- package/dist/compliance.d.ts +1 -0
- package/dist/compliance.js +17 -0
- package/dist/config-CaK60ByQ.d.ts +241 -0
- package/dist/config.d.ts +1 -97
- package/dist/config.js +8 -78
- package/dist/errcorrect-BcX1Ft0K.d.ts +44 -0
- package/dist/errcorrect.d.ts +1 -37
- package/dist/errcorrect.js +8 -68
- package/dist/error-Do8Qji43.d.ts +231 -0
- package/dist/id/index.d.ts +2 -83
- package/dist/id/index.js +14 -65
- package/dist/index-BKJZClJx.d.ts +1486 -0
- package/dist/index-C3Zw4wDJ.d.ts +359 -0
- package/dist/index-DCe1boBz.d.ts +328 -0
- package/dist/index-VLDcI5NF.d.ts +95 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +132 -0
- package/dist/llm-DYuR7_uX.d.ts +1069 -0
- package/dist/llm-anthropic-Cd_I2xXB.d.ts +67 -0
- package/dist/llm-anthropic.d.ts +2 -0
- package/dist/llm-anthropic.js +13 -0
- package/dist/llm-openai-DyFrucmV.d.ts +87 -0
- package/dist/llm-openai.d.ts +2 -0
- package/dist/llm-openai.js +19 -0
- package/dist/llm.d.ts +1 -188
- package/dist/llm.js +101 -356
- package/dist/log-CkaxMX3U.d.ts +115 -0
- package/dist/log.d.ts +2 -0
- package/dist/log.js +14 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/index.js +184 -0
- package/dist/netpolicy-Brx5XZUB.d.ts +151 -0
- package/dist/netpolicy.d.ts +1 -0
- package/dist/netpolicy.js +25 -0
- package/dist/output-CjZWmL8s.d.ts +420 -0
- package/dist/output.d.ts +3 -213
- package/dist/output.js +137 -447
- package/dist/parity-CvMuYcVI.d.ts +61 -0
- package/dist/progress-CAzyxeWl.d.ts +38 -0
- package/dist/progress.d.ts +1 -30
- package/dist/progress.js +6 -55
- package/dist/provenance-SMFHm5gx.d.ts +30 -0
- package/dist/provenance.d.ts +1 -24
- package/dist/provenance.js +6 -31
- package/dist/routellm-B0nv59mH.d.ts +111 -0
- package/dist/routellm.d.ts +2 -64
- package/dist/routellm.js +13 -255
- package/dist/rpc-D31Lg0Kc.d.ts +32 -0
- package/dist/rpc.d.ts +2 -25
- package/dist/rpc.js +4 -98
- package/dist/safety-CUaYEjZc.d.ts +30 -0
- package/dist/safety.d.ts +1 -24
- package/dist/safety.js +6 -43
- package/dist/scope-BrhYB4ij.d.ts +141 -0
- package/dist/scope.d.ts +1 -121
- package/dist/scope.js +20 -377
- package/dist/serve-D8yWMrTc.d.ts +646 -0
- package/dist/serve.d.ts +3 -0
- package/dist/serve.js +75 -0
- package/dist/sqlstore.js +6 -39
- package/dist/stream-CrjeCp0M.d.ts +44 -0
- package/dist/stream.d.ts +1 -35
- package/dist/stream.js +8 -51
- package/dist/telemetry/index.d.ts +1 -315
- package/dist/telemetry/index.js +26 -599
- package/dist/toolspec-DcPwwoAZ.d.ts +368 -0
- package/dist/toolspec.d.ts +2 -0
- package/dist/toolspec.js +37 -0
- package/dist/tui/index.d.ts +6 -298
- package/dist/tui/index.js +18 -322
- package/dist/upgrade-p_zh3pvV.d.ts +102 -0
- package/dist/upgrade.d.ts +1 -95
- package/dist/upgrade.js +6 -114
- package/dist/xdg-DB7Shoib.d.ts +179 -0
- package/dist/xdg.d.ts +1 -169
- package/dist/xdg.js +14 -128
- package/package.json +236 -46
- package/src/README.md +15 -0
- package/src/alias.conformance.test.ts +250 -0
- package/src/alias.test.ts +206 -2
- package/src/alias.ts +196 -21
- package/src/cli.help-command.test.ts +105 -0
- package/src/cli.offline.test.ts +93 -0
- package/src/cli.parity.test.ts +192 -0
- package/src/cli.test.ts +95 -1
- package/src/cli.ts +195 -25
- package/src/completion-scripts.ts +16 -0
- package/src/completion-values.test.ts +76 -16
- package/src/completion-values.ts +170 -66
- package/src/completion.conformance.test.ts +297 -0
- package/src/completion.test.ts +256 -145
- package/src/completion.ts +767 -121
- package/src/compliance.test.ts +196 -2
- package/src/compliance.ts +440 -20
- package/src/config-merge.test.ts +425 -0
- package/src/config.test.ts +228 -1
- package/src/config.ts +429 -16
- package/src/gen/crud_pb.ts +7 -7
- package/src/id/README.md +38 -0
- package/src/id/contract.test.ts +1 -1
- package/src/id/index.test.ts +1 -1
- package/src/id/index.ts +3 -2
- package/src/index.test.ts +71 -0
- package/src/index.ts +46 -0
- package/src/llm-anthropic.conformance.test.ts +1135 -0
- package/src/llm-anthropic.test.ts +152 -0
- package/src/llm-anthropic.ts +940 -0
- package/src/llm-choice-hermetic.test.ts +335 -0
- package/src/llm-config.test.ts +232 -0
- package/src/llm-config.ts +106 -0
- package/src/llm-endpoint.ts +254 -0
- package/src/llm-error.ts +17 -0
- package/src/llm-errors.ts +152 -0
- package/src/llm-file.ts +126 -0
- package/src/llm-http.ts +317 -0
- package/src/llm-keys.ts +167 -0
- package/src/llm-media.conformance.test.ts +87 -0
- package/src/llm-media.test.ts +279 -0
- package/src/llm-media.ts +322 -0
- package/src/llm-openai.conformance.test.ts +407 -0
- package/src/llm-openai.test.ts +510 -0
- package/src/llm-openai.ts +708 -0
- package/src/llm-payload.ts +155 -0
- package/src/llm-quote.ts +46 -0
- package/src/llm-routellm.conformance.test.ts +44 -0
- package/src/llm-toolnames.conformance.test.ts +112 -0
- package/src/llm-toolnames.test.ts +132 -0
- package/src/llm-toolnames.ts +298 -0
- package/src/llm-uri.ts +99 -0
- package/src/llm.test.ts +43 -15
- package/src/llm.toolcall.test.ts +175 -0
- package/src/llm.ts +602 -190
- package/src/log.parity.test.ts +105 -0
- package/src/log.test.ts +270 -1
- package/src/log.ts +228 -49
- package/src/mcp/README.md +63 -0
- package/src/mcp/args-separator.test.ts +164 -0
- package/src/mcp/args.test.ts +223 -0
- package/src/mcp/args.ts +160 -0
- package/src/mcp/audit.test.ts +520 -0
- package/src/mcp/audit.ts +70 -0
- package/src/mcp/bridge.test.ts +222 -0
- package/src/mcp/bridge.ts +291 -0
- package/src/mcp/conformance.test.ts +357 -0
- package/src/mcp/dispatch.ts +359 -0
- package/src/mcp/identity.test.ts +166 -0
- package/src/mcp/identity.ts +72 -0
- package/src/mcp/index.ts +176 -0
- package/src/mcp/legacy-notify.test.ts +59 -0
- package/src/mcp/legacy.ts +367 -0
- package/src/mcp/mcp.test.ts +707 -0
- package/src/mcp/modern-confirm.test.ts +520 -0
- package/src/mcp/modern-confirm.ts +484 -0
- package/src/mcp/modern.ts +788 -0
- package/src/mcp/stdio.test.ts +313 -0
- package/src/mcp/stdio.ts +254 -0
- package/src/mcp/tasks.ts +82 -0
- package/src/mcp/testtree.ts +250 -0
- package/src/mcp/toolname.test.ts +147 -0
- package/src/mcp/types.ts +689 -0
- package/src/netpolicy.test.ts +280 -0
- package/src/netpolicy.ts +256 -0
- package/src/output/README.md +46 -0
- package/src/output/builtins.ts +5 -5
- package/src/output/dispatch.ts +10 -2
- package/src/output/error.ts +459 -0
- package/src/output/formatter.ts +29 -5
- package/src/output/formatters/README.md +50 -0
- package/src/output/formatters/csv.ts +48 -12
- package/src/output/formatters/json.ts +4 -25
- package/src/output/formatters/project.ts +34 -0
- package/src/output/formatters/table.ts +8 -6
- package/src/output/formatters/text.ts +7 -3
- package/src/output/formatters/yaml.ts +5 -22
- package/src/output/projection.test.ts +298 -0
- package/src/output/projection.ts +33 -59
- package/src/output/template.ts +11 -21
- package/src/output.surface.test.ts +41 -0
- package/src/output.ts +64 -3
- package/src/published-types.test.ts +133 -0
- package/src/routellm.test.ts +209 -28
- package/src/routellm.toolcall.test.ts +521 -0
- package/src/routellm.ts +213 -177
- package/src/router/README.md +44 -0
- package/src/router/server.test.ts +41 -0
- package/src/router/server.ts +11 -0
- package/src/safety.test.ts +19 -9
- package/src/scope-defaults.json +29 -29
- package/src/serve.test.ts +1257 -0
- package/src/serve.ts +1665 -0
- package/src/telemetry/README.md +48 -0
- package/src/telemetry/client.test.ts +31 -0
- package/src/telemetry/client.ts +12 -6
- package/src/telemetry/offline.test.ts +64 -0
- package/src/toolspec-internal.ts +279 -0
- package/src/toolspec-manifest.test.ts +167 -0
- package/src/toolspec-manifest.ts +410 -0
- package/src/toolspec-spec-schemas.ts +12 -0
- package/src/toolspec-spec.test.ts +273 -0
- package/src/toolspec-spec.ts +458 -0
- package/src/toolspec.from-command.test.ts +350 -0
- package/src/toolspec.test.ts +2 -2
- package/src/toolspec.ts +222 -0
- package/src/triton/README.md +34 -0
- package/src/tui/README.md +42 -0
- package/src/tui/parity.test.ts +142 -0
- package/src/tui/parity.ts +59 -13
- package/src/wizard.test.ts +7 -7
- package/test/llm/conformance-helpers.ts +100 -0
- package/test/output/csv.test.ts +164 -0
- package/test/output/dispatch.test.ts +1 -2
- package/test/output/error.test.ts +277 -0
- package/test/output/smoke-capture.ts +2 -2
- package/test/output/smoke.test.ts +2 -2
- package/test/output/taxonomy-contract.test.ts +148 -0
- package/test/setup-env.ts +16 -0
- package/test/toolspec/fixture.ts +79 -0
- package/tsconfig.json +3 -1
- package/tsconfig.test.json +10 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkYGYQUFGYjs = require('./chunk-YGYQUFGY.js');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
var _chunkPSFYJGDGjs = require('./chunk-PSFYJGDG.js');
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
var _chunkR5U7XKVJjs = require('./chunk-R5U7XKVJ.js');
|
|
30
|
+
|
|
31
|
+
// src/llm-anthropic.ts
|
|
32
|
+
var llm_anthropic_exports = {};
|
|
33
|
+
_chunkR5U7XKVJjs.__export.call(void 0, llm_anthropic_exports, {
|
|
34
|
+
ANTHROPIC_DEFAULT_BASE_URL: () => ANTHROPIC_DEFAULT_BASE_URL,
|
|
35
|
+
AnthropicAdapter: () => AnthropicAdapter
|
|
36
|
+
});
|
|
37
|
+
var SCHEME = "anthropic";
|
|
38
|
+
var ANTHROPIC_DEFAULT_BASE_URL = "https://api.anthropic.com";
|
|
39
|
+
var API_VERSION = "2023-06-01";
|
|
40
|
+
var DEFAULT_MAX_TOKENS = 1024;
|
|
41
|
+
var MIN_THINKING_BUDGET = 1024;
|
|
42
|
+
var SYSTEM_SEPARATOR = "\n\n";
|
|
43
|
+
var EVENT_STATUS = {
|
|
44
|
+
invalid_request_error: 400,
|
|
45
|
+
authentication_error: 401,
|
|
46
|
+
billing_error: 402,
|
|
47
|
+
permission_error: 403,
|
|
48
|
+
not_found_error: 404,
|
|
49
|
+
request_too_large: 413,
|
|
50
|
+
rate_limit_error: 429,
|
|
51
|
+
api_error: 500,
|
|
52
|
+
timeout_error: 504,
|
|
53
|
+
overloaded_error: 529
|
|
54
|
+
};
|
|
55
|
+
var AnthropicAdapter = class {
|
|
56
|
+
constructor(cfg) {
|
|
57
|
+
const key = _nullishCoalesce(cfg.provider.apiKey, () => ( ""));
|
|
58
|
+
if (key.trim() === "") throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: API key required");
|
|
59
|
+
this.limits = _chunkYGYQUFGYjs.limitsFrom.call(void 0, SCHEME, cfg.provider.params);
|
|
60
|
+
try {
|
|
61
|
+
this.toolNames = _chunkPSFYJGDGjs.toolNameModeOf.call(void 0, cfg.provider.params);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(`anthropic: ${err.message}`);
|
|
64
|
+
}
|
|
65
|
+
const env = envReader(cfg);
|
|
66
|
+
const envBase = env("ANTHROPIC_BASE_URL");
|
|
67
|
+
if (envBase !== void 0) checkBaseURL("ANTHROPIC_BASE_URL", envBase);
|
|
68
|
+
if (cfg.provider.baseURL) checkBaseURL("base URL", cfg.provider.baseURL);
|
|
69
|
+
const base = cfg.provider.baseURL || envBase || ANTHROPIC_DEFAULT_BASE_URL;
|
|
70
|
+
this.baseURL = base;
|
|
71
|
+
this.model = cfg.provider.model || _optionalChain([cfg, 'access', _ => _.uri, 'optionalAccess', _2 => _2.model]) || "";
|
|
72
|
+
_chunkPSFYJGDGjs.checkKeyEndpoint.call(void 0, SCHEME, key, base);
|
|
73
|
+
this.headers = {
|
|
74
|
+
"content-type": "application/json",
|
|
75
|
+
accept: "application/json",
|
|
76
|
+
"anthropic-version": API_VERSION,
|
|
77
|
+
"x-api-key": key
|
|
78
|
+
};
|
|
79
|
+
const token = _nullishCoalesce(env("ANTHROPIC_AUTH_TOKEN"), () => ( ""));
|
|
80
|
+
const extra = {};
|
|
81
|
+
if (token.trim() !== "") {
|
|
82
|
+
_chunkPSFYJGDGjs.checkKeyEndpoint.call(void 0, SCHEME, token, base);
|
|
83
|
+
extra["authorization"] = `Bearer ${token}`;
|
|
84
|
+
}
|
|
85
|
+
const ws = _nullishCoalesce(env("ANTHROPIC_WORKSPACE_ID"), () => ( ""));
|
|
86
|
+
if (ws !== "") extra["anthropic-workspace-id"] = ws;
|
|
87
|
+
const trusted = [ANTHROPIC_DEFAULT_BASE_URL];
|
|
88
|
+
if (envBase) trusted.push(envBase);
|
|
89
|
+
if (Object.keys(extra).length > 0 && _chunkPSFYJGDGjs.credentialsTrusted.call(void 0, SCHEME, base, ...trusted)) {
|
|
90
|
+
Object.assign(this.headers, extra);
|
|
91
|
+
}
|
|
92
|
+
this.secrets = [key, token].filter((s) => s.trim() !== "");
|
|
93
|
+
}
|
|
94
|
+
close() {
|
|
95
|
+
}
|
|
96
|
+
async complete(req) {
|
|
97
|
+
const names = this.toolNameMap([]);
|
|
98
|
+
const body = await this.buildBody(req, names);
|
|
99
|
+
setToolChoice(body, req.toolChoice, [], names);
|
|
100
|
+
const msg = await this.send(body, req);
|
|
101
|
+
return {
|
|
102
|
+
content: textOf(msg),
|
|
103
|
+
role: "assistant",
|
|
104
|
+
finishReason: _nullishCoalesce(msg.stop_reason, () => ( "")),
|
|
105
|
+
usage: usageOf(msg.usage),
|
|
106
|
+
thinking: thinkingBlocks(msg)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
async callWithTools(req, tools) {
|
|
110
|
+
const names = this.toolNameMap(tools);
|
|
111
|
+
const body = await this.buildBody(req, names);
|
|
112
|
+
body["tools"] = mapTools(names.defs(tools));
|
|
113
|
+
setToolChoice(body, req.toolChoice, tools, names);
|
|
114
|
+
const msg = await this.send(body, req);
|
|
115
|
+
return toolResponseOf(msg, names);
|
|
116
|
+
}
|
|
117
|
+
stream(req) {
|
|
118
|
+
return this.openStream(req, void 0);
|
|
119
|
+
}
|
|
120
|
+
streamWithTools(req, tools) {
|
|
121
|
+
return this.openStream(req, tools);
|
|
122
|
+
}
|
|
123
|
+
// ── private ────────────────────────────────────────────────────
|
|
124
|
+
toolNameMap(tools) {
|
|
125
|
+
return _chunkPSFYJGDGjs.ToolNames.build(_chunkPSFYJGDGjs.ANTHROPIC_TOOL_NAMES, this.toolNames, tools);
|
|
126
|
+
}
|
|
127
|
+
transport(req) {
|
|
128
|
+
const model = req.model || this.model;
|
|
129
|
+
return {
|
|
130
|
+
provider: SCHEME,
|
|
131
|
+
secrets: this.secrets,
|
|
132
|
+
limits: this.limits,
|
|
133
|
+
mapError: (r) => mapErrorResponse(r, model)
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
async send(body, req) {
|
|
137
|
+
const opts = this.transport(req);
|
|
138
|
+
const resp = await _chunkYGYQUFGYjs.post.call(void 0, opts, this.url(), this.headers, body);
|
|
139
|
+
const text = await _chunkYGYQUFGYjs.readBody.call(void 0, resp, this.limits.body, SCHEME).catch((e) => {
|
|
140
|
+
throw _chunkYGYQUFGYjs.scrubError.call(void 0, e, this.secrets);
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
return JSON.parse(text);
|
|
144
|
+
} catch (e2) {
|
|
145
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: response is not valid JSON");
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
url() {
|
|
149
|
+
return _chunkYGYQUFGYjs.joinURL.call(void 0, this.baseURL, "v1/messages");
|
|
150
|
+
}
|
|
151
|
+
openStream(req, tools) {
|
|
152
|
+
let outcome;
|
|
153
|
+
const gen = this.run(req, tools, (o) => {
|
|
154
|
+
outcome = o;
|
|
155
|
+
});
|
|
156
|
+
return {
|
|
157
|
+
[Symbol.asyncIterator]: () => gen,
|
|
158
|
+
outcome: () => outcome
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
async *run(req, tools, done) {
|
|
162
|
+
const names = this.toolNameMap(_nullishCoalesce(tools, () => ( [])));
|
|
163
|
+
const body = await this.buildBody(req, names);
|
|
164
|
+
if (tools) body["tools"] = mapTools(names.defs(tools));
|
|
165
|
+
setToolChoice(body, req.toolChoice, _nullishCoalesce(tools, () => ( [])), names);
|
|
166
|
+
body["stream"] = true;
|
|
167
|
+
const opts = this.transport(req);
|
|
168
|
+
const resp = await _chunkYGYQUFGYjs.post.call(void 0, opts, this.url(), {
|
|
169
|
+
...this.headers,
|
|
170
|
+
accept: "text/event-stream"
|
|
171
|
+
}, body);
|
|
172
|
+
const acc = new Accumulator();
|
|
173
|
+
const events = req.streamEvents ? new EventMapper(acc, names) : void 0;
|
|
174
|
+
try {
|
|
175
|
+
for await (const ev of _chunkYGYQUFGYjs.sseEvents.call(void 0, resp, this.limits.event, SCHEME)) {
|
|
176
|
+
let evt;
|
|
177
|
+
try {
|
|
178
|
+
evt = JSON.parse(ev.data);
|
|
179
|
+
} catch (e3) {
|
|
180
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: stream event is not valid JSON");
|
|
181
|
+
}
|
|
182
|
+
if (evt.type === "error") {
|
|
183
|
+
throw streamError(evt.error, req.model || this.model);
|
|
184
|
+
}
|
|
185
|
+
acc.add(evt);
|
|
186
|
+
const d = _nullishCoalesce(evt.delta, () => ( {}));
|
|
187
|
+
if (evt.type === "content_block_delta" && d.type === "text_delta") {
|
|
188
|
+
yield { content: _nullishCoalesce(d.text, () => ( "")), done: false };
|
|
189
|
+
} else if (evt.type === "content_block_delta" && d.type === "thinking_delta" && req.thinking) {
|
|
190
|
+
yield { content: "", thinking: _nullishCoalesce(d.thinking, () => ( "")), done: false };
|
|
191
|
+
} else if (evt.type === "message_stop") {
|
|
192
|
+
done(acc.outcome(names));
|
|
193
|
+
yield { content: "", done: true };
|
|
194
|
+
return;
|
|
195
|
+
} else {
|
|
196
|
+
const tok = _optionalChain([events, 'optionalAccess', _3 => _3.map, 'call', _4 => _4(evt)]);
|
|
197
|
+
if (tok) yield tok;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} catch (err) {
|
|
201
|
+
throw _chunkYGYQUFGYjs.scrubError.call(void 0, err, this.secrets);
|
|
202
|
+
}
|
|
203
|
+
throw new (0, _chunkPSFYJGDGjs.StreamTruncatedError)(SCHEME);
|
|
204
|
+
}
|
|
205
|
+
async buildBody(req, names) {
|
|
206
|
+
const maxTokens = req.maxTokens !== void 0 && req.maxTokens > 0 ? req.maxTokens : DEFAULT_MAX_TOKENS;
|
|
207
|
+
const { system, messages } = await extractSystem(req.messages, names);
|
|
208
|
+
const body = {
|
|
209
|
+
model: req.model || this.model,
|
|
210
|
+
max_tokens: maxTokens,
|
|
211
|
+
messages
|
|
212
|
+
};
|
|
213
|
+
if (system.length > 0) body["system"] = system;
|
|
214
|
+
if (req.thinking) {
|
|
215
|
+
const b = req.thinking.budgetTokens;
|
|
216
|
+
if (b < MIN_THINKING_BUDGET) {
|
|
217
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
218
|
+
`anthropic: thinking budget_tokens must be at least ${MIN_THINKING_BUDGET}, got ${b}`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
if (b >= maxTokens) {
|
|
222
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
223
|
+
`anthropic: thinking budget_tokens (${b}) must be less than max_tokens (${maxTokens})`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
body["thinking"] = { type: "enabled", budget_tokens: b };
|
|
227
|
+
}
|
|
228
|
+
if (req.temperature !== void 0) body["temperature"] = req.temperature;
|
|
229
|
+
if (_optionalChain([req, 'access', _5 => _5.stopSequences, 'optionalAccess', _6 => _6.length])) body["stop_sequences"] = req.stopSequences;
|
|
230
|
+
return body;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
function envReader(cfg) {
|
|
234
|
+
return cfg.hermetic ? () => void 0 : (k) => process.env[k];
|
|
235
|
+
}
|
|
236
|
+
function checkBaseURL(what, base) {
|
|
237
|
+
try {
|
|
238
|
+
new URL(base);
|
|
239
|
+
} catch (e4) {
|
|
240
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(`anthropic: invalid ${what}: ${JSON.stringify(_chunkPSFYJGDGjs.redactURI.call(void 0, base))}`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
async function extractSystem(msgs, names) {
|
|
244
|
+
const system = systemBlocks(msgs);
|
|
245
|
+
const out = [];
|
|
246
|
+
for (let i = 0; i < msgs.length; i++) {
|
|
247
|
+
const m = msgs[i];
|
|
248
|
+
if (m.role === "system") continue;
|
|
249
|
+
checkLinkageRole(m);
|
|
250
|
+
if (m.role === "tool") {
|
|
251
|
+
const block = toolResultBlock(m);
|
|
252
|
+
setCache(block, m.cacheControl);
|
|
253
|
+
if (i > 0 && msgs[i - 1].role === "tool" && out.length > 0) {
|
|
254
|
+
out[out.length - 1].content.push(block);
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
out.push({ role: "user", content: [block] });
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
let blocks = [];
|
|
261
|
+
const calls = _nullishCoalesce(m.toolCalls, () => ( []));
|
|
262
|
+
if (_optionalChain([m, 'access', _7 => _7.parts, 'optionalAccess', _8 => _8.length])) {
|
|
263
|
+
blocks = await mapParts(m.parts);
|
|
264
|
+
} else if (calls.length === 0 || m.content !== "") {
|
|
265
|
+
blocks.push({ type: "text", text: m.content });
|
|
266
|
+
}
|
|
267
|
+
blocks.push(...toolUseBlocks(calls, names));
|
|
268
|
+
if (blocks.length > 0) setCache(blocks[blocks.length - 1], m.cacheControl);
|
|
269
|
+
blocks = [...replayThinking(_nullishCoalesce(m.thinking, () => ( []))), ...blocks];
|
|
270
|
+
out.push({ role: m.role, content: blocks });
|
|
271
|
+
}
|
|
272
|
+
return { system, messages: out };
|
|
273
|
+
}
|
|
274
|
+
function systemBlocks(msgs) {
|
|
275
|
+
const out = [];
|
|
276
|
+
let text = "";
|
|
277
|
+
for (const m of msgs) {
|
|
278
|
+
if (m.role !== "system") continue;
|
|
279
|
+
if (m.content !== "") {
|
|
280
|
+
text = text === "" ? m.content : text + SYSTEM_SEPARATOR + m.content;
|
|
281
|
+
}
|
|
282
|
+
if (!m.cacheControl || text === "") continue;
|
|
283
|
+
out.push({ type: "text", text, cache_control: cacheParam(m.cacheControl) });
|
|
284
|
+
text = "";
|
|
285
|
+
}
|
|
286
|
+
if (text !== "") out.push({ type: "text", text });
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
function checkLinkageRole(m) {
|
|
290
|
+
if ((_nullishCoalesce(_optionalChain([m, 'access', _9 => _9.toolCalls, 'optionalAccess', _10 => _10.length]), () => ( 0))) > 0 && m.role !== "assistant") {
|
|
291
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
292
|
+
`anthropic: ToolCalls are valid only on role assistant, got ${JSON.stringify(m.role)}`
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
if ((_nullishCoalesce(_optionalChain([m, 'access', _11 => _11.thinking, 'optionalAccess', _12 => _12.length]), () => ( 0))) > 0 && m.role !== "assistant") {
|
|
296
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
297
|
+
`anthropic: Thinking is valid only on role assistant, got ${JSON.stringify(m.role)}`
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
if (m.toolCallId && m.role !== "tool") {
|
|
301
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
302
|
+
`anthropic: ToolCallID is valid only on role tool, got ${JSON.stringify(m.role)}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function toolResultBlock(m) {
|
|
307
|
+
if (!m.toolCallId) {
|
|
308
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: tool result needs ToolCallID");
|
|
309
|
+
}
|
|
310
|
+
if (_optionalChain([m, 'access', _13 => _13.parts, 'optionalAccess', _14 => _14.length])) {
|
|
311
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
312
|
+
`anthropic: tool result ${JSON.stringify(m.toolCallId)} carries only Content`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
const res = { type: "tool_result", tool_use_id: m.toolCallId };
|
|
316
|
+
if (m.content) res["content"] = [{ type: "text", text: m.content }];
|
|
317
|
+
return res;
|
|
318
|
+
}
|
|
319
|
+
function toolUseBlocks(calls, names) {
|
|
320
|
+
return calls.map((tc) => {
|
|
321
|
+
if (!tc.id) {
|
|
322
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(`anthropic: tool call ${JSON.stringify(tc.name)} needs an ID`);
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
type: "tool_use",
|
|
326
|
+
id: tc.id,
|
|
327
|
+
name: names.wire(tc.name),
|
|
328
|
+
input: inputOf(tc)
|
|
329
|
+
};
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
function inputOf(tc) {
|
|
333
|
+
const args = tc.arguments;
|
|
334
|
+
if (args === void 0 || args === null || args === "") return {};
|
|
335
|
+
if (typeof args !== "string") return args;
|
|
336
|
+
try {
|
|
337
|
+
return JSON.parse(args);
|
|
338
|
+
} catch (e5) {
|
|
339
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
340
|
+
`anthropic: tool call ${JSON.stringify(tc.id)} arguments are not valid JSON`
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function replayThinking(tbs) {
|
|
345
|
+
return tbs.map(
|
|
346
|
+
(tb) => tb.redacted ? { type: "redacted_thinking", data: tb.redacted } : { type: "thinking", thinking: tb.thinking, signature: tb.signature }
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
function cacheParam(cc) {
|
|
350
|
+
if (!cc) return void 0;
|
|
351
|
+
const ttl = _nullishCoalesce(cc.ttl, () => ( ""));
|
|
352
|
+
if (ttl === "") return { type: "ephemeral" };
|
|
353
|
+
if (ttl === "5m" || ttl === "1h") return { type: "ephemeral", ttl };
|
|
354
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
355
|
+
`anthropic: cache_control ttl must be "5m" or "1h", got ${JSON.stringify(ttl)}`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
function setCache(block, cc) {
|
|
359
|
+
const p = cacheParam(cc);
|
|
360
|
+
if (p) block["cache_control"] = p;
|
|
361
|
+
}
|
|
362
|
+
async function mapParts(parts) {
|
|
363
|
+
const out = [];
|
|
364
|
+
for (const p of parts) {
|
|
365
|
+
switch (p.type) {
|
|
366
|
+
case "text":
|
|
367
|
+
out.push({ type: "text", text: _nullishCoalesce(p.text, () => ( "")) });
|
|
368
|
+
break;
|
|
369
|
+
case "image": {
|
|
370
|
+
const mime = p.mimeType || _optionalChain([p, 'access', _15 => _15.source, 'optionalAccess', _16 => _16.mimeType, 'call', _17 => _17()]) || "";
|
|
371
|
+
if (!mime) throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: image part needs a MIME type");
|
|
372
|
+
if (!p.source) throw new (0, _chunkPSFYJGDGjs.LLMError)("anthropic: nil media source");
|
|
373
|
+
const url = p.source.url();
|
|
374
|
+
if (url) {
|
|
375
|
+
out.push({ type: mime === "application/pdf" ? "document" : "image", source: { type: "url", url } });
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
const data = Buffer.from(await p.source.read()).toString("base64");
|
|
379
|
+
out.push(
|
|
380
|
+
mime === "application/pdf" ? { type: "document", source: { type: "base64", media_type: mime, data } } : { type: "image", source: { type: "base64", media_type: mime, data } }
|
|
381
|
+
);
|
|
382
|
+
break;
|
|
383
|
+
}
|
|
384
|
+
default:
|
|
385
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(`anthropic: unsupported modality ${JSON.stringify(p.type)}`);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
}
|
|
390
|
+
function setToolChoice(body, c, tools, names) {
|
|
391
|
+
try {
|
|
392
|
+
_chunkPSFYJGDGjs.checkToolChoice.call(void 0, c, tools);
|
|
393
|
+
} catch (err) {
|
|
394
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(`anthropic: ${err.message}`);
|
|
395
|
+
}
|
|
396
|
+
if (!c) return;
|
|
397
|
+
body["tool_choice"] = c.mode === "tool" ? { type: "tool", name: names.wire(_nullishCoalesce(c.name, () => ( ""))) } : { type: c.mode };
|
|
398
|
+
}
|
|
399
|
+
function mapTools(defs) {
|
|
400
|
+
return defs.map((d) => {
|
|
401
|
+
const t = {
|
|
402
|
+
name: d.name,
|
|
403
|
+
description: _nullishCoalesce(d.description, () => ( "")),
|
|
404
|
+
input_schema: schemaOf(d.parameters)
|
|
405
|
+
};
|
|
406
|
+
const cc = cacheParam(d.cacheControl);
|
|
407
|
+
if (cc) t["cache_control"] = cc;
|
|
408
|
+
return t;
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
function schemaOf(params) {
|
|
412
|
+
let v = params;
|
|
413
|
+
if (typeof v === "string") {
|
|
414
|
+
try {
|
|
415
|
+
v = JSON.parse(v);
|
|
416
|
+
} catch (e6) {
|
|
417
|
+
v = void 0;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) {
|
|
421
|
+
return { type: "object", properties: {} };
|
|
422
|
+
}
|
|
423
|
+
return { type: "object", ...v };
|
|
424
|
+
}
|
|
425
|
+
function textOf(msg) {
|
|
426
|
+
return (_nullishCoalesce(msg.content, () => ( []))).filter((b) => b.type === "text").map((b) => _nullishCoalesce(b.text, () => ( ""))).join("");
|
|
427
|
+
}
|
|
428
|
+
function usageOf(u) {
|
|
429
|
+
const out = {
|
|
430
|
+
promptTokens: _nullishCoalesce(_optionalChain([u, 'optionalAccess', _18 => _18.input_tokens]), () => ( 0)),
|
|
431
|
+
completionTokens: _nullishCoalesce(_optionalChain([u, 'optionalAccess', _19 => _19.output_tokens]), () => ( 0)),
|
|
432
|
+
cacheReadTokens: _nullishCoalesce(_optionalChain([u, 'optionalAccess', _20 => _20.cache_read_input_tokens]), () => ( 0)),
|
|
433
|
+
cacheCreationTokens: _nullishCoalesce(_optionalChain([u, 'optionalAccess', _21 => _21.cache_creation_input_tokens]), () => ( 0)),
|
|
434
|
+
totalTokens: 0
|
|
435
|
+
};
|
|
436
|
+
out.totalTokens = out.promptTokens + out.completionTokens + out.cacheReadTokens + out.cacheCreationTokens;
|
|
437
|
+
return out;
|
|
438
|
+
}
|
|
439
|
+
function thinkingBlocks(msg) {
|
|
440
|
+
const out = [];
|
|
441
|
+
for (const b of _nullishCoalesce(msg.content, () => ( []))) {
|
|
442
|
+
if (b.type === "thinking") {
|
|
443
|
+
out.push({ thinking: _nullishCoalesce(b.thinking, () => ( "")), signature: _nullishCoalesce(b.signature, () => ( "")), redacted: "" });
|
|
444
|
+
} else if (b.type === "redacted_thinking") {
|
|
445
|
+
out.push({ thinking: "", signature: "", redacted: _nullishCoalesce(b.data, () => ( "")) });
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return out;
|
|
449
|
+
}
|
|
450
|
+
function toolCallOf(b, names) {
|
|
451
|
+
return { id: _nullishCoalesce(b.id, () => ( "")), name: names.original(_nullishCoalesce(b.name, () => ( ""))), arguments: _nullishCoalesce(b.input, () => ( {})) };
|
|
452
|
+
}
|
|
453
|
+
function toolResponseOf(msg, names) {
|
|
454
|
+
const calls = [];
|
|
455
|
+
for (const b of _nullishCoalesce(msg.content, () => ( []))) {
|
|
456
|
+
if (b.type === "tool_use") calls.push(toolCallOf(b, names));
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
content: textOf(msg),
|
|
460
|
+
toolCalls: calls,
|
|
461
|
+
thinking: thinkingBlocks(msg),
|
|
462
|
+
finishReason: _nullishCoalesce(msg.stop_reason, () => ( "")),
|
|
463
|
+
usage: usageOf(msg.usage)
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
var Accumulator = class {
|
|
467
|
+
constructor() {
|
|
468
|
+
this.content = [];
|
|
469
|
+
this.stopReason = "";
|
|
470
|
+
this.usage_ = {};
|
|
471
|
+
}
|
|
472
|
+
add(evt) {
|
|
473
|
+
switch (evt.type) {
|
|
474
|
+
case "message_start": {
|
|
475
|
+
const m = _nullishCoalesce(evt.message, () => ( {}));
|
|
476
|
+
this.content = (_nullishCoalesce(m.content, () => ( []))).map((b) => ({ ...b }));
|
|
477
|
+
this.stopReason = _nullishCoalesce(m.stop_reason, () => ( ""));
|
|
478
|
+
this.usage_ = { ..._nullishCoalesce(m.usage, () => ( {})) };
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
case "message_delta": {
|
|
482
|
+
this.stopReason = _nullishCoalesce(_optionalChain([evt, 'access', _22 => _22.delta, 'optionalAccess', _23 => _23.stop_reason]), () => ( ""));
|
|
483
|
+
const u = _nullishCoalesce(evt.usage, () => ( {}));
|
|
484
|
+
this.usage_.output_tokens = _nullishCoalesce(u.output_tokens, () => ( 0));
|
|
485
|
+
if (u.input_tokens !== void 0) this.usage_.input_tokens = u.input_tokens;
|
|
486
|
+
if (u.cache_read_input_tokens != null) {
|
|
487
|
+
this.usage_.cache_read_input_tokens = u.cache_read_input_tokens;
|
|
488
|
+
}
|
|
489
|
+
if (u.cache_creation_input_tokens != null) {
|
|
490
|
+
this.usage_.cache_creation_input_tokens = u.cache_creation_input_tokens;
|
|
491
|
+
}
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
case "content_block_start": {
|
|
495
|
+
if (evt.index !== this.content.length) {
|
|
496
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
497
|
+
`anthropic: received event of type content_block_start for content block at index ${evt.index}, expected index ${this.content.length}`
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
const b = { ..._nullishCoalesce(evt.content_block, () => ( { type: "" })) };
|
|
501
|
+
if (b.type === "tool_use") b.inputRaw = JSON.stringify(_nullishCoalesce(b.input, () => ( {})));
|
|
502
|
+
this.content.push(b);
|
|
503
|
+
break;
|
|
504
|
+
}
|
|
505
|
+
case "content_block_delta": {
|
|
506
|
+
const i = _nullishCoalesce(evt.index, () => ( -1));
|
|
507
|
+
if (i < 0 || i >= this.content.length) {
|
|
508
|
+
throw new (0, _chunkPSFYJGDGjs.LLMError)(
|
|
509
|
+
`anthropic: received event of type content_block_delta for content block at index ${i}, only ${this.content.length} blocks exist`
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
const b = this.content[i];
|
|
513
|
+
const d = _nullishCoalesce(evt.delta, () => ( {}));
|
|
514
|
+
switch (d.type) {
|
|
515
|
+
case "text_delta":
|
|
516
|
+
b.text = (_nullishCoalesce(b.text, () => ( ""))) + (_nullishCoalesce(d.text, () => ( "")));
|
|
517
|
+
break;
|
|
518
|
+
case "input_json_delta":
|
|
519
|
+
if (d.partial_json) {
|
|
520
|
+
b.inputRaw = b.inputRaw === "{}" || b.inputRaw === void 0 ? d.partial_json : b.inputRaw + d.partial_json;
|
|
521
|
+
}
|
|
522
|
+
break;
|
|
523
|
+
case "thinking_delta":
|
|
524
|
+
b.thinking = (_nullishCoalesce(b.thinking, () => ( ""))) + (_nullishCoalesce(d.thinking, () => ( "")));
|
|
525
|
+
break;
|
|
526
|
+
case "signature_delta":
|
|
527
|
+
b.signature = (_nullishCoalesce(b.signature, () => ( ""))) + (_nullishCoalesce(d.signature, () => ( "")));
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
/** The usage folded so far. */
|
|
535
|
+
usage() {
|
|
536
|
+
return usageOf(this.usage_);
|
|
537
|
+
}
|
|
538
|
+
/** The block at content index `i`, undefined when none started. */
|
|
539
|
+
block(i) {
|
|
540
|
+
return this.content[i];
|
|
541
|
+
}
|
|
542
|
+
/** The tool_use block at `i` as its finished call. */
|
|
543
|
+
call(i, names) {
|
|
544
|
+
return toolCallOf(finished(this.content[i]), names);
|
|
545
|
+
}
|
|
546
|
+
outcome(names) {
|
|
547
|
+
const msg = {
|
|
548
|
+
content: this.content.map(finished),
|
|
549
|
+
stop_reason: this.stopReason,
|
|
550
|
+
usage: this.usage_
|
|
551
|
+
};
|
|
552
|
+
const tr = toolResponseOf(msg, names);
|
|
553
|
+
return {
|
|
554
|
+
content: tr.content,
|
|
555
|
+
thinking: _nullishCoalesce(tr.thinking, () => ( [])),
|
|
556
|
+
toolCalls: tr.toolCalls,
|
|
557
|
+
finishReason: _nullishCoalesce(tr.finishReason, () => ( "")),
|
|
558
|
+
usage: tr.usage
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
function finished(b) {
|
|
563
|
+
return b.type === "tool_use" ? { ...b, input: parseInput(b.inputRaw) } : b;
|
|
564
|
+
}
|
|
565
|
+
var EventMapper = class {
|
|
566
|
+
constructor(acc, names) {
|
|
567
|
+
this.acc = acc;
|
|
568
|
+
this.names = names;
|
|
569
|
+
/** A tool_use block's content index to its call index. */
|
|
570
|
+
this.calls = /* @__PURE__ */ new Map();
|
|
571
|
+
}
|
|
572
|
+
/** The token `evt`, already folded into the accumulator, makes. */
|
|
573
|
+
map(evt) {
|
|
574
|
+
const at = _nullishCoalesce(evt.index, () => ( -1));
|
|
575
|
+
switch (evt.type) {
|
|
576
|
+
case "message_start":
|
|
577
|
+
case "message_delta":
|
|
578
|
+
return { content: "", usage: this.acc.usage(), done: false };
|
|
579
|
+
case "content_block_start": {
|
|
580
|
+
const b = this.acc.block(at);
|
|
581
|
+
if (_optionalChain([b, 'optionalAccess', _24 => _24.type]) !== "tool_use") return void 0;
|
|
582
|
+
const index = this.calls.size;
|
|
583
|
+
this.calls.set(at, index);
|
|
584
|
+
return {
|
|
585
|
+
content: "",
|
|
586
|
+
toolCall: { kind: "start", index, id: _nullishCoalesce(b.id, () => ( "")), name: this.names.original(_nullishCoalesce(b.name, () => ( ""))) },
|
|
587
|
+
done: false
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
case "content_block_delta": {
|
|
591
|
+
const index = this.calls.get(at);
|
|
592
|
+
const d = _nullishCoalesce(evt.delta, () => ( {}));
|
|
593
|
+
if (index === void 0 || d.type !== "input_json_delta" || !d.partial_json) {
|
|
594
|
+
return void 0;
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
content: "",
|
|
598
|
+
toolCall: { kind: "delta", index, argumentsDelta: d.partial_json },
|
|
599
|
+
done: false
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
case "content_block_stop": {
|
|
603
|
+
const index = this.calls.get(at);
|
|
604
|
+
if (index === void 0) return void 0;
|
|
605
|
+
const call = this.acc.call(at, this.names);
|
|
606
|
+
return {
|
|
607
|
+
content: "",
|
|
608
|
+
toolCall: { kind: "end", index, id: call.id, name: call.name, call },
|
|
609
|
+
done: false
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return void 0;
|
|
614
|
+
}
|
|
615
|
+
};
|
|
616
|
+
function parseInput(raw) {
|
|
617
|
+
if (raw === void 0 || raw === "") return {};
|
|
618
|
+
try {
|
|
619
|
+
return JSON.parse(raw);
|
|
620
|
+
} catch (e7) {
|
|
621
|
+
return raw;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
function mapErrorResponse(r, model) {
|
|
625
|
+
let e = {};
|
|
626
|
+
try {
|
|
627
|
+
e = _nullishCoalesce(JSON.parse(r.body).error, () => ( {}));
|
|
628
|
+
} catch (e8) {
|
|
629
|
+
}
|
|
630
|
+
const detail = e.message !== void 0 ? `${_nullishCoalesce(e.type, () => ( "error"))}: ${e.message}` : r.body.slice(0, 2048);
|
|
631
|
+
return classify(r.status, _nullishCoalesce(e.type, () => ( "")), `anthropic: ${r.status} ${detail}`.trim(), model);
|
|
632
|
+
}
|
|
633
|
+
function streamError(e, model) {
|
|
634
|
+
const type = _nullishCoalesce(_optionalChain([e, 'optionalAccess', _25 => _25.type]), () => ( ""));
|
|
635
|
+
const status = _nullishCoalesce(EVENT_STATUS[type], () => ( 0));
|
|
636
|
+
return classify(status, type, `anthropic: stream error: ${type}: ${_nullishCoalesce(_optionalChain([e, 'optionalAccess', _26 => _26.message]), () => ( ""))}`, model);
|
|
637
|
+
}
|
|
638
|
+
function classify(status, type, message, model) {
|
|
639
|
+
const api = new (0, _chunkPSFYJGDGjs.APIError)(SCHEME, status, type, message);
|
|
640
|
+
switch (status) {
|
|
641
|
+
case 401:
|
|
642
|
+
return new (0, _chunkPSFYJGDGjs.AuthError)(SCHEME, api);
|
|
643
|
+
case 403:
|
|
644
|
+
return new (0, _chunkPSFYJGDGjs.PermissionDeniedError)(SCHEME, api);
|
|
645
|
+
case 429:
|
|
646
|
+
return new (0, _chunkPSFYJGDGjs.RateLimitError)(SCHEME);
|
|
647
|
+
case 404:
|
|
648
|
+
return new (0, _chunkPSFYJGDGjs.ModelError)(model, SCHEME);
|
|
649
|
+
}
|
|
650
|
+
if (status >= 500) return new (0, _chunkPSFYJGDGjs.HTTPStatusError)(status, message);
|
|
651
|
+
return api;
|
|
652
|
+
}
|
|
653
|
+
function anthropicFactory(cfg) {
|
|
654
|
+
const a = new AnthropicAdapter(cfg);
|
|
655
|
+
return {
|
|
656
|
+
complete: (req) => a.complete(req),
|
|
657
|
+
stream: (req) => a.stream(req),
|
|
658
|
+
callWithTools: (req, tools) => a.callWithTools(req, tools),
|
|
659
|
+
streamWithTools: (req, tools) => a.streamWithTools(req, tools),
|
|
660
|
+
close: () => a.close()
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
_chunkPSFYJGDGjs.register.call(void 0, SCHEME, anthropicFactory, { endpoints: [ANTHROPIC_DEFAULT_BASE_URL] });
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
exports.ANTHROPIC_DEFAULT_BASE_URL = ANTHROPIC_DEFAULT_BASE_URL; exports.AnthropicAdapter = AnthropicAdapter; exports.llm_anthropic_exports = llm_anthropic_exports;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkR5U7XKVJjs = require('./chunk-R5U7XKVJ.js');
|
|
4
|
+
|
|
5
|
+
// src/auth.ts
|
|
6
|
+
var auth_exports = {};
|
|
7
|
+
_chunkR5U7XKVJjs.__export.call(void 0, auth_exports, {
|
|
8
|
+
NoAuth: () => NoAuth
|
|
9
|
+
});
|
|
10
|
+
var NoAuth = class {
|
|
11
|
+
async inspect() {
|
|
12
|
+
return {
|
|
13
|
+
source: "none",
|
|
14
|
+
identity: "anonymous",
|
|
15
|
+
scopes: [],
|
|
16
|
+
renewable: false
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async refresh() {
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
exports.NoAuth = NoAuth; exports.auth_exports = auth_exports;
|