@morozinc/opencode-tencentdb-plugin 0.0.0-stage → 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.
- package/LICENSE +661 -0
- package/LICENSE-MIT +24 -0
- package/NOTICE.md +14 -0
- package/README.md +207 -2
- package/dist/gui-experimental.cjs +64 -0
- package/dist/integration.mjs +1986 -0
- package/docs/screenshots/code-indexes.png +0 -0
- package/package.json +61 -4
- package/scripts/prepare-runtime.mjs +298 -0
- package/src/binding.ts +33 -0
- package/src/bmad-artifact-preview.tsx +111 -0
- package/src/embedding-settings.tsx +191 -0
- package/src/gui.tsx +388 -0
- package/src/index.ts +484 -0
- package/src/integration.d.ts +75 -0
- package/src/rpc.ts +195 -0
- package/src/rule-document.ts +44 -0
- package/src/rules-contract.ts +77 -0
- package/src/rules.ts +237 -0
- package/src/runtime.ts +436 -0
- package/vendor/TENCENT-LICENSE +27 -0
- package/vendor/integration/native/config.ts +253 -0
- package/vendor/integration/native/embedding.ts +270 -0
- package/vendor/integration/native/index.ts +265 -0
- package/vendor/integration/project/index.ts +151 -0
- package/vendor/integration/project/knowledge.ts +283 -0
- package/vendor/integration/server.ts +154 -0
- package/vendor/integration/tencentChatRules.ts +84 -0
- package/vendor/integration/tencentGenerationContext.ts +4 -0
- package/vendor/integration/tencentKnowledgeClient.ts +512 -0
- package/vendor/integration/tencentMemoryClient.ts +391 -0
- package/vendor/integration/tencentUserQuestion.ts +51 -0
- package/vendor/locks/MemoryCore.json +4776 -0
- package/vendor/locks/MemoryKnowledge.json +3761 -0
- package/vendor/locks/MemoryPanel.json +467 -0
- package/vendor/locks/web.json +6418 -0
- package/vendor/provenance.json +21 -0
- package/vendor/runner.mjs +12 -0
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createServer,
|
|
3
|
+
type IncomingMessage,
|
|
4
|
+
type ServerResponse,
|
|
5
|
+
} from "node:http";
|
|
6
|
+
import { createServer as portProbe } from "node:net";
|
|
7
|
+
import { randomBytes } from "node:crypto";
|
|
8
|
+
import { mkdir, readFile, writeFile, rm, realpath } from "node:fs/promises";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
import {
|
|
14
|
+
nativeConfig,
|
|
15
|
+
configureEmbeddingRuntime,
|
|
16
|
+
embeddingStatus,
|
|
17
|
+
probeEmbedding,
|
|
18
|
+
applyEmbedding,
|
|
19
|
+
stopEmbeddingJob,
|
|
20
|
+
startNativeMemory,
|
|
21
|
+
stopNativeMemory,
|
|
22
|
+
TencentMemoryClient,
|
|
23
|
+
type Configuration,
|
|
24
|
+
} from "../dist/integration.mjs";
|
|
25
|
+
|
|
26
|
+
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
27
|
+
export const optionsSchema = z.object({
|
|
28
|
+
dataDir: z.string().min(1).optional(),
|
|
29
|
+
runtimeDir: z.string().min(1).optional(),
|
|
30
|
+
projectless: z.boolean().default(false),
|
|
31
|
+
});
|
|
32
|
+
type Options = z.infer<typeof optionsSchema>;
|
|
33
|
+
type Model = { id: string; providerID: string; variant?: string };
|
|
34
|
+
type Generate = (prompt: string, signal: AbortSignal) => Promise<string>;
|
|
35
|
+
type Capability = { model: Model; generate: Generate; expires: number };
|
|
36
|
+
const nonce = () => randomBytes(32).toString("hex");
|
|
37
|
+
|
|
38
|
+
async function freePort(): Promise<number> {
|
|
39
|
+
const server = portProbe();
|
|
40
|
+
await new Promise<void>((resolve, reject) => {
|
|
41
|
+
server.once("error", reject);
|
|
42
|
+
server.listen(0, "127.0.0.1", resolve);
|
|
43
|
+
});
|
|
44
|
+
const address = server.address();
|
|
45
|
+
if (!address || typeof address === "string")
|
|
46
|
+
throw new Error("Could not allocate a memory port.");
|
|
47
|
+
await new Promise<void>((resolve, reject) =>
|
|
48
|
+
server.close((e) => (e ? reject(e) : resolve())),
|
|
49
|
+
);
|
|
50
|
+
return address.port;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function rawBody(
|
|
54
|
+
request: IncomingMessage,
|
|
55
|
+
limit = 1024 * 1024,
|
|
56
|
+
): Promise<Buffer> {
|
|
57
|
+
const chunks: Buffer[] = [];
|
|
58
|
+
let size = 0;
|
|
59
|
+
for await (const chunk of request) {
|
|
60
|
+
size += chunk.length;
|
|
61
|
+
if (size > limit) throw new Error("Memory request exceeds its size limit.");
|
|
62
|
+
chunks.push(Buffer.from(chunk));
|
|
63
|
+
}
|
|
64
|
+
return Buffer.concat(chunks);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One runtime per OpenCode server, shared across its project plugin instances. */
|
|
68
|
+
export class Runtime {
|
|
69
|
+
config!: Configuration;
|
|
70
|
+
dataDir: string;
|
|
71
|
+
panelUrl = "";
|
|
72
|
+
origin = "";
|
|
73
|
+
private lock = "";
|
|
74
|
+
private lockOwner = "";
|
|
75
|
+
private capabilities = new Map<string, Capability>();
|
|
76
|
+
private tickets = new Map<string, number>();
|
|
77
|
+
private browserKeys = new Map<string, number>();
|
|
78
|
+
private server = createServer((request, response) => {
|
|
79
|
+
void this.serve(request, response);
|
|
80
|
+
});
|
|
81
|
+
private cookie = "";
|
|
82
|
+
constructor(readonly options: Options) {
|
|
83
|
+
this.dataDir = path.resolve(
|
|
84
|
+
options.dataDir ??
|
|
85
|
+
path.join(
|
|
86
|
+
process.env.XDG_DATA_HOME ?? path.join(homedir(), ".local/share"),
|
|
87
|
+
"opencode/tencentdb-plugin",
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
async start() {
|
|
92
|
+
const bundle = path.resolve(
|
|
93
|
+
this.options.runtimeDir ??
|
|
94
|
+
path.join(root, "runtime", `${process.platform}-${process.arch}`),
|
|
95
|
+
);
|
|
96
|
+
await readFile(path.join(bundle, "runtime.json")); // Fail before creating an incomplete profile.
|
|
97
|
+
await mkdir(this.dataDir, { recursive: true, mode: 0o700 });
|
|
98
|
+
this.dataDir = await realpath(this.dataDir);
|
|
99
|
+
this.lock = path.join(this.dataDir, "runtime.lock");
|
|
100
|
+
this.lockOwner = JSON.stringify({ pid: process.pid, nonce: nonce() });
|
|
101
|
+
try {
|
|
102
|
+
await writeFile(this.lock, this.lockOwner, { flag: "wx", mode: 0o600 });
|
|
103
|
+
} catch (cause) {
|
|
104
|
+
if ((cause as NodeJS.ErrnoException).code !== "EEXIST") throw cause;
|
|
105
|
+
const owner = JSON.parse(await readFile(this.lock, "utf8")) as {
|
|
106
|
+
pid: number;
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
process.kill(owner.pid, 0);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error;
|
|
112
|
+
await rm(this.lock); // Reclaim only a positively dead owner; never stop another server.
|
|
113
|
+
await writeFile(this.lock, this.lockOwner, { flag: "wx", mode: 0o600 });
|
|
114
|
+
}
|
|
115
|
+
if ((await readFile(this.lock, "utf8")) !== this.lockOwner)
|
|
116
|
+
throw new Error("Another OpenCode process owns this memory directory.");
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
configureEmbeddingRuntime(bundle, this.dataDir);
|
|
120
|
+
const settings = path.join(
|
|
121
|
+
this.dataDir,
|
|
122
|
+
"tencentdb/runtime-settings.json",
|
|
123
|
+
);
|
|
124
|
+
try {
|
|
125
|
+
await readFile(settings);
|
|
126
|
+
} catch (cause) {
|
|
127
|
+
if ((cause as NodeJS.ErrnoException).code !== "ENOENT") throw cause;
|
|
128
|
+
await mkdir(path.dirname(settings), { recursive: true, mode: 0o700 });
|
|
129
|
+
const ports = new Set<number>();
|
|
130
|
+
while (ports.size < 3) ports.add(await freePort());
|
|
131
|
+
const [corePort, knowledgePort, panelPort] = [...ports];
|
|
132
|
+
await writeFile(
|
|
133
|
+
settings,
|
|
134
|
+
JSON.stringify(
|
|
135
|
+
{
|
|
136
|
+
corePort,
|
|
137
|
+
knowledgePort,
|
|
138
|
+
panelPort,
|
|
139
|
+
embedding: {
|
|
140
|
+
provider: "openai",
|
|
141
|
+
baseUrl: "http://127.0.0.1:11434/v1",
|
|
142
|
+
apiKey: "ollama",
|
|
143
|
+
model: "bge-m3",
|
|
144
|
+
dimensions: 1024,
|
|
145
|
+
sendDimensions: false,
|
|
146
|
+
maxInputChars: 8192,
|
|
147
|
+
timeoutMs: 60000,
|
|
148
|
+
},
|
|
149
|
+
knowledgeWorkspaces: [],
|
|
150
|
+
},
|
|
151
|
+
null,
|
|
152
|
+
2,
|
|
153
|
+
),
|
|
154
|
+
{ flag: "wx", mode: 0o600 },
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
const configFile = await startNativeMemory(bundle, this.dataDir);
|
|
158
|
+
this.config = JSON.parse(
|
|
159
|
+
await readFile(configFile, "utf8"),
|
|
160
|
+
) as Configuration;
|
|
161
|
+
this.panelUrl = nativeConfig(this.dataDir).panelUrl;
|
|
162
|
+
await new Promise<void>((resolve, reject) => {
|
|
163
|
+
this.server.once("error", reject);
|
|
164
|
+
this.server.listen(0, "127.0.0.1", resolve);
|
|
165
|
+
});
|
|
166
|
+
const address = this.server.address();
|
|
167
|
+
if (!address || typeof address === "string")
|
|
168
|
+
throw new Error("Memory host could not listen.");
|
|
169
|
+
this.origin = `http://127.0.0.1:${address.port}`;
|
|
170
|
+
this.cookie = `opencode_tc_${address.port}`;
|
|
171
|
+
} catch (cause) {
|
|
172
|
+
await this.stop();
|
|
173
|
+
throw cause;
|
|
174
|
+
}
|
|
175
|
+
return this;
|
|
176
|
+
}
|
|
177
|
+
client() {
|
|
178
|
+
return new TencentMemoryClient(this.config);
|
|
179
|
+
}
|
|
180
|
+
embeddingStatus() {
|
|
181
|
+
return embeddingStatus();
|
|
182
|
+
}
|
|
183
|
+
probeEmbedding(input: { baseUrl: string; model: string; apiKey?: string }) {
|
|
184
|
+
return probeEmbedding(input);
|
|
185
|
+
}
|
|
186
|
+
applyEmbedding(input: { baseUrl: string; model: string; apiKey?: string }) {
|
|
187
|
+
applyEmbedding(input);
|
|
188
|
+
}
|
|
189
|
+
generation(model: Model, generate: Generate) {
|
|
190
|
+
for (const [key, value] of this.capabilities)
|
|
191
|
+
if (value.expires < Date.now()) this.capabilities.delete(key);
|
|
192
|
+
if (this.capabilities.size >= 1000)
|
|
193
|
+
throw new Error("Too many pending memory generation jobs.");
|
|
194
|
+
const token = nonce();
|
|
195
|
+
this.capabilities.set(token, {
|
|
196
|
+
model: { ...model },
|
|
197
|
+
generate,
|
|
198
|
+
expires: Date.now() + 24 * 60 * 60_000,
|
|
199
|
+
});
|
|
200
|
+
return {
|
|
201
|
+
base_url: `${this.origin}/v1`,
|
|
202
|
+
model: `${model.providerID}/${model.id}`,
|
|
203
|
+
callback_token: token,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
panel() {
|
|
207
|
+
for (const [key, expires] of this.tickets)
|
|
208
|
+
if (expires < Date.now()) this.tickets.delete(key);
|
|
209
|
+
const ticket = nonce();
|
|
210
|
+
this.tickets.set(ticket, Date.now() + 60_000);
|
|
211
|
+
return `${this.origin}/?ticket=${ticket}`;
|
|
212
|
+
}
|
|
213
|
+
private async serve(request: IncomingMessage, response: ServerResponse) {
|
|
214
|
+
try {
|
|
215
|
+
if (request.headers.host !== new URL(this.origin).host) {
|
|
216
|
+
response.writeHead(403).end();
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const url = new URL(request.url ?? "/", this.origin);
|
|
220
|
+
if (url.pathname === "/v1/chat/completions") {
|
|
221
|
+
const token =
|
|
222
|
+
request.headers.authorization?.replace(/^Bearer /, "") ?? "";
|
|
223
|
+
const job = this.capabilities.get(token);
|
|
224
|
+
if (!job || job.expires < Date.now()) {
|
|
225
|
+
response.writeHead(401).end();
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (request.method !== "POST") {
|
|
229
|
+
response.writeHead(405).end();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const input = z
|
|
233
|
+
.record(z.string(), z.unknown())
|
|
234
|
+
.parse(JSON.parse((await rawBody(request)).toString("utf8")));
|
|
235
|
+
if (
|
|
236
|
+
input.model !== `${job.model.providerID}/${job.model.id}` ||
|
|
237
|
+
input.stream === true ||
|
|
238
|
+
input.tools
|
|
239
|
+
)
|
|
240
|
+
throw new Error(
|
|
241
|
+
"Memory generation must use its bound model without tools or streaming.",
|
|
242
|
+
);
|
|
243
|
+
const messages = z
|
|
244
|
+
.array(z.object({ role: z.string(), content: z.unknown() }))
|
|
245
|
+
.min(1)
|
|
246
|
+
.parse(input.messages);
|
|
247
|
+
const signal = AbortSignal.timeout(300_000);
|
|
248
|
+
const text = await job.generate(
|
|
249
|
+
messages
|
|
250
|
+
.map(
|
|
251
|
+
(m) =>
|
|
252
|
+
`${m.role}:\n${typeof m.content === "string" ? m.content : JSON.stringify(m.content)}`,
|
|
253
|
+
)
|
|
254
|
+
.join("\n\n"),
|
|
255
|
+
signal,
|
|
256
|
+
);
|
|
257
|
+
response.writeHead(200, { "Content-Type": "application/json" }).end(
|
|
258
|
+
JSON.stringify({
|
|
259
|
+
id: "memory-generation",
|
|
260
|
+
object: "chat.completion",
|
|
261
|
+
model: input.model,
|
|
262
|
+
choices: [
|
|
263
|
+
{
|
|
264
|
+
index: 0,
|
|
265
|
+
message: { role: "assistant", content: text },
|
|
266
|
+
finish_reason: "stop",
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
}),
|
|
270
|
+
);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const ticket = url.searchParams.get("ticket");
|
|
274
|
+
let bootstrap = "";
|
|
275
|
+
if (ticket && (this.tickets.get(ticket) ?? 0) > Date.now()) {
|
|
276
|
+
this.tickets.delete(ticket);
|
|
277
|
+
bootstrap = nonce();
|
|
278
|
+
this.browserKeys.set(bootstrap, Date.now() + 12 * 60 * 60_000);
|
|
279
|
+
response.setHeader(
|
|
280
|
+
"Set-Cookie",
|
|
281
|
+
`${this.cookie}=${bootstrap}; HttpOnly; SameSite=Strict; Path=/; Max-Age=43200`,
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
const headerKey = request.headers["x-opencode-tencent-session"];
|
|
285
|
+
const key =
|
|
286
|
+
bootstrap ||
|
|
287
|
+
(typeof headerKey === "string" ? headerKey : "") ||
|
|
288
|
+
request.headers.cookie
|
|
289
|
+
?.split(";")
|
|
290
|
+
.map((c) => c.trim())
|
|
291
|
+
.find((c) => c.startsWith(`${this.cookie}=`))
|
|
292
|
+
?.slice(this.cookie.length + 1) ||
|
|
293
|
+
"";
|
|
294
|
+
const asset =
|
|
295
|
+
request.method === "GET" &&
|
|
296
|
+
(url.pathname === "/logo.png" ||
|
|
297
|
+
/^\/assets\/[a-zA-Z0-9_.-]+\.(js|css|svg|png|woff2?)$/.test(
|
|
298
|
+
url.pathname,
|
|
299
|
+
));
|
|
300
|
+
if (!asset && (this.browserKeys.get(key) ?? 0) <= Date.now()) {
|
|
301
|
+
response
|
|
302
|
+
.writeHead(401)
|
|
303
|
+
.end("Open Tencent Memory from OpenCode to sign in.");
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (request.headers.origin && request.headers.origin !== this.origin) {
|
|
307
|
+
response.writeHead(403).end();
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
const forwardHeaders: Record<string, string> = {};
|
|
311
|
+
for (const name of [
|
|
312
|
+
"content-type",
|
|
313
|
+
"x-tdai-service-id",
|
|
314
|
+
"x-tdai-user-key",
|
|
315
|
+
]) {
|
|
316
|
+
const value = request.headers[name];
|
|
317
|
+
if (typeof value === "string") forwardHeaders[name] = value;
|
|
318
|
+
}
|
|
319
|
+
const upstream = await fetch(
|
|
320
|
+
new URL(url.pathname + (ticket ? "" : url.search), this.panelUrl),
|
|
321
|
+
{
|
|
322
|
+
method: request.method,
|
|
323
|
+
redirect: "error",
|
|
324
|
+
signal: AbortSignal.timeout(30_000),
|
|
325
|
+
headers: forwardHeaders,
|
|
326
|
+
body: ["GET", "HEAD"].includes(request.method ?? "")
|
|
327
|
+
? undefined
|
|
328
|
+
: Uint8Array.from(await rawBody(request, 20 * 1024 * 1024)),
|
|
329
|
+
},
|
|
330
|
+
);
|
|
331
|
+
const headers: Record<string, string> = {
|
|
332
|
+
"Content-Type":
|
|
333
|
+
upstream.headers.get("content-type") ?? "application/octet-stream",
|
|
334
|
+
"Cache-Control": "no-store",
|
|
335
|
+
"Referrer-Policy": "no-referrer",
|
|
336
|
+
};
|
|
337
|
+
if (
|
|
338
|
+
upstream.headers.get("content-type")?.includes("text/html") &&
|
|
339
|
+
url.pathname === "/"
|
|
340
|
+
) {
|
|
341
|
+
const userKey = (
|
|
342
|
+
await readFile(
|
|
343
|
+
path.join(this.dataDir, "tencentdb/business-key"),
|
|
344
|
+
"utf8",
|
|
345
|
+
)
|
|
346
|
+
).trim();
|
|
347
|
+
const identity = await this.client().call<{ user: unknown }>(
|
|
348
|
+
"meta/auth/verify",
|
|
349
|
+
{ user_key: userKey },
|
|
350
|
+
);
|
|
351
|
+
const session = JSON.stringify({
|
|
352
|
+
authMethod: "user_key",
|
|
353
|
+
instanceId: "default",
|
|
354
|
+
instanceName: "OpenCode",
|
|
355
|
+
userKey,
|
|
356
|
+
user: identity.user,
|
|
357
|
+
}).replace(/</g, "\\u003c");
|
|
358
|
+
// Cross-site Desktop iframes may block cookies. Add a capability only to same-origin fetches.
|
|
359
|
+
const html = (await upstream.text()).replace(
|
|
360
|
+
"<head>",
|
|
361
|
+
`<head><script>localStorage.setItem('tdai-panel.session',JSON.stringify(${session}));const originalFetch=window.fetch.bind(window);window.fetch=(input,init)=>{const r=new Request(input,init);if(new URL(r.url).origin!==location.origin)return originalFetch(r);const h=new Headers(r.headers);h.set('X-OpenCode-Tencent-Session',${JSON.stringify(key)});return originalFetch(new Request(r,{headers:h}));};</script>`,
|
|
362
|
+
);
|
|
363
|
+
response.writeHead(upstream.status, headers).end(html);
|
|
364
|
+
} else
|
|
365
|
+
response
|
|
366
|
+
.writeHead(upstream.status, headers)
|
|
367
|
+
.end(Buffer.from(await upstream.arrayBuffer()));
|
|
368
|
+
} catch {
|
|
369
|
+
response
|
|
370
|
+
.writeHead(502, { "Content-Type": "text/plain" })
|
|
371
|
+
.end("Tencent memory request failed. Check the private runtime logs.");
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
async stop() {
|
|
375
|
+
this.capabilities.clear();
|
|
376
|
+
this.tickets.clear();
|
|
377
|
+
this.browserKeys.clear();
|
|
378
|
+
this.server.closeAllConnections();
|
|
379
|
+
if (this.server.listening)
|
|
380
|
+
await new Promise<void>((resolve) => this.server.close(() => resolve()));
|
|
381
|
+
await stopEmbeddingJob();
|
|
382
|
+
await stopNativeMemory();
|
|
383
|
+
if (
|
|
384
|
+
this.lock &&
|
|
385
|
+
(await readFile(this.lock, "utf8").catch(() => "")) === this.lockOwner
|
|
386
|
+
)
|
|
387
|
+
await rm(this.lock);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
type Shared = {
|
|
392
|
+
key: string;
|
|
393
|
+
refs: number;
|
|
394
|
+
runtime: Promise<Runtime>;
|
|
395
|
+
closing?: Promise<void>;
|
|
396
|
+
};
|
|
397
|
+
const key = Symbol.for("opencode-tencentdb-plugin.runtime.v1");
|
|
398
|
+
const host = globalThis as typeof globalThis & { [key]?: Shared };
|
|
399
|
+
export async function acquire(options: Options) {
|
|
400
|
+
while (host[key]?.closing) await host[key]!.closing;
|
|
401
|
+
const identity = JSON.stringify({
|
|
402
|
+
dataDir: options.dataDir,
|
|
403
|
+
runtimeDir: options.runtimeDir,
|
|
404
|
+
});
|
|
405
|
+
if (host[key] && host[key].key !== identity)
|
|
406
|
+
throw new Error(
|
|
407
|
+
"Tencent runtime settings must be identical across OpenCode projects. Set them globally.",
|
|
408
|
+
);
|
|
409
|
+
const state = (host[key] ??= {
|
|
410
|
+
key: identity,
|
|
411
|
+
refs: 0,
|
|
412
|
+
runtime: new Runtime(options).start(),
|
|
413
|
+
});
|
|
414
|
+
state.refs++;
|
|
415
|
+
let runtime: Runtime;
|
|
416
|
+
try {
|
|
417
|
+
runtime = await state.runtime;
|
|
418
|
+
} catch (cause) {
|
|
419
|
+
if (--state.refs === 0) delete host[key];
|
|
420
|
+
throw cause;
|
|
421
|
+
}
|
|
422
|
+
let released = false;
|
|
423
|
+
return {
|
|
424
|
+
runtime,
|
|
425
|
+
async release() {
|
|
426
|
+
if (released) return;
|
|
427
|
+
released = true;
|
|
428
|
+
if (--state.refs === 0) {
|
|
429
|
+
state.closing = runtime.stop().finally(() => {
|
|
430
|
+
if (host[key] === state) delete host[key];
|
|
431
|
+
});
|
|
432
|
+
await state.closing;
|
|
433
|
+
}
|
|
434
|
+
},
|
|
435
|
+
};
|
|
436
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Tencent is pleased to support the open source community by making TencentDB Agent Memory available.
|
|
2
|
+
|
|
3
|
+
Copyright (C) 2026 Tencent. All rights reserved.
|
|
4
|
+
|
|
5
|
+
TencentDB Agent Memory is licensed under the MIT.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Terms of the MIT:
|
|
9
|
+
--------------------------------------------------------------------
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
11
|
+
a copy of this software and associated documentation files (the
|
|
12
|
+
"Software"), to deal in the Software without restriction, including
|
|
13
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
14
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
15
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
16
|
+
the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be
|
|
19
|
+
included in all copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
22
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
23
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
24
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
25
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
26
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
27
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|