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