-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprovider.ts
More file actions
131 lines (122 loc) · 4.48 KB
/
Copy pathprovider.ts
File metadata and controls
131 lines (122 loc) · 4.48 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { emptyPluginConfigSchema, type OpenClawPluginApi } from "openclaw/plugin-sdk";
import { PROVIDER_ID, PROVIDER_LABEL, PROXY_PATH, DEFAULT_BASE_URL } from "./constants.js";
import { verifyDifyKey } from "./dify/client.js";
import { handleProxyRequest } from "./proxy/index.js";
import { createCompositeKey } from "./utils/auth.js";
export const difyAuthPlugin = {
id: "dify-auth",
name: "Dify Auth",
description: "Dify provider authentication and proxy",
configSchema: emptyPluginConfigSchema(),
register(api: OpenClawPluginApi) {
// OpenClaw's responses client calls nested paths like /v1/responses under the
// configured base URL, so the proxy route must stay publicly reachable and
// prefix-match subpaths.
api.registerHttpRoute({
path: PROXY_PATH,
auth: "plugin",
match: "prefix",
handler: handleProxyRequest,
});
// 2. Register Provider
api.registerProvider({
id: PROVIDER_ID,
label: PROVIDER_LABEL,
auth: [
{
id: "dify-api-key",
label: "Dify API Key",
hint: "API Key & Base URL",
kind: "api_key",
run: async (ctx) => {
// Ask for API Key
const apiKey = await ctx.prompter.text({
message: "Enter Dify App API Key",
validate: (val) => (val?.trim().length > 5 ? undefined : "Invalid Key"),
});
// Ask for Base URL
const baseUrl = await ctx.prompter.text({
message: "Enter Dify API Base URL",
initialValue: DEFAULT_BASE_URL,
validate: (val) =>
val?.startsWith("http") ? undefined : "Must start with http/https",
});
// Ask for App Type
// const appType = await ctx.prompter.select({
// message: "Select App Type",
// options: [
// { value: "chat", label: "ChatFlow" },
// { value: "agent", label: "Agent" },
// ],
// });
const appType = "chat";
// Verify Key
const progress = ctx.prompter.progress("Verifying Dify API Key...");
let siteInfo: { title?: string } = {};
try {
siteInfo = await verifyDifyKey(apiKey, baseUrl);
progress.stop(`Verified: ${siteInfo.title || "Dify App"}`);
} catch (err) {
progress.stop("Verification failed");
throw new Error(`Failed to verify key: ${String(err)}`, { cause: err });
}
// Construct Config Patch
const compositeKey = createCompositeKey(apiKey, baseUrl, appType);
// Resolve Gateway Port (default to 18789 if not found)
const gatewayPort = ctx.config.gateway?.port ?? 18789;
const proxyUrl = `http://127.0.0.1:${gatewayPort}${PROXY_PATH}`;
// Determine Model ID
const modelId = "chat-flow";
const defaultName = "Dify ChatFlow";
return {
profiles: [
{
profileId: `${PROVIDER_ID}:default`,
credential: {
type: "api_key",
provider: PROVIDER_ID,
key: compositeKey,
},
},
],
configPatch: {
models: {
providers: {
[PROVIDER_ID]: {
baseUrl: proxyUrl,
apiKey: compositeKey,
api: "openai-responses",
models: [
{
id: modelId,
name: siteInfo.title || defaultName,
contextWindow: 128000,
maxTokens: 8192,
reasoning: false,
input: ["text", "image"],
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
},
},
],
},
},
},
agents: {
defaults: {
model: {
primary: `${PROVIDER_ID}/${modelId}`,
},
},
},
},
};
},
},
],
});
},
};