@mapick/cost-firewall 0.1.0

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.
Files changed (104) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +161 -0
  3. package/dist/breaker.d.ts +30 -0
  4. package/dist/breaker.d.ts.map +1 -0
  5. package/dist/breaker.js +131 -0
  6. package/dist/breaker.js.map +1 -0
  7. package/dist/cli/index.d.ts +18 -0
  8. package/dist/cli/index.d.ts.map +1 -0
  9. package/dist/cli/index.js +244 -0
  10. package/dist/cli/index.js.map +1 -0
  11. package/dist/config-warn.d.ts +11 -0
  12. package/dist/config-warn.d.ts.map +1 -0
  13. package/dist/config-warn.js +26 -0
  14. package/dist/config-warn.js.map +1 -0
  15. package/dist/config.d.ts +7 -0
  16. package/dist/config.d.ts.map +1 -0
  17. package/dist/config.js +32 -0
  18. package/dist/config.js.map +1 -0
  19. package/dist/dashboard/html.d.ts +2 -0
  20. package/dist/dashboard/html.d.ts.map +1 -0
  21. package/dist/dashboard/html.js +898 -0
  22. package/dist/dashboard/html.js.map +1 -0
  23. package/dist/dashboard/index.d.ts +8 -0
  24. package/dist/dashboard/index.d.ts.map +1 -0
  25. package/dist/dashboard/index.js +163 -0
  26. package/dist/dashboard/index.js.map +1 -0
  27. package/dist/dashboard/sse.d.ts +9 -0
  28. package/dist/dashboard/sse.d.ts.map +1 -0
  29. package/dist/dashboard/sse.js +22 -0
  30. package/dist/dashboard/sse.js.map +1 -0
  31. package/dist/hooks/agent-end.d.ts +18 -0
  32. package/dist/hooks/agent-end.d.ts.map +1 -0
  33. package/dist/hooks/agent-end.js +25 -0
  34. package/dist/hooks/agent-end.js.map +1 -0
  35. package/dist/hooks/before-agent-reply.d.ts +29 -0
  36. package/dist/hooks/before-agent-reply.d.ts.map +1 -0
  37. package/dist/hooks/before-agent-reply.js +42 -0
  38. package/dist/hooks/before-agent-reply.js.map +1 -0
  39. package/dist/hooks/index.d.ts +7 -0
  40. package/dist/hooks/index.d.ts.map +1 -0
  41. package/dist/hooks/index.js +17 -0
  42. package/dist/hooks/index.js.map +1 -0
  43. package/dist/hooks/model-call.d.ts +39 -0
  44. package/dist/hooks/model-call.d.ts.map +1 -0
  45. package/dist/hooks/model-call.js +120 -0
  46. package/dist/hooks/model-call.js.map +1 -0
  47. package/dist/index.d.ts +13 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +49 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/pricing.d.ts +12 -0
  52. package/dist/pricing.d.ts.map +1 -0
  53. package/dist/pricing.js +43 -0
  54. package/dist/pricing.js.map +1 -0
  55. package/dist/provider/auth.d.ts +14 -0
  56. package/dist/provider/auth.d.ts.map +1 -0
  57. package/dist/provider/auth.js +53 -0
  58. package/dist/provider/auth.js.map +1 -0
  59. package/dist/provider/index.d.ts +12 -0
  60. package/dist/provider/index.d.ts.map +1 -0
  61. package/dist/provider/index.js +134 -0
  62. package/dist/provider/index.js.map +1 -0
  63. package/dist/provider/route.d.ts +10 -0
  64. package/dist/provider/route.d.ts.map +1 -0
  65. package/dist/provider/route.js +19 -0
  66. package/dist/provider/route.js.map +1 -0
  67. package/dist/provider/stream.d.ts +10 -0
  68. package/dist/provider/stream.d.ts.map +1 -0
  69. package/dist/provider/stream.js +120 -0
  70. package/dist/provider/stream.js.map +1 -0
  71. package/dist/provider/synthetic.d.ts +13 -0
  72. package/dist/provider/synthetic.d.ts.map +1 -0
  73. package/dist/provider/synthetic.js +59 -0
  74. package/dist/provider/synthetic.js.map +1 -0
  75. package/dist/provider/upstream/anthropic.d.ts +13 -0
  76. package/dist/provider/upstream/anthropic.d.ts.map +1 -0
  77. package/dist/provider/upstream/anthropic.js +62 -0
  78. package/dist/provider/upstream/anthropic.js.map +1 -0
  79. package/dist/provider/upstream/openai.d.ts +17 -0
  80. package/dist/provider/upstream/openai.d.ts.map +1 -0
  81. package/dist/provider/upstream/openai.js +75 -0
  82. package/dist/provider/upstream/openai.js.map +1 -0
  83. package/dist/source.d.ts +35 -0
  84. package/dist/source.d.ts.map +1 -0
  85. package/dist/source.js +41 -0
  86. package/dist/source.js.map +1 -0
  87. package/dist/state.d.ts +56 -0
  88. package/dist/state.d.ts.map +1 -0
  89. package/dist/state.js +178 -0
  90. package/dist/state.js.map +1 -0
  91. package/dist/store.d.ts +23 -0
  92. package/dist/store.d.ts.map +1 -0
  93. package/dist/store.js +68 -0
  94. package/dist/store.js.map +1 -0
  95. package/dist/tools/index.d.ts +13 -0
  96. package/dist/tools/index.d.ts.map +1 -0
  97. package/dist/tools/index.js +63 -0
  98. package/dist/tools/index.js.map +1 -0
  99. package/dist/types.d.ts +98 -0
  100. package/dist/types.d.ts.map +1 -0
  101. package/dist/types.js +7 -0
  102. package/dist/types.js.map +1 -0
  103. package/openclaw.plugin.json +44 -0
  104. package/package.json +49 -0
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @mapick/cost-firewall — OpenClaw plugin entry point
3
+ */
4
+ import { PLUGIN_ID, PLUGIN_NAME } from "./types.js";
5
+ import { FirewallState } from "./state.js";
6
+ import { EventStore } from "./store.js";
7
+ import { registerHooks } from "./hooks/index.js";
8
+ import { registerCli } from "./cli/index.js";
9
+ import { registerDashboard } from "./dashboard/index.js";
10
+ import { registerProvider } from "./provider/index.js";
11
+ import { registerTools } from "./tools/index.js";
12
+ import { detectConfigRisks } from "./config-warn.js";
13
+ export default {
14
+ id: PLUGIN_ID,
15
+ name: PLUGIN_NAME,
16
+ version: "0.1.0",
17
+ register(api) {
18
+ const config = api.config?.plugins?.entries?.[PLUGIN_ID]?.config ?? {};
19
+ const state = new FirewallState(config);
20
+ const store = new EventStore();
21
+ // Register Hook Layer
22
+ registerHooks(api, state, store);
23
+ // Register Provider Layer
24
+ registerProvider(api, state, store);
25
+ // Register CLI
26
+ registerCli(api, state, store);
27
+ // Register Agent Tools (/firewall status/stop/resume conversational commands)
28
+ registerTools(api, state, store);
29
+ // Config risk detection (fallback bypass etc.)
30
+ const warnings = detectConfigRisks(api.config);
31
+ for (const w of warnings) {
32
+ store.append({ type: "config_warning", reason: w.message, source: w.level });
33
+ }
34
+ // Register Dashboard
35
+ const sse = registerDashboard(api, state, store);
36
+ // Broadcast stats update
37
+ const originalUpdate = state.updateSourceStats.bind(state);
38
+ state.updateSourceStats = (source, cost) => {
39
+ originalUpdate(source, cost);
40
+ sse.broadcast({
41
+ type: "stats_update",
42
+ today_tokens: state.globalStats.todayTokens,
43
+ today_blocked: state.globalStats.todayBlocked,
44
+ });
45
+ };
46
+ },
47
+ };
48
+ export { PLUGIN_ID, PLUGIN_NAME };
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,eAAe;IACb,EAAE,EAAE,SAAS;IACb,IAAI,EAAE,WAAW;IACjB,OAAO,EAAE,OAAO;IAEhB,QAAQ,CAAC,GAAQ;QACf,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,SAAS,CAAC,EAAE,MAAM,IAAI,EAAE,CAAC;QACvE,MAAM,KAAK,GAAG,IAA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AE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Model pricing and cost estimation
3
+ *
4
+ * OpenRouter: pass-through pricing (inherits upstream provider pricing)
5
+ */
6
+ export interface TokenUsage {
7
+ prompt_tokens?: number;
8
+ completion_tokens?: number;
9
+ }
10
+ export declare function estimateCost(usage: TokenUsage | null | undefined, provider: string, model: string, responseStreamBytes?: number): number;
11
+ export declare function getProviderModelKey(provider: string, model: string): string;
12
+ //# sourceMappingURL=pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.d.ts","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AA6BH,MAAM,WAAW,UAAU;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,wBAAgB,YAAY,CAC1B,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,EACpC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,mBAAmB,CAAC,EAAE,MAAM,GAC3B,MAAM,CAUR;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE3E"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Model pricing and cost estimation
3
+ *
4
+ * OpenRouter: pass-through pricing (inherits upstream provider pricing)
5
+ */
6
+ // Pricing table: $/1M tokens
7
+ const PRICING = {
8
+ openai: {
9
+ "gpt-4o": { input: 2.5, output: 10 },
10
+ "gpt-4o-mini": { input: 0.15, output: 0.6 },
11
+ "gpt-4-turbo": { input: 10, output: 30 },
12
+ "gpt-3.5-turbo": { input: 0.5, output: 1.5 },
13
+ },
14
+ anthropic: {
15
+ "claude-sonnet-4-6": { input: 3, output: 15 },
16
+ "claude-sonnet-4-20250514": { input: 3, output: 15 },
17
+ "claude-sonnet-4-5": { input: 3, output: 15 },
18
+ "claude-haiku-3-5": { input: 0.8, output: 4 },
19
+ },
20
+ openrouter: {}, // pass-through pricing
21
+ deepseek: {
22
+ "deepseek-chat": { input: 0.28, output: 0.42 },
23
+ "deepseek-reasoner": { input: 0.55, output: 2.19 },
24
+ },
25
+ qwen: {
26
+ "qwen3-plus": { input: 0.5, output: 2 },
27
+ },
28
+ };
29
+ // Fallback: estimate by response bytes (assuming 1 token ≈ 4 bytes, $3/1M tokens)
30
+ const FALLBACK_COST_PER_BYTE = 3 / 1_000_000 / 4;
31
+ export function estimateCost(usage, provider, model, responseStreamBytes) {
32
+ if (usage?.prompt_tokens || usage?.completion_tokens) {
33
+ return (usage.prompt_tokens ?? 0) + (usage.completion_tokens ?? 0);
34
+ }
35
+ if (responseStreamBytes) {
36
+ return responseStreamBytes;
37
+ }
38
+ return 0;
39
+ }
40
+ export function getProviderModelKey(provider, model) {
41
+ return `${provider}/${model}`;
42
+ }
43
+ //# sourceMappingURL=pricing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pricing.js","sourceRoot":"","sources":["../src/pricing.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,6BAA6B;AAC7B,MAAM,OAAO,GAAsE;IACjF,MAAM,EAAE;QACN,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,EAAE,EAAE;QACpC,aAAa,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE;QAC3C,aAAa,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;QACxC,eAAe,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE;KAC7C;IACD,SAAS,EAAE;QACT,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE;QAC7C,0BAA0B,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE;QACpD,mBAAmB,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE;QAC7C,kBAAkB,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE;KAC9C;IACD,UAAU,EAAE,EAAE,EAAE,uBAAuB;IACvC,QAAQ,EAAE;QACR,eAAe,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;QAC9C,mBAAmB,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE;KACnD;IACD,IAAI,EAAE;QACJ,YAAY,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,EAAE;KACxC;CACF,CAAC;AAEF,kFAAkF;AAClF,MAAM,sBAAsB,GAAG,CAAC,GAAG,SAAS,GAAG,CAAC,CAAC;AAOjD,MAAM,UAAU,YAAY,CAC1B,KAAoC,EACpC,QAAgB,EAChB,KAAa,EACb,mBAA4B;IAE5B,IAAI,KAAK,EAAE,aAAa,IAAI,KAAK,EAAE,iBAAiB,EAAE,CAAC;QACrD,OAAO,CAAC,KAAK,CAAC,aAAa,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,iBAAiB,IAAI,CAAC,CAAC,CAAC;IACrE,CAAC;IAED,IAAI,mBAAmB,EAAE,CAAC;QACxB,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAE,KAAa;IACjE,OAAO,GAAG,QAAQ,IAAI,KAAK,EAAE,CAAC;AAChC,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Upstream auth resolution
3
+ *
4
+ * Priority:
5
+ * 1. api.runtime.modelAuth (OpenClaw SDK)
6
+ * 2. api.config.models.providers (direct config read)
7
+ * 3. Environment variables
8
+ */
9
+ export interface AuthResult {
10
+ apiKey: string;
11
+ headers?: Record<string, string>;
12
+ }
13
+ export declare function resolveUpstreamAuth(api: any, upstream: string, model: string): Promise<AuthResult>;
14
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/provider/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAWD,wBAAsB,mBAAmB,CACvC,GAAG,EAAE,GAAG,EACR,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,UAAU,CAAC,CAqCrB"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Upstream auth resolution
3
+ *
4
+ * Priority:
5
+ * 1. api.runtime.modelAuth (OpenClaw SDK)
6
+ * 2. api.config.models.providers (direct config read)
7
+ * 3. Environment variables
8
+ */
9
+ // Environment variable name mapping
10
+ const ENV_VAR_MAP = {
11
+ openai: "OPENAI_API_KEY",
12
+ anthropic: "ANTHROPIC_API_KEY",
13
+ openrouter: "OPENROUTER_API_KEY",
14
+ deepseek: "DEEPSEEK_API_KEY",
15
+ qwen: "DASHSCOPE_API_KEY",
16
+ };
17
+ export async function resolveUpstreamAuth(api, upstream, model) {
18
+ // 1. Try SDK runtime auth
19
+ if (api.runtime?.modelAuth?.getApiKeyForModel) {
20
+ try {
21
+ const key = await api.runtime.modelAuth.getApiKeyForModel({
22
+ model: { provider: upstream, id: model },
23
+ cfg: api.config,
24
+ workspaceDir: api.workspaceDir,
25
+ });
26
+ if (key)
27
+ return { apiKey: key };
28
+ }
29
+ catch { /* continue */ }
30
+ }
31
+ if (api.runtime?.modelAuth?.resolveApiKeyForProvider) {
32
+ try {
33
+ const key = await api.runtime.modelAuth.resolveApiKeyForProvider(upstream);
34
+ if (key)
35
+ return { apiKey: key };
36
+ }
37
+ catch { /* continue */ }
38
+ }
39
+ // 2. Read directly from config
40
+ const providerConfig = api.config?.models?.providers?.[upstream];
41
+ if (providerConfig?.apiKey) {
42
+ return { apiKey: providerConfig.apiKey };
43
+ }
44
+ // 3. Read from environment variables
45
+ const envVar = ENV_VAR_MAP[upstream];
46
+ if (envVar && process.env[envVar]) {
47
+ return { apiKey: process.env[envVar] };
48
+ }
49
+ throw new Error(`Mapick: unable to resolve API key for upstream "${upstream}". ` +
50
+ `Configure it in OpenClaw (models.providers.${upstream}.apiKey) ` +
51
+ `or set the ${ENV_VAR_MAP[upstream] ?? upstream.toUpperCase() + "_API_KEY"} environment variable.`);
52
+ }
53
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/provider/auth.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAOH,oCAAoC;AACpC,MAAM,WAAW,GAA2B;IAC1C,MAAM,EAAE,gBAAgB;IACxB,SAAS,EAAE,mBAAmB;IAC9B,UAAU,EAAE,oBAAoB;IAChC,QAAQ,EAAE,kBAAkB;IAC5B,IAAI,EAAE,mBAAmB;CAC1B,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,GAAQ,EACR,QAAgB,EAChB,KAAa;IAEb,0BAA0B;IAC1B,IAAI,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,iBAAiB,EAAE,CAAC;QAC9C,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,iBAAiB,CAAC;gBACxD,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE;gBACxC,GAAG,EAAE,GAAG,CAAC,MAAM;gBACf,YAAY,EAAE,GAAG,CAAC,YAAY;aAC/B,CAAC,CAAC;YACH,IAAI,GAAG;gBAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC,CAAC,cAAc,CAAC,CAAC;IAC5B,CAAC;IAED,IAAI,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,wBAAwB,EAAE,CAAC;QACrD,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC;YAC3E,IAAI,GAAG;gBAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;QAClC,CAAC;QAAC,MAAM,CAAC,CAAC,cAAc,CAAC,CAAC;IAC5B,CAAC;IAED,+BAA+B;IAC/B,MAAM,cAAc,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,QAAQ,CAAC,CAAC;IACjE,IAAI,cAAc,EAAE,MAAM,EAAE,CAAC;QAC3B,OAAO,EAAE,MAAM,EAAE,cAAc,CAAC,MAAM,EAAE,CAAC;IAC3C,CAAC;IAED,qCAAqC;IACrC,MAAM,MAAM,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IACrC,IAAI,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QAClC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAE,EAAE,CAAC;IAC1C,CAAC;IAED,MAAM,IAAI,KAAK,CACb,mDAAmD,QAAQ,KAAK;QAChE,8CAA8C,QAAQ,WAAW;QACjE,cAAc,WAAW,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,WAAW,EAAE,GAAG,UAAU,wBAAwB,CACnG,CAAC;AACJ,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Provider registration entry point
3
+ *
4
+ * mapick/<upstream>/<model> routing
5
+ * - catalog: declare model catalog
6
+ * - resolveDynamicModel: dynamically accept any upstream model ID
7
+ * - createStreamFn: precheck + upstream forwarding
8
+ */
9
+ import type { FirewallState } from "../state.js";
10
+ import type { EventStore } from "../store.js";
11
+ export declare function registerProvider(api: any, state: FirewallState, store: EventStore): void;
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/provider/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAoB9C,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,GAAG,EACR,KAAK,EAAE,aAAa,EACpB,KAAK,EAAE,UAAU,GAChB,IAAI,CAuGN"}
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Provider registration entry point
3
+ *
4
+ * mapick/<upstream>/<model> routing
5
+ * - catalog: declare model catalog
6
+ * - resolveDynamicModel: dynamically accept any upstream model ID
7
+ * - createStreamFn: precheck + upstream forwarding
8
+ */
9
+ import { parseMapickModelRef } from "./route.js";
10
+ import { resolveUpstreamAuth } from "./auth.js";
11
+ import { createBlockedStream } from "./synthetic.js";
12
+ import { streamOpenAi, getOpenAiBaseUrl } from "./upstream/openai.js";
13
+ import { streamAnthropic } from "./upstream/anthropic.js";
14
+ import { estimateCost } from "../pricing.js";
15
+ function categorizeError(err) {
16
+ const m = err?.message ?? String(err);
17
+ if (m.includes("timeout") || m.includes("ETIMEDOUT"))
18
+ return "timeout";
19
+ if (m.includes("401") || m.includes("Unauthorized"))
20
+ return "auth_error";
21
+ if (m.includes("429") || m.includes("rate"))
22
+ return "rate_limit";
23
+ if (m.includes("403") || m.includes("Forbidden"))
24
+ return "access_denied";
25
+ if (/^5\d\d/.test(m) || m.includes("server"))
26
+ return "server_error";
27
+ if (m.includes("ECONNREFUSED") || m.includes("ECONNRESET"))
28
+ return "connection_closed";
29
+ if (m.includes("fetch"))
30
+ return "network_error";
31
+ return "unknown";
32
+ }
33
+ export function registerProvider(api, state, store) {
34
+ api.registerProvider({
35
+ id: "mapick",
36
+ label: "Mapick Cost Firewall",
37
+ envVars: [],
38
+ auth: [],
39
+ catalog: {
40
+ order: "simple",
41
+ run: async (_ctx) => ({
42
+ provider: {
43
+ api: "openai-completions",
44
+ baseUrl: "https://api.mapick.ai/v1",
45
+ apiKey: "mapick-firewall-plugin",
46
+ models: [{
47
+ id: "mapick-default",
48
+ name: "Mapick Firewall Passthrough",
49
+ reasoning: false,
50
+ input: ["text"],
51
+ cost: { input: 0, output: 0 },
52
+ contextWindow: 200000,
53
+ maxTokens: 8192,
54
+ }],
55
+ },
56
+ }),
57
+ },
58
+ resolveDynamicModel: (ctx) => {
59
+ const modelId = ctx.modelId;
60
+ const fullModel = modelId.startsWith("mapick/") ? modelId.slice("mapick/".length) : modelId;
61
+ return {
62
+ id: modelId,
63
+ name: `Mapick → ${fullModel}`,
64
+ provider: "mapick",
65
+ api: "openai-completions",
66
+ baseUrl: "https://api.mapick.ai/v1",
67
+ reasoning: false,
68
+ input: ["text"],
69
+ cost: { input: 0, output: 0 },
70
+ contextWindow: 200000,
71
+ maxTokens: 8192,
72
+ };
73
+ },
74
+ createStreamFn() {
75
+ const self = this;
76
+ return async function* stream(model, context, options) {
77
+ // model may come from ctx, use catalog + resolveDynamicModel provided model
78
+ const modelId = typeof model === "string" ? model : model?.id ?? "";
79
+ const route = parseMapickModelRef(modelId);
80
+ if (!route)
81
+ throw new Error(`Invalid Mapick model reference: ${modelId}`);
82
+ const src = route.upstream;
83
+ // Unified precheck
84
+ const result = state.precheck(src);
85
+ if (!result.allow) {
86
+ state.globalStats.todayBlocked++;
87
+ store.append({
88
+ type: "blocked",
89
+ provider: route.upstream,
90
+ model: route.model,
91
+ reason: result.reason,
92
+ layer: result.layer,
93
+ });
94
+ yield* createBlockedStream({
95
+ provider: route.upstream,
96
+ model: route.model,
97
+ reason: result.reason,
98
+ format: route.upstream === "anthropic" ? "anthropic" : "openai",
99
+ });
100
+ return;
101
+ }
102
+ // Resolve upstream auth
103
+ const auth = await resolveUpstreamAuth(api, route.upstream, route.model);
104
+ let inputTokens = 0;
105
+ let outputTokens = 0;
106
+ let responseStreamBytes = 0;
107
+ try {
108
+ const s = route.upstream === "anthropic"
109
+ ? streamAnthropic({ apiKey: auth.apiKey, model: route.model, messages: context?.messages ?? [], ...options })
110
+ : streamOpenAi({ baseUrl: getOpenAiBaseUrl(route.upstream), apiKey: auth.apiKey, model: route.model, messages: context?.messages ?? [], ...options });
111
+ for await (const chunk of s) {
112
+ responseStreamBytes += JSON.stringify(chunk).length;
113
+ if (chunk.usage) {
114
+ inputTokens = chunk.usage.prompt_tokens ?? inputTokens;
115
+ outputTokens = chunk.usage.completion_tokens ?? outputTokens;
116
+ }
117
+ yield chunk;
118
+ }
119
+ const cost = estimateCost({ prompt_tokens: inputTokens, completion_tokens: outputTokens }, route.upstream, route.model, responseStreamBytes);
120
+ store.append({ type: "model_call_ended", provider: route.upstream, model: route.model, outcome: "completed", estimatedCost: cost });
121
+ state.updateSourceStats(src, cost);
122
+ state.breaker.recordSuccess(src);
123
+ }
124
+ catch (err) {
125
+ const msg = err?.message ?? String(err);
126
+ store.append({ type: "model_call_ended", provider: route.upstream, model: route.model, outcome: "error", failureKind: categorizeError(err), reason: msg.slice(0, 200) });
127
+ state.breaker.recordFailure(src);
128
+ throw err;
129
+ }
130
+ };
131
+ },
132
+ });
133
+ }
134
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/provider/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,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EAAE,CAAC,CAAC;oBACpI,KAAK,CAAC,iBAAiB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;oBACnC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;gBACnC,CAAC;gBAAC,OAAO,GAAQ,EAAE,CAAC;oBAClB,MAAM,GAAG,GAAG,GAAG,EAAE,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;oBACxC,KAAK,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,eAAe,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC;oBACzK,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;oBACjC,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * mapick/<upstream>/<model> route resolution
3
+ */
4
+ export interface RouteInfo {
5
+ upstream: string;
6
+ model: string;
7
+ }
8
+ export declare function parseMapickModelRef(ref: string): RouteInfo | null;
9
+ export declare function isMapickModelRef(ref: string): boolean;
10
+ //# sourceMappingURL=route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/provider/route.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAYjE;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErD"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * mapick/<upstream>/<model> route resolution
3
+ */
4
+ export function parseMapickModelRef(ref) {
5
+ if (!ref.startsWith("mapick/"))
6
+ return null;
7
+ const parts = ref.slice("mapick/".length).split("/");
8
+ if (parts.length < 2)
9
+ return null;
10
+ const upstream = parts[0];
11
+ const model = parts.slice(1).join("/");
12
+ if (!upstream || !model)
13
+ return null;
14
+ return { upstream, model };
15
+ }
16
+ export function isMapickModelRef(ref) {
17
+ return ref.startsWith("mapick/");
18
+ }
19
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","sourceRoot":"","sources":["../../src/provider/route.ts"],"names":[],"mappings":"AAAA;;GAEG;AAOH,MAAM,UAAU,mBAAmB,CAAC,GAAW;IAC7C,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC;QAAE,OAAO,IAAI,CAAC;IAE5C,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACrD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,IAAI,CAAC;IAElC,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEvC,IAAI,CAAC,QAAQ,IAAI,CAAC,KAAK;QAAE,OAAO,IAAI,CAAC;IAErC,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * createStreamFn implementation
3
+ *
4
+ * Core: precheck before real upstream request is sent
5
+ * SDK convention: createStreamFn(ctx) => async function*(model, context, options)
6
+ */
7
+ import type { FirewallState } from "../state.js";
8
+ import type { EventStore } from "../store.js";
9
+ export declare function createStreamFn(state: FirewallState, store: EventStore, api: any): (ctx: any) => (model: string, context: any, options: any) => AsyncGenerator<any, void, any>;
10
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/provider/stream.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAS9C,wBAAgB,cAAc,CAC5B,KAAK,EAAE,aAAa,EACpB,KAAK,EAAE,UAAU,EACjB,GAAG,EAAE,GAAG,IAIS,KAAK,GAAG,MACO,OAAO,MAAM,EAAE,SAAS,GAAG,EAAE,SAAS,GAAG,oCAwG1E"}
@@ -0,0 +1,120 @@
1
+ /**
2
+ * createStreamFn implementation
3
+ *
4
+ * Core: precheck before real upstream request is sent
5
+ * SDK convention: createStreamFn(ctx) => async function*(model, context, options)
6
+ */
7
+ import { parseMapickModelRef } from "./route.js";
8
+ import { resolveUpstreamAuth } from "./auth.js";
9
+ import { createBlockedStream } from "./synthetic.js";
10
+ import { streamOpenAi, getOpenAiBaseUrl } from "./upstream/openai.js";
11
+ import { streamAnthropic } from "./upstream/anthropic.js";
12
+ import { sourceFromProviderContext } from "../source.js";
13
+ import { estimateCost } from "../pricing.js";
14
+ export function createStreamFn(state, store, api) {
15
+ // Return createStreamFn matching SDK signature
16
+ // OpenClaw calls createStreamFn(ctx), returns async generator function
17
+ return function (ctx) {
18
+ return async function* stream(model, context, options) {
19
+ const route = parseMapickModelRef(model);
20
+ if (!route) {
21
+ throw new Error(`Invalid Mapick model reference: ${model}`);
22
+ }
23
+ const source = sourceFromProviderContext({ ...context, ...ctx }, route);
24
+ // Precheck
25
+ const result = state.precheck(source);
26
+ if (!result.allow) {
27
+ store.append({
28
+ type: "blocked",
29
+ source,
30
+ provider: route.upstream,
31
+ model: route.model,
32
+ reason: result.reason,
33
+ layer: result.layer,
34
+ });
35
+ state.globalStats.todayBlocked++;
36
+ state.globalStats.todaySavedEstimate += estimateCost(null, route.upstream, route.model);
37
+ yield* createBlockedStream({
38
+ provider: route.upstream,
39
+ model: route.model,
40
+ reason: result.reason,
41
+ format: route.upstream === "anthropic" ? "anthropic" : "openai",
42
+ });
43
+ return;
44
+ }
45
+ // Get upstream auth
46
+ const auth = await resolveUpstreamAuth(api, route.upstream, route.model);
47
+ // Call upstream
48
+ let inputTokens = 0;
49
+ let outputTokens = 0;
50
+ let responseStreamBytes = 0;
51
+ try {
52
+ let upstreamStream;
53
+ if (route.upstream === "anthropic") {
54
+ upstreamStream = streamAnthropic({
55
+ apiKey: auth.apiKey,
56
+ model: route.model,
57
+ messages: context.messages ?? [],
58
+ ...options,
59
+ });
60
+ }
61
+ else {
62
+ upstreamStream = streamOpenAi({
63
+ baseUrl: getOpenAiBaseUrl(route.upstream),
64
+ apiKey: auth.apiKey,
65
+ model: route.model,
66
+ messages: context.messages ?? [],
67
+ ...options,
68
+ });
69
+ }
70
+ for await (const chunk of upstreamStream) {
71
+ responseStreamBytes += JSON.stringify(chunk).length;
72
+ if (chunk.usage) {
73
+ inputTokens = chunk.usage.prompt_tokens ?? inputTokens;
74
+ outputTokens = chunk.usage.completion_tokens ?? outputTokens;
75
+ }
76
+ yield chunk;
77
+ }
78
+ const cost = estimateCost({ prompt_tokens: inputTokens, completion_tokens: outputTokens }, route.upstream, route.model, responseStreamBytes);
79
+ store.append({
80
+ type: "model_call_ended",
81
+ source,
82
+ provider: route.upstream,
83
+ model: route.model,
84
+ outcome: "completed",
85
+ estimatedCost: cost,
86
+ });
87
+ state.updateSourceStats(source, cost);
88
+ state.breaker.recordSuccess(source);
89
+ }
90
+ catch (err) {
91
+ store.append({
92
+ type: "model_call_ended",
93
+ source,
94
+ provider: route.upstream,
95
+ model: route.model,
96
+ outcome: "error",
97
+ failureKind: categorizeError(err),
98
+ });
99
+ state.breaker.recordFailure(source);
100
+ throw err;
101
+ }
102
+ };
103
+ };
104
+ }
105
+ function categorizeError(err) {
106
+ if (err.message.includes("timeout"))
107
+ return "timeout";
108
+ if (err.message.includes("ECONNRESET"))
109
+ return "connection_reset";
110
+ if (err.message.includes("ECONNREFUSED"))
111
+ return "connection_closed";
112
+ if (err.message.includes("401"))
113
+ return "auth_error";
114
+ if (err.message.includes("429"))
115
+ return "rate_limit";
116
+ if (err.message.includes("5"))
117
+ return "server_error";
118
+ return "unknown";
119
+ }
120
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/provider/stream.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,MAAM,UAAU,cAAc,CAC5B,KAAoB,EACpB,KAAiB,EACjB,GAAQ;IAER,+CAA+C;IAC/C,uEAAuE;IACvE,OAAO,UAAU,GAAQ;QACvB,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM,CAAC,KAAa,EAAE,OAAY,EAAE,OAAY;YACrE,MAAM,KAAK,GAAG,mBAAmB,CAAC,KAAK,CAAC,CAAC;YACzC,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,MAAM,IAAI,KAAK,CAAC,m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B,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Blocked response generation
3
+ *
4
+ * Return synthetic stream when request is blocked by Mapick
5
+ */
6
+ export interface BlockedStreamOptions {
7
+ provider: string;
8
+ model: string;
9
+ reason: string;
10
+ format?: "openai" | "anthropic";
11
+ }
12
+ export declare function createBlockedStream(options: BlockedStreamOptions): AsyncGenerator<any>;
13
+ //# sourceMappingURL=synthetic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"synthetic.d.ts","sourceRoot":"","sources":["../../src/provider/synthetic.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,QAAQ,GAAG,WAAW,CAAC;CACjC;AAED,wBAAuB,mBAAmB,CACxC,OAAO,EAAE,oBAAoB,GAC5B,cAAc,CAAC,GAAG,CAAC,CAwDrB"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Blocked response generation
3
+ *
4
+ * Return synthetic stream when request is blocked by Mapick
5
+ */
6
+ export async function* createBlockedStream(options) {
7
+ const { provider, model, reason, format = "openai" } = options;
8
+ const message = `Mapick Cost Firewall: Request blocked (${reason}). Provider: ${provider}, Model: ${model}.`;
9
+ if (format === "anthropic") {
10
+ yield {
11
+ type: "message_start",
12
+ message: {
13
+ id: `msg_mapick_${Date.now()}`,
14
+ type: "message",
15
+ role: "assistant",
16
+ content: [],
17
+ model: "mapick-blocked",
18
+ stop_reason: "end_turn",
19
+ usage: { input_tokens: 0, output_tokens: 0 },
20
+ },
21
+ };
22
+ yield {
23
+ type: "content_block_start",
24
+ index: 0,
25
+ content_block: { type: "text", text: "" },
26
+ };
27
+ yield {
28
+ type: "content_block_delta",
29
+ index: 0,
30
+ delta: { type: "text_delta", text: message },
31
+ };
32
+ yield {
33
+ type: "content_block_stop",
34
+ index: 0,
35
+ };
36
+ yield {
37
+ type: "message_delta",
38
+ delta: { stop_reason: "end_turn" },
39
+ usage: { output_tokens: 0 },
40
+ };
41
+ }
42
+ else {
43
+ yield {
44
+ id: `chatcmpl_mapick_${Date.now()}`,
45
+ object: "chat.completion.chunk",
46
+ created: Math.floor(Date.now() / 1000),
47
+ model: "mapick-blocked",
48
+ choices: [
49
+ {
50
+ index: 0,
51
+ delta: { role: "assistant", content: message },
52
+ finish_reason: "stop",
53
+ },
54
+ ],
55
+ usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 },
56
+ };
57
+ }
58
+ }
59
+ //# sourceMappingURL=synthetic.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"synthetic.js","sourceRoot":"","sources":["../../src/provider/synthetic.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,mBAAmB,CACxC,OAA6B;IAE7B,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC/D,MAAM,OAAO,GAAG,0CAA0C,MAAM,gBAAgB,QAAQ,YAAY,KAAK,GAAG,CAAC;IAE7G,IAAI,MAAM,KAAK,WAAW,EAAE,CAAC;QAC3B,MAAM;YACJ,IAAI,EAAE,eAAe;YACrB,OAAO,EAAE;gBACP,EAAE,EAAE,cAAc,IAAI,CAAC,GAAG,EAAE,EAAE;gBAC9B,IAAI,EAAE,SAAS;gBACf,IAAI,EAAE,WAAW;gBACjB,OAAO,EAAE,EAAE;gBACX,KAAK,EAAE,gBAAgB;gBACvB,WAAW,EAAE,UAAU;gBACvB,KAAK,EAAE,EAAE,YAAY,EAAE,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE;aAC7C;SACF,CAAC;QAEF,MAAM;YACJ,IAAI,EAAE,qBAAqB;YAC3B,KAAK,EAAE,CAAC;YACR,aAAa,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE;SAC1C,CAAC;QAEF,MAAM;YACJ,IAAI,EAAE,qBAAqB;YAC3B,KAAK,EAAE,CAAC;YACR,KAAK,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE;SAC7C,CAAC;QAEF,MAAM;YACJ,IAAI,EAAE,oBAAoB;YAC1B,KAAK,EAAE,CAAC;SACT,CAAC;QAEF,MAAM;YACJ,IAAI,EAAE,eAAe;YACrB,KAAK,EAAE,EAAE,WAAW,EAAE,UAAU,EAAE;YAClC,KAAK,EAAE,EAAE,aAAa,EAAE,CAAC,EAAE;SAC5B,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,MAAM;YACJ,EAAE,EAAE,mBAAmB,IAAI,CAAC,GAAG,EAAE,EAAE;YACnC,MAAM,EAAE,uBAAuB;YAC/B,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;YACtC,KAAK,EAAE,gBAAgB;YACvB,OAAO,EAAE;gBACP;oBACE,KAAK,EAAE,CAAC;oBACR,KAAK,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE;oBAC9C,aAAa,EAAE,MAAM;iBACtB;aACF;YACD,KAAK,EAAE,EAAE,aAAa,EAAE,CAAC,EAAE,iBAAiB,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE;SACnE,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Anthropic Messages API upstream transport
3
+ */
4
+ export interface AnthropicStreamOptions {
5
+ apiKey: string;
6
+ model: string;
7
+ messages: any[];
8
+ max_tokens?: number;
9
+ stream?: boolean;
10
+ [key: string]: any;
11
+ }
12
+ export declare function streamAnthropic(options: AnthropicStreamOptions): AsyncGenerator<any>;
13
+ //# sourceMappingURL=anthropic.d.ts.map