-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.ts
More file actions
112 lines (106 loc) · 3.27 KB
/
Copy pathmain.ts
File metadata and controls
112 lines (106 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* OpenRouter MCP Server
*
* This MCP provides tools for interacting with OpenRouter's API,
* including model discovery, comparison, and AI chat completions.
*
* OpenRouter offers a unified API for accessing hundreds of AI models
* with built-in fallback mechanisms, cost optimization, and provider routing.
*/
import type { Registry } from "@decocms/mcps-shared/registry";
import { serve } from "@decocms/mcps-shared/serve";
import { tools } from "@decocms/openrouter/tools";
import { BindingOf, type DefaultEnv, withRuntime } from "@decocms/runtime";
import { z } from "zod";
import { calculatePreAuthAmount, toMicrodollars } from "./usage";
export const StateSchema = z.object({
WALLET: BindingOf("@deco/wallet"),
});
/**
* Environment type combining Deco bindings and Cloudflare Workers context
*/
export type Env = DefaultEnv<typeof StateSchema, Registry>;
interface OpenRouterUsageReport {
providerMetadata: {
openrouter: {
usage: {
cost: number;
};
};
};
}
const isOpenRouterUsageReport = (
usage: unknown | OpenRouterUsageReport,
): usage is OpenRouterUsageReport => {
return (
typeof usage === "object" &&
usage !== null &&
"providerMetadata" in usage &&
typeof usage.providerMetadata === "object" &&
usage.providerMetadata !== null &&
"openrouter" in usage.providerMetadata &&
typeof usage.providerMetadata.openrouter === "object" &&
usage.providerMetadata.openrouter !== null &&
"usage" in usage.providerMetadata.openrouter &&
typeof usage.providerMetadata.openrouter.usage === "object" &&
usage.providerMetadata.openrouter.usage !== null &&
"cost" in usage.providerMetadata.openrouter.usage
);
};
const runtime = withRuntime<
DefaultEnv<typeof StateSchema, Registry>,
typeof StateSchema,
Registry
>({
tools: (env) => {
return tools(env, {
start: async (modelInfo, params) => {
const amount = calculatePreAuthAmount(modelInfo, params);
const { id } =
await env.MESH_REQUEST_CONTEXT.state.WALLET.PRE_AUTHORIZE_AMOUNT({
amount,
metadata: {
modelId: modelInfo.id,
params: params,
},
}).catch((err) => {
console.error("WALLET ERROR", err);
return {
id: undefined,
};
});
return {
end: async (usage) => {
if (!id) {
return;
}
if (!isOpenRouterUsageReport(usage)) {
throw new Error("Usage cost not found");
}
const vendorId = process.env.WALLET_VENDOR_ID ?? "deco";
await env.MESH_REQUEST_CONTEXT.state.WALLET.COMMIT_PRE_AUTHORIZED_AMOUNT(
{
identifier: id,
contractId:
env.MESH_REQUEST_CONTEXT.connectionId ??
env.MESH_REQUEST_CONTEXT.state.WALLET.value,
vendorId,
amount: toMicrodollars(
usage.providerMetadata.openrouter.usage.cost,
),
},
);
},
};
},
});
},
configuration: {
state: StateSchema,
scopes: [
"WALLET::PRE_AUTHORIZE_AMOUNT",
"WALLET::COMMIT_PRE_AUTHORIZED_AMOUNT",
],
},
});
serve(runtime.fetch);