@aliyunrds/ctxdb-dsh 0.0.0-stage → 0.0.1-beta.3
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/README.md +77 -2
- package/cordis.patch.yml +6 -0
- package/dist/client.js +482 -0
- package/dist/configure-host.js +249 -0
- package/dist/configure.js +1988 -0
- package/dist/index.js +9272 -0
- package/package.json +56 -3
- package/skills/contextdb-dsh/SKILL.md +20 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { createRequire as __ctxdbCreateRequire } from "node:module"; const require = __ctxdbCreateRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// ../../plugins/dsh-plugins/src/configure-host.ts
|
|
4
|
+
import { readFileSync as readFileSync2, realpathSync } from "node:fs";
|
|
5
|
+
import { createRequire } from "node:module";
|
|
6
|
+
import { basename, dirname as dirname3, join } from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
|
|
9
|
+
// ../shared/src/auth-config.ts
|
|
10
|
+
import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
12
|
+
import { dirname } from "node:path";
|
|
13
|
+
var AuthConfigError = class extends Error {
|
|
14
|
+
constructor(message) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = "AuthConfigError";
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
function readAuthConfig(path) {
|
|
20
|
+
if (!existsSync(path)) return { version: 2, agents: {} };
|
|
21
|
+
let raw;
|
|
22
|
+
try {
|
|
23
|
+
raw = JSON.parse(readFileSync(path, "utf8"));
|
|
24
|
+
} catch {
|
|
25
|
+
throw new AuthConfigError("Invalid ctxdb.json; repair the JSON before changing authentication.");
|
|
26
|
+
}
|
|
27
|
+
if (raw?.version !== 2 || !object(raw.agents) || raw.logins !== void 0 && !object(raw.logins)) {
|
|
28
|
+
throw new AuthConfigError("Unsupported ctxdb configuration; authentication was not changed.");
|
|
29
|
+
}
|
|
30
|
+
return raw;
|
|
31
|
+
}
|
|
32
|
+
function object(value) {
|
|
33
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
function withAuthConfigLock(path, task) {
|
|
36
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
37
|
+
const lock = path + ".oauth-config.lock", deadline = Date.now() + 1e4;
|
|
38
|
+
let fd;
|
|
39
|
+
while (true) {
|
|
40
|
+
try {
|
|
41
|
+
fd = openSync(lock, "wx", 384);
|
|
42
|
+
break;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
if (error.code !== "EEXIST") throw new AuthConfigError("Cannot lock ctxdb configuration.");
|
|
45
|
+
if (Date.now() >= deadline) throw new AuthConfigError("Configuration is locked. Check the previous CLI process before removing a stale .oauth-config.lock file.");
|
|
46
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
return task();
|
|
51
|
+
} finally {
|
|
52
|
+
closeSync(fd);
|
|
53
|
+
unlinkSync(lock);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function updateAuthConfig(path, task) {
|
|
57
|
+
return withAuthConfigLock(path, () => {
|
|
58
|
+
const raw = readAuthConfig(path), result = task(raw);
|
|
59
|
+
writeAuthConfig(path, raw);
|
|
60
|
+
return result;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function writeAuthConfig(path, raw) {
|
|
64
|
+
const temporary = path + "." + randomBytes(12).toString("hex") + ".tmp";
|
|
65
|
+
let fd;
|
|
66
|
+
try {
|
|
67
|
+
fd = openSync(temporary, "wx", 384);
|
|
68
|
+
writeFileSync(fd, JSON.stringify(raw, null, 2) + "\n");
|
|
69
|
+
fsyncSync(fd);
|
|
70
|
+
closeSync(fd);
|
|
71
|
+
fd = void 0;
|
|
72
|
+
renameSync(temporary, path);
|
|
73
|
+
if (process.platform !== "win32") {
|
|
74
|
+
try {
|
|
75
|
+
const dir = openSync(dirname(path), "r");
|
|
76
|
+
try {
|
|
77
|
+
fsyncSync(dir);
|
|
78
|
+
} finally {
|
|
79
|
+
closeSync(dir);
|
|
80
|
+
}
|
|
81
|
+
} catch {
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} finally {
|
|
85
|
+
if (fd !== void 0) closeSync(fd);
|
|
86
|
+
if (existsSync(temporary)) unlinkSync(temporary);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ../../plugins/dsh-plugins/src/connection.ts
|
|
91
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
92
|
+
import { dirname as dirname2 } from "node:path";
|
|
93
|
+
|
|
94
|
+
// ../shared/src/sites.ts
|
|
95
|
+
var PUBLIC_SITES = {
|
|
96
|
+
cn: {
|
|
97
|
+
baseUrl: "https://api.cn-hangzhou.agentcontext.aliyuncs.com",
|
|
98
|
+
aliases: ["https://context-database.aliyuncs.com"],
|
|
99
|
+
authorizationUrl: "https://rdsnext.console.aliyun.com/contextDbCliAuthorization/cn-hangzhou"
|
|
100
|
+
},
|
|
101
|
+
intl: {
|
|
102
|
+
baseUrl: "https://context-database.ap-southeast-1.aliyuncs.com",
|
|
103
|
+
aliases: [],
|
|
104
|
+
authorizationUrl: "https://rds.console.alibabacloud.com/contextDbCliAuthorization/ap-southeast-1"
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// ../shared/src/debug-policy.ts
|
|
109
|
+
var OFFICIAL_PRODUCTION_BASE_URLS = Object.values(PUBLIC_SITES).flatMap((site) => [site.baseUrl, ...site.aliases]);
|
|
110
|
+
|
|
111
|
+
// ../shared/src/recall-trace.ts
|
|
112
|
+
var RECALL_TRACE_MAX_BYTES = 10 * 1024 * 1024;
|
|
113
|
+
var RECALL_TRACE_OUTCOMES = [
|
|
114
|
+
"success",
|
|
115
|
+
"empty",
|
|
116
|
+
"timeout",
|
|
117
|
+
"malformed_response",
|
|
118
|
+
"http_error"
|
|
119
|
+
];
|
|
120
|
+
var TRACE_OUTCOMES = new Set(RECALL_TRACE_OUTCOMES);
|
|
121
|
+
|
|
122
|
+
// ../shared/src/auth-trace.ts
|
|
123
|
+
var AUTH_TRACE_MAX_BYTES = 2 * 1024 * 1024;
|
|
124
|
+
|
|
125
|
+
// ../shared/src/data-endpoint.ts
|
|
126
|
+
var DEFAULT_DATA_BASE_URL = PUBLIC_SITES.cn.baseUrl;
|
|
127
|
+
|
|
128
|
+
// ../../plugins/dsh-plugins/src/connection.ts
|
|
129
|
+
function managedCredentialRef(configPath, site) {
|
|
130
|
+
const profile = createHash2("sha256").update(dirname2(configPath)).digest("hex").slice(0, 32);
|
|
131
|
+
return `CTXDB_DSH_${site.toUpperCase()}_${profile}`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ../../plugins/dsh-plugins/src/configure-host.ts
|
|
135
|
+
var inject = ["profileContext", "appReady", "appExit"];
|
|
136
|
+
async function configure(ctx, dir, site) {
|
|
137
|
+
if (realpathSync(dir) !== process.cwd() || basename(dirname3(dir)) !== "profiles" || site !== "cn" && site !== "intl") {
|
|
138
|
+
throw new Error("Expected the DSH-selected profile and site cn or intl.");
|
|
139
|
+
}
|
|
140
|
+
const profile = {
|
|
141
|
+
...ctx.profileContext,
|
|
142
|
+
name: basename(dir),
|
|
143
|
+
dir,
|
|
144
|
+
home: dirname3(dirname3(dir)),
|
|
145
|
+
patchPath: join(dir, "cordis.patch.yml"),
|
|
146
|
+
overlays: []
|
|
147
|
+
};
|
|
148
|
+
process.env.DSH_HOME = profile.home;
|
|
149
|
+
const key = readFileSync2(0, "utf8");
|
|
150
|
+
if (!key || /\s/.test(key)) throw new Error("Invalid ContextDB API Key.");
|
|
151
|
+
const configPath = join(profile.dir, "aliyun-contextdb", "ctxdb.json");
|
|
152
|
+
readAuthConfig(configPath);
|
|
153
|
+
const credentialRef = managedCredentialRef(configPath, site);
|
|
154
|
+
const requireHost = createRequire(profile.installAnchor);
|
|
155
|
+
const { parseDocument, isSeq, isMap, isScalar } = requireHost("yaml");
|
|
156
|
+
const { load } = requireHost("js-yaml");
|
|
157
|
+
const { entryListSchema } = requireHost("@deepseek-ai/cordis-plugin-include");
|
|
158
|
+
const {
|
|
159
|
+
boot,
|
|
160
|
+
loadLayeredEnv,
|
|
161
|
+
loadProfileDirectory,
|
|
162
|
+
readProfilePatches,
|
|
163
|
+
composeEntries,
|
|
164
|
+
PluginPackages,
|
|
165
|
+
createRuntimeResolution
|
|
166
|
+
} = requireHost("@deepseek-ai/dsh-app-boot");
|
|
167
|
+
const { writeFileAtomic } = requireHost("@deepseek-ai/dsh-atomic-write");
|
|
168
|
+
let before;
|
|
169
|
+
try {
|
|
170
|
+
before = readFileSync2(profile.patchPath, "utf8");
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (error.code !== "ENOENT") throw error;
|
|
173
|
+
before = "";
|
|
174
|
+
}
|
|
175
|
+
const document = parseDocument(before, {
|
|
176
|
+
customTags: [{ tag: "tag:yaml.org,2002:js", resolve: (value) => value }]
|
|
177
|
+
});
|
|
178
|
+
if (document.errors.length) throw new Error("Invalid DSH profile YAML.");
|
|
179
|
+
if (document.contents === null) document.contents = document.createNode([]);
|
|
180
|
+
if (!isSeq(document.contents)) throw new Error("Expected a list of DSH profile patches.");
|
|
181
|
+
const installed = loadProfileDirectory("dsh", dir, profile.installAnchor);
|
|
182
|
+
const findProvider = (patches) => composeEntries([readProfilePatches("dsh", profile, { ...installed, patches })]).find((row) => row.id === "credentials");
|
|
183
|
+
let credentialsEntry = findProvider(installed.patches);
|
|
184
|
+
let nextPatch = before;
|
|
185
|
+
if (!credentialsEntry || credentialsEntry.disabled) {
|
|
186
|
+
const profileRows = composeEntries([...installed.layers.map((layer) => layer.patches), installed.patches]);
|
|
187
|
+
const hasProvider = profileRows.some((row) => row.id === "credentials");
|
|
188
|
+
const credentialRow = document.contents.items.findLast((row) => isMap(row) && row.get("id") === "credentials" && !row.has("insert"));
|
|
189
|
+
if (!hasProvider) {
|
|
190
|
+
document.contents.flow = false;
|
|
191
|
+
document.add({ insert: [{ id: "credentials", name: "@deepseek-ai/dsh-credentials-local" }] });
|
|
192
|
+
} else if (credentialRow) {
|
|
193
|
+
const disabled = credentialRow.get("disabled", true);
|
|
194
|
+
if (isScalar(disabled)) {
|
|
195
|
+
disabled.tag = void 0;
|
|
196
|
+
disabled.value = false;
|
|
197
|
+
} else credentialRow.set("disabled", false);
|
|
198
|
+
} else {
|
|
199
|
+
document.contents.flow = false;
|
|
200
|
+
document.add({ id: "credentials", disabled: false });
|
|
201
|
+
}
|
|
202
|
+
nextPatch = String(document);
|
|
203
|
+
credentialsEntry = findProvider(load(nextPatch, { schema: entryListSchema }));
|
|
204
|
+
}
|
|
205
|
+
if (!credentialsEntry || credentialsEntry.disabled === true) {
|
|
206
|
+
process.stderr.write("The target profile needs an enabled credentials provider. Check its bundles and the DSH home patch.\n");
|
|
207
|
+
throw new Error("Credentials disabled outside this profile.");
|
|
208
|
+
}
|
|
209
|
+
const providerBase = pathToFileURL(join(profile.dir, "package.json")).href;
|
|
210
|
+
const resolution = await createRuntimeResolution({ installAnchor: profile.installAnchor, profile: installed, home: profile.home });
|
|
211
|
+
const credentialHost = await boot(
|
|
212
|
+
"dsh",
|
|
213
|
+
join(ctx.profileContext.dir, "credentials-root.yml"),
|
|
214
|
+
[{ insert: [credentialsEntry] }],
|
|
215
|
+
async (host) => {
|
|
216
|
+
host.provide("profileContext", { ...profile, startedBundles: installed.layers.map((layer) => layer.packageName) });
|
|
217
|
+
host.provide("launchEnvironment", loadLayeredEnv("dsh", profile.dir));
|
|
218
|
+
await host.plugin(PluginPackages, { resolution });
|
|
219
|
+
},
|
|
220
|
+
providerBase
|
|
221
|
+
);
|
|
222
|
+
try {
|
|
223
|
+
const credentials = credentialHost.get("credentials");
|
|
224
|
+
if (!credentials) throw new Error("The target profile's credential provider did not activate.");
|
|
225
|
+
await credentials.set(credentialRef, key);
|
|
226
|
+
} finally {
|
|
227
|
+
await credentialHost.fiber.dispose();
|
|
228
|
+
}
|
|
229
|
+
if (before !== nextPatch) await writeFileAtomic(profile.patchPath, nextPatch, { mode: 384 });
|
|
230
|
+
updateAuthConfig(configPath, (raw) => {
|
|
231
|
+
raw.dsh_auth = { mode: "api-key", site, credentialRef };
|
|
232
|
+
});
|
|
233
|
+
console.log(`ContextDB API Key saved for ${profile.name} (${site}).`);
|
|
234
|
+
}
|
|
235
|
+
function apply(ctx, config) {
|
|
236
|
+
ctx.appReady.onReady(() => {
|
|
237
|
+
void configure(ctx, config.profileDir, config.site).then(
|
|
238
|
+
() => ctx.appExit(0),
|
|
239
|
+
() => {
|
|
240
|
+
process.stderr.write("Could not configure ContextDB. Check the DSH credential store and target profile permissions, then retry.\n");
|
|
241
|
+
ctx.appExit(1);
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
export {
|
|
247
|
+
apply,
|
|
248
|
+
inject
|
|
249
|
+
};
|