@celljs/ta-secrets 0.0.0-stage → 4.18.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.en.md +22 -0
- package/README.md +22 -2
- package/extension/index.mjs +742 -0
- package/host/index.mjs +1388 -0
- package/package.json +65 -4
- package/web/index.js +593 -0
|
@@ -0,0 +1,742 @@
|
|
|
1
|
+
// ../ta-protocol/esm/common/ta-extension-host.js
|
|
2
|
+
var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
|
|
3
|
+
var TA_HOST_EVENT = "ta:host";
|
|
4
|
+
var TaHostUtil;
|
|
5
|
+
(function(TaHostUtil2) {
|
|
6
|
+
TaHostUtil2.WAIT_MS = 1000;
|
|
7
|
+
function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
|
|
8
|
+
return new Promise((resolve) => {
|
|
9
|
+
let heard = false;
|
|
10
|
+
let answered = false;
|
|
11
|
+
const timer = setTimeout(() => resolve(undefined), waitMs);
|
|
12
|
+
const request = {
|
|
13
|
+
name,
|
|
14
|
+
heard: () => {
|
|
15
|
+
heard = true;
|
|
16
|
+
},
|
|
17
|
+
resolve: (answer) => {
|
|
18
|
+
answered = true;
|
|
19
|
+
clearTimeout(timer);
|
|
20
|
+
resolve(answer);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
events.emit(TA_HOST_EVENT, request);
|
|
24
|
+
if (!heard && !answered) {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
resolve(undefined);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
TaHostUtil2.reach = reach;
|
|
31
|
+
function marked(systemPrompt, mark) {
|
|
32
|
+
return role(systemPrompt).includes(mark);
|
|
33
|
+
}
|
|
34
|
+
TaHostUtil2.marked = marked;
|
|
35
|
+
function role(systemPrompt) {
|
|
36
|
+
if (!systemPrompt) {
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
const open = `<addendum>
|
|
40
|
+
`;
|
|
41
|
+
const start = systemPrompt.indexOf(open);
|
|
42
|
+
const context = systemPrompt.indexOf(`<project_context>
|
|
43
|
+
`);
|
|
44
|
+
if (start < 0 || context >= 0 && context < start) {
|
|
45
|
+
return "";
|
|
46
|
+
}
|
|
47
|
+
const end = systemPrompt.indexOf(`
|
|
48
|
+
</addendum>`, start);
|
|
49
|
+
return systemPrompt.slice(start + open.length, end < 0 ? undefined : end);
|
|
50
|
+
}
|
|
51
|
+
TaHostUtil2.role = role;
|
|
52
|
+
})(TaHostUtil || (TaHostUtil = {}));
|
|
53
|
+
var TA_ASK_ENTRY = "ta-ask";
|
|
54
|
+
var TaAskUtil;
|
|
55
|
+
(function(TaAskUtil2) {
|
|
56
|
+
const LENGTH = 120;
|
|
57
|
+
function entry(question) {
|
|
58
|
+
const line = question.replace(/\s+/g, " ").trim();
|
|
59
|
+
return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
|
|
60
|
+
}
|
|
61
|
+
TaAskUtil2.entry = entry;
|
|
62
|
+
function asking(entries) {
|
|
63
|
+
var _a;
|
|
64
|
+
for (let i = entries.length - 1;i >= 0; i--) {
|
|
65
|
+
const entry2 = entries[i];
|
|
66
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
|
|
70
|
+
return read(entry2);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
TaAskUtil2.asking = asking;
|
|
76
|
+
function next(asked, entry2) {
|
|
77
|
+
var _a;
|
|
78
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
|
|
82
|
+
}
|
|
83
|
+
TaAskUtil2.next = next;
|
|
84
|
+
function read(found) {
|
|
85
|
+
var _a;
|
|
86
|
+
const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
|
|
87
|
+
if (typeof question !== "string" || !question.trim()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
|
|
91
|
+
return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
|
|
92
|
+
}
|
|
93
|
+
})(TaAskUtil || (TaAskUtil = {}));
|
|
94
|
+
|
|
95
|
+
// web-src/command-prelude.ts
|
|
96
|
+
var CommandPrelude;
|
|
97
|
+
((CommandPrelude) => {
|
|
98
|
+
function quote(value) {
|
|
99
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
100
|
+
}
|
|
101
|
+
CommandPrelude.quote = quote;
|
|
102
|
+
function usesGit(command) {
|
|
103
|
+
return /(^|[\s;&|(`])git\s+-?[A-Za-z]/.test(command);
|
|
104
|
+
}
|
|
105
|
+
CommandPrelude.usesGit = usesGit;
|
|
106
|
+
function usesSsh(command) {
|
|
107
|
+
return /(^|[\s;&|(`/])(ssh|scp|sftp|rsync|git)(\s|$)/.test(command);
|
|
108
|
+
}
|
|
109
|
+
CommandPrelude.usesSsh = usesSsh;
|
|
110
|
+
function gitHelper(name, user) {
|
|
111
|
+
return `!f() { test "$1" = get || return 0; printf 'username=%s\\npassword=%s\\n' ${quote(user)} "$TA_SECRET_${name}"; }; f`;
|
|
112
|
+
}
|
|
113
|
+
CommandPrelude.gitHelper = gitHelper;
|
|
114
|
+
function script(parts) {
|
|
115
|
+
const lines = [];
|
|
116
|
+
for (const [name, value] of parts.env) {
|
|
117
|
+
lines.push(`export TA_SECRET_${name}=${quote(value)}`);
|
|
118
|
+
}
|
|
119
|
+
if (parts.git.length) {
|
|
120
|
+
lines.push("ta_git=${GIT_CONFIG_COUNT:-0}");
|
|
121
|
+
const config = (key, value) => {
|
|
122
|
+
lines.push(`export "GIT_CONFIG_KEY_$ta_git"=${quote(key)} "GIT_CONFIG_VALUE_$ta_git"=${quote(value)}`, "ta_git=$((ta_git + 1))");
|
|
123
|
+
};
|
|
124
|
+
for (const one of parts.git) {
|
|
125
|
+
const key = `credential.https://${one.host}.helper`;
|
|
126
|
+
config(key, "");
|
|
127
|
+
config(key, gitHelper(one.name, one.user));
|
|
128
|
+
}
|
|
129
|
+
lines.push("export GIT_CONFIG_COUNT=$ta_git GIT_TERMINAL_PROMPT=0", "unset ta_git");
|
|
130
|
+
}
|
|
131
|
+
if (parts.proxy) {
|
|
132
|
+
const url = quote(parts.proxy.url);
|
|
133
|
+
const bundle = quote(parts.proxy.bundle);
|
|
134
|
+
lines.push(`export HTTPS_PROXY=${url} https_proxy=${url} HTTP_PROXY=${url} http_proxy=${url} NODE_USE_ENV_PROXY=1`, "export NO_PROXY='localhost,127.0.0.1,::1' no_proxy='localhost,127.0.0.1,::1'", `export SSL_CERT_FILE=${bundle} CURL_CA_BUNDLE=${bundle} REQUESTS_CA_BUNDLE=${bundle} NODE_EXTRA_CA_CERTS=${bundle} GIT_SSL_CAINFO=${bundle} DENO_CERT=${bundle}`, "unset ALL_PROXY all_proxy");
|
|
135
|
+
}
|
|
136
|
+
if (parts.keys.length) {
|
|
137
|
+
lines.push('eval "$(ssh-agent -s -t 1800)" >/dev/null');
|
|
138
|
+
for (const key of parts.keys) {
|
|
139
|
+
lines.push(`ssh-add -q ${quote(key)} </dev/null >/dev/null 2>&1`);
|
|
140
|
+
}
|
|
141
|
+
lines.push("trap 'ssh-agent -k >/dev/null 2>&1' EXIT");
|
|
142
|
+
}
|
|
143
|
+
lines.push("true");
|
|
144
|
+
return `${lines.join(`
|
|
145
|
+
`)}
|
|
146
|
+
`;
|
|
147
|
+
}
|
|
148
|
+
CommandPrelude.script = script;
|
|
149
|
+
})(CommandPrelude ||= {});
|
|
150
|
+
|
|
151
|
+
// web-src/secrets-protocol.ts
|
|
152
|
+
var NAME = "@celljs/ta-secrets";
|
|
153
|
+
var SECTION = "ta-secrets";
|
|
154
|
+
var REQUEST_TOOL = "request_secret";
|
|
155
|
+
var ENV_PREFIX = "TA_SECRET_";
|
|
156
|
+
var Keys;
|
|
157
|
+
((Keys) => {
|
|
158
|
+
Keys.ENTRIES = "entries";
|
|
159
|
+
Keys.CAPTURE = "capture";
|
|
160
|
+
Keys.PERSONAL = "personal";
|
|
161
|
+
Keys.USES = "uses";
|
|
162
|
+
})(Keys ||= {});
|
|
163
|
+
var SecretNames;
|
|
164
|
+
((SecretNames) => {
|
|
165
|
+
SecretNames.MAX = 48;
|
|
166
|
+
function normalize(name) {
|
|
167
|
+
return name.trim().replace(/^\$?\{?TA_SECRET_/i, "").replace(/[^A-Za-z0-9]+/g, "_").replace(/^_+|_+$/g, "").toUpperCase().slice(0, SecretNames.MAX);
|
|
168
|
+
}
|
|
169
|
+
SecretNames.normalize = normalize;
|
|
170
|
+
function valid(name) {
|
|
171
|
+
return /^[A-Z0-9][A-Z0-9_]*$/.test(name) && name.length <= SecretNames.MAX;
|
|
172
|
+
}
|
|
173
|
+
SecretNames.valid = valid;
|
|
174
|
+
function env(name) {
|
|
175
|
+
return ENV_PREFIX + name;
|
|
176
|
+
}
|
|
177
|
+
SecretNames.env = env;
|
|
178
|
+
function handle(name) {
|
|
179
|
+
return `$${env(name)}`;
|
|
180
|
+
}
|
|
181
|
+
SecretNames.handle = handle;
|
|
182
|
+
function references(command) {
|
|
183
|
+
return [...new Set([...command.matchAll(/\bTA_SECRET_([A-Z0-9_]+)/g)].map((match) => match[1]))];
|
|
184
|
+
}
|
|
185
|
+
SecretNames.references = references;
|
|
186
|
+
function free(base, taken) {
|
|
187
|
+
const name = normalize(base) || "SECRET";
|
|
188
|
+
if (!taken.has(name)) {
|
|
189
|
+
return name;
|
|
190
|
+
}
|
|
191
|
+
for (let n = 2;; n++) {
|
|
192
|
+
const next = `${name.slice(0, SecretNames.MAX - String(n).length - 1)}_${n}`;
|
|
193
|
+
if (!taken.has(next)) {
|
|
194
|
+
return next;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
SecretNames.free = free;
|
|
199
|
+
function looksSecret(value) {
|
|
200
|
+
if (/[()[\]{}<>]/.test(value)) {
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
return /[0-9]/.test(value) && /[A-Za-z]/.test(value) || value.length >= 20;
|
|
204
|
+
}
|
|
205
|
+
SecretNames.looksSecret = looksSecret;
|
|
206
|
+
function hosts(value) {
|
|
207
|
+
const items = Array.isArray(value) ? value : typeof value === "string" ? value.split(/[\s,;]+/) : [];
|
|
208
|
+
const hosts2 = items.map((item) => String(item).trim().toLowerCase().replace(/^[a-z]+:\/\//, "").replace(/^[^@/]*@/, "").replace(/\/.*$/, "")).filter((host) => /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+(:\d{1,5})?$/.test(host));
|
|
209
|
+
return [...new Set(hosts2)];
|
|
210
|
+
}
|
|
211
|
+
SecretNames.hosts = hosts;
|
|
212
|
+
function defaultHosts(name) {
|
|
213
|
+
return /GITHUB/.test(name) ? ["github.com", "api.github.com"] : /GITLAB/.test(name) ? ["gitlab.com"] : /GITEE/.test(name) ? ["gitee.com"] : [];
|
|
214
|
+
}
|
|
215
|
+
SecretNames.defaultHosts = defaultHosts;
|
|
216
|
+
function gitUser(entry, host) {
|
|
217
|
+
return entry.gitUser || (host === "gitlab.com" || host.startsWith("gitlab.") ? "oauth2" : "x-access-token");
|
|
218
|
+
}
|
|
219
|
+
SecretNames.gitUser = gitUser;
|
|
220
|
+
function privateKey(value) {
|
|
221
|
+
return /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*-----END [A-Z0-9 ]*PRIVATE KEY-----/.test(value);
|
|
222
|
+
}
|
|
223
|
+
SecretNames.privateKey = privateKey;
|
|
224
|
+
function uses(value) {
|
|
225
|
+
return Array.isArray(value) ? value.filter((use) => !!use && typeof use === "object" && typeof use.at === "number" && Array.isArray(use.names)) : [];
|
|
226
|
+
}
|
|
227
|
+
SecretNames.uses = uses;
|
|
228
|
+
function entries(value) {
|
|
229
|
+
if (!Array.isArray(value)) {
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
return value.map((entry) => entry && typeof entry === "object" && Array.isArray(entry.git) && !entry.hosts ? (({ git, ...rest }) => ({ ...rest, hosts: git }))(entry) : entry).filter((entry) => !!entry && typeof entry === "object" && typeof entry.name === "string" && valid(entry.name));
|
|
233
|
+
}
|
|
234
|
+
SecretNames.entries = entries;
|
|
235
|
+
})(SecretNames ||= {});
|
|
236
|
+
|
|
237
|
+
// ../ta-protocol/esm/common/language.js
|
|
238
|
+
var TaLanguageUtil;
|
|
239
|
+
(function(TaLanguageUtil2) {
|
|
240
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
241
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
242
|
+
function of(tag) {
|
|
243
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
244
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
245
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
246
|
+
}
|
|
247
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
248
|
+
}
|
|
249
|
+
TaLanguageUtil2.of = of;
|
|
250
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
251
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
252
|
+
}
|
|
253
|
+
TaLanguageUtil2.server = server;
|
|
254
|
+
function pick(language, words) {
|
|
255
|
+
return words[of(language)];
|
|
256
|
+
}
|
|
257
|
+
TaLanguageUtil2.pick = pick;
|
|
258
|
+
function say(words) {
|
|
259
|
+
return words[server()];
|
|
260
|
+
}
|
|
261
|
+
TaLanguageUtil2.say = say;
|
|
262
|
+
function chinese(language) {
|
|
263
|
+
return of(language).startsWith("zh");
|
|
264
|
+
}
|
|
265
|
+
TaLanguageUtil2.chinese = chinese;
|
|
266
|
+
function cjk(language) {
|
|
267
|
+
return of(language) !== "en-US";
|
|
268
|
+
}
|
|
269
|
+
TaLanguageUtil2.cjk = cjk;
|
|
270
|
+
function list(language, items) {
|
|
271
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
272
|
+
}
|
|
273
|
+
TaLanguageUtil2.list = list;
|
|
274
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
275
|
+
|
|
276
|
+
// ../ta-protocol/esm/common/message-protocol.js
|
|
277
|
+
var TaMessageContext;
|
|
278
|
+
(function(TaMessageContext2) {
|
|
279
|
+
const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
|
|
280
|
+
function wrap(context, message) {
|
|
281
|
+
return `<context>
|
|
282
|
+
${context}
|
|
283
|
+
</context>
|
|
284
|
+
${message}`;
|
|
285
|
+
}
|
|
286
|
+
TaMessageContext2.wrap = wrap;
|
|
287
|
+
function strip(message) {
|
|
288
|
+
return message.replace(BLOCK, "");
|
|
289
|
+
}
|
|
290
|
+
TaMessageContext2.strip = strip;
|
|
291
|
+
function read(message) {
|
|
292
|
+
var _a;
|
|
293
|
+
const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
|
|
294
|
+
return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
|
|
295
|
+
}
|
|
296
|
+
TaMessageContext2.read = read;
|
|
297
|
+
})(TaMessageContext || (TaMessageContext = {}));
|
|
298
|
+
var TaMessageSource;
|
|
299
|
+
(function(TaMessageSource2) {
|
|
300
|
+
TaMessageSource2.DOUBLE = "ta:double";
|
|
301
|
+
const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
|
|
302
|
+
function mark(message, from, detail) {
|
|
303
|
+
return marked(message, line(from, detail));
|
|
304
|
+
}
|
|
305
|
+
TaMessageSource2.mark = mark;
|
|
306
|
+
function notice(message, from, detail) {
|
|
307
|
+
return marked(message, line(from, detail, true));
|
|
308
|
+
}
|
|
309
|
+
TaMessageSource2.notice = notice;
|
|
310
|
+
function line(from, detail, notice2 = false) {
|
|
311
|
+
return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
|
|
312
|
+
}
|
|
313
|
+
TaMessageSource2.line = line;
|
|
314
|
+
function of(message) {
|
|
315
|
+
const context = TaMessageContext.read(message);
|
|
316
|
+
const match = context === undefined ? null : LINE.exec(context);
|
|
317
|
+
return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
|
|
318
|
+
}
|
|
319
|
+
TaMessageSource2.of = of;
|
|
320
|
+
function isNotice(message) {
|
|
321
|
+
var _a;
|
|
322
|
+
return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
|
|
323
|
+
}
|
|
324
|
+
TaMessageSource2.isNotice = isNotice;
|
|
325
|
+
TaMessageSource2.SECTION = "ta-sources";
|
|
326
|
+
TaMessageSource2.PROMPT = [
|
|
327
|
+
"Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
|
|
328
|
+
"One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
|
|
329
|
+
"- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
|
|
330
|
+
"- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
|
|
331
|
+
"- Never quote it as the user's words nor answer it as if they had said it."
|
|
332
|
+
].join(`
|
|
333
|
+
`);
|
|
334
|
+
function marked(message, source) {
|
|
335
|
+
if (/^[/!]/.test(message) || of(message)) {
|
|
336
|
+
return message;
|
|
337
|
+
}
|
|
338
|
+
const context = TaMessageContext.read(message);
|
|
339
|
+
return TaMessageContext.wrap(context === undefined ? source : `${context}
|
|
340
|
+
${source}`, TaMessageContext.strip(message));
|
|
341
|
+
}
|
|
342
|
+
})(TaMessageSource || (TaMessageSource = {}));
|
|
343
|
+
var TaImageNote;
|
|
344
|
+
(function(TaImageNote2) {
|
|
345
|
+
const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$/;
|
|
346
|
+
function strip(text) {
|
|
347
|
+
const lines = text.split(`
|
|
348
|
+
`);
|
|
349
|
+
const kept = lines.filter((line) => !NOTE.test(line.trim()));
|
|
350
|
+
if (kept.length === lines.length) {
|
|
351
|
+
return text;
|
|
352
|
+
}
|
|
353
|
+
return kept.join(`
|
|
354
|
+
`).replace(/\n+$/, "").replace(/^\n+/, "");
|
|
355
|
+
}
|
|
356
|
+
TaImageNote2.strip = strip;
|
|
357
|
+
})(TaImageNote || (TaImageNote = {}));
|
|
358
|
+
var TaImageSize;
|
|
359
|
+
(function(TaImageSize2) {
|
|
360
|
+
const JPEG_BYTES = 256 * 1024;
|
|
361
|
+
function of(base64) {
|
|
362
|
+
var _a, _b, _c;
|
|
363
|
+
let bytes;
|
|
364
|
+
try {
|
|
365
|
+
const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
|
|
366
|
+
bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
367
|
+
} catch (_d) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
|
|
371
|
+
return size && size.width > 0 && size.height > 0 ? size : undefined;
|
|
372
|
+
}
|
|
373
|
+
TaImageSize2.of = of;
|
|
374
|
+
function u16be(bytes, at) {
|
|
375
|
+
return bytes[at] << 8 | bytes[at + 1];
|
|
376
|
+
}
|
|
377
|
+
function u16le(bytes, at) {
|
|
378
|
+
return bytes[at] | bytes[at + 1] << 8;
|
|
379
|
+
}
|
|
380
|
+
function u24le(bytes, at) {
|
|
381
|
+
return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
|
|
382
|
+
}
|
|
383
|
+
function u32be(bytes, at) {
|
|
384
|
+
return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
|
|
385
|
+
}
|
|
386
|
+
function text(bytes, at, length) {
|
|
387
|
+
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
388
|
+
}
|
|
389
|
+
function png(bytes) {
|
|
390
|
+
return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
|
|
391
|
+
}
|
|
392
|
+
function gif(bytes) {
|
|
393
|
+
return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
|
|
394
|
+
}
|
|
395
|
+
function webp(bytes) {
|
|
396
|
+
if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
switch (text(bytes, 12, 4)) {
|
|
400
|
+
case "VP8 ":
|
|
401
|
+
return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
|
|
402
|
+
case "VP8L": {
|
|
403
|
+
const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
|
|
404
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
405
|
+
}
|
|
406
|
+
case "VP8X":
|
|
407
|
+
return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
|
|
408
|
+
default:
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
function jpeg(bytes) {
|
|
413
|
+
if (bytes[0] !== 255 || bytes[1] !== 216) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
let at = 2;
|
|
417
|
+
while (at + 9 < bytes.length) {
|
|
418
|
+
if (bytes[at] !== 255) {
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
const marker = bytes[at + 1];
|
|
422
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
423
|
+
return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
|
|
424
|
+
}
|
|
425
|
+
at += 2 + u16be(bytes, at + 2);
|
|
426
|
+
}
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
})(TaImageSize || (TaImageSize = {}));
|
|
430
|
+
var TaAgentSummary;
|
|
431
|
+
(function(TaAgentSummary2) {
|
|
432
|
+
const START = /^\s*<conversation_history_summary>/;
|
|
433
|
+
const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
|
|
434
|
+
function read(text) {
|
|
435
|
+
if (!START.test(text)) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
|
|
439
|
+
}
|
|
440
|
+
TaAgentSummary2.read = read;
|
|
441
|
+
function strip(text) {
|
|
442
|
+
return text.replace(BLOCK, "");
|
|
443
|
+
}
|
|
444
|
+
TaAgentSummary2.strip = strip;
|
|
445
|
+
})(TaAgentSummary || (TaAgentSummary = {}));
|
|
446
|
+
var TaSkillUse;
|
|
447
|
+
(function(TaSkillUse2) {
|
|
448
|
+
const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
449
|
+
function parse(message) {
|
|
450
|
+
var _a;
|
|
451
|
+
const match = BLOCK.exec(message);
|
|
452
|
+
if (!match) {
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
|
|
456
|
+
}
|
|
457
|
+
TaSkillUse2.parse = parse;
|
|
458
|
+
function command(use) {
|
|
459
|
+
return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
|
|
460
|
+
}
|
|
461
|
+
TaSkillUse2.command = command;
|
|
462
|
+
})(TaSkillUse || (TaSkillUse = {}));
|
|
463
|
+
var TaTaskNote;
|
|
464
|
+
(function(TaTaskNote2) {
|
|
465
|
+
const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
|
|
466
|
+
const ATTRIBUTE = /(\w+)="([^"]*)"/g;
|
|
467
|
+
function write(note) {
|
|
468
|
+
const attributes = [`id="${quote(note.id)}"`];
|
|
469
|
+
if (note.was !== undefined) {
|
|
470
|
+
attributes.push(`was="${quote(note.was)}"`);
|
|
471
|
+
}
|
|
472
|
+
attributes.push(`title="${quote(note.title)}"`);
|
|
473
|
+
if (note.status !== undefined) {
|
|
474
|
+
attributes.push(`status="${quote(note.status)}"`);
|
|
475
|
+
}
|
|
476
|
+
return `<task_${note.kind} ${attributes.join(" ")}>
|
|
477
|
+
${note.body}
|
|
478
|
+
</task_${note.kind}>`;
|
|
479
|
+
}
|
|
480
|
+
TaTaskNote2.write = write;
|
|
481
|
+
function parse(text) {
|
|
482
|
+
const match = NOTE.exec(text.trim());
|
|
483
|
+
if (!match) {
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
const attributes = {};
|
|
487
|
+
for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
|
|
488
|
+
attributes[name] = unquote(value);
|
|
489
|
+
}
|
|
490
|
+
if (!attributes.id || attributes.title === undefined) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const { id, title, status, was } = attributes;
|
|
494
|
+
return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
|
|
495
|
+
}
|
|
496
|
+
TaTaskNote2.parse = parse;
|
|
497
|
+
function quote(text) {
|
|
498
|
+
return text.replace(/&/g, "&").replace(/"/g, """).replace(/\n/g, " ");
|
|
499
|
+
}
|
|
500
|
+
function unquote(text) {
|
|
501
|
+
return text.replace(/"/g, '"').replace(/&/g, "&");
|
|
502
|
+
}
|
|
503
|
+
})(TaTaskNote || (TaTaskNote = {}));
|
|
504
|
+
var TaSecretRef;
|
|
505
|
+
(function(TaSecretRef2) {
|
|
506
|
+
TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
|
|
507
|
+
const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
|
|
508
|
+
const KINDS = [
|
|
509
|
+
[/^PHONE(?:_\d+)?$/, "phone"],
|
|
510
|
+
[/^EMAIL(?:_\d+)?$/, "email"],
|
|
511
|
+
[/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
|
|
512
|
+
[/^BANK_CARD(?:_\d+)?$/, "bankCard"]
|
|
513
|
+
];
|
|
514
|
+
function has(text) {
|
|
515
|
+
return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
|
|
516
|
+
}
|
|
517
|
+
TaSecretRef2.has = has;
|
|
518
|
+
function kind(name) {
|
|
519
|
+
var _a;
|
|
520
|
+
var _b;
|
|
521
|
+
return (_b = (_a = KINDS.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
|
|
522
|
+
}
|
|
523
|
+
TaSecretRef2.kind = kind;
|
|
524
|
+
function split(text) {
|
|
525
|
+
if (!has(text)) {
|
|
526
|
+
return [{ text }];
|
|
527
|
+
}
|
|
528
|
+
const parts = [];
|
|
529
|
+
let at = 0;
|
|
530
|
+
for (const match of text.matchAll(REF)) {
|
|
531
|
+
if (match.index > at) {
|
|
532
|
+
parts.push({ text: text.slice(at, match.index) });
|
|
533
|
+
}
|
|
534
|
+
parts.push({ secret: match[1], kind: kind(match[1]) });
|
|
535
|
+
at = match.index + match[0].length;
|
|
536
|
+
}
|
|
537
|
+
if (at < text.length) {
|
|
538
|
+
parts.push({ text: text.slice(at) });
|
|
539
|
+
}
|
|
540
|
+
return parts;
|
|
541
|
+
}
|
|
542
|
+
TaSecretRef2.split = split;
|
|
543
|
+
function plain(text, label) {
|
|
544
|
+
return has(text) ? text.replace(REF, (_whole, name) => label(name, kind(name))) : text;
|
|
545
|
+
}
|
|
546
|
+
TaSecretRef2.plain = plain;
|
|
547
|
+
})(TaSecretRef || (TaSecretRef = {}));
|
|
548
|
+
|
|
549
|
+
// web-src/server-texts.ts
|
|
550
|
+
var KINDS = {
|
|
551
|
+
"zh-CN": { phone: "手机号", email: "邮箱", idNumber: "证件号", bankCard: "银行卡号", secret: "密钥" },
|
|
552
|
+
"zh-TW": { phone: "手機號碼", email: "電子郵件", idNumber: "證件號碼", bankCard: "銀行卡號", secret: "金鑰" },
|
|
553
|
+
"en-US": { phone: "phone number", email: "email", idNumber: "ID number", bankCard: "bank card", secret: "secret" },
|
|
554
|
+
"ja-JP": { phone: "電話番号", email: "メール", idNumber: "身分証番号", bankCard: "銀行カード番号", secret: "秘密" }
|
|
555
|
+
};
|
|
556
|
+
var SecretsServerTexts;
|
|
557
|
+
((SecretsServerTexts) => {
|
|
558
|
+
function language(env = process.env) {
|
|
559
|
+
return TaLanguageUtil.of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || "");
|
|
560
|
+
}
|
|
561
|
+
SecretsServerTexts.language = language;
|
|
562
|
+
function captured(names) {
|
|
563
|
+
const words = TaLanguageUtil.say(KINDS);
|
|
564
|
+
const list = TaLanguageUtil.list(language(), [...new Set(names.map((name) => words[TaSecretRef.kind(name)]))]);
|
|
565
|
+
return TaLanguageUtil.say({
|
|
566
|
+
"zh-CN": `已把你发的${list}收进保险箱:Ta 看不到内容,用到时才填入`,
|
|
567
|
+
"zh-TW": `已把你發的${list}收進保險箱:Ta 看不到內容,用到時才填入`,
|
|
568
|
+
"en-US": `Kept the ${list} you sent in the vault: Ta never sees it, and fills it in where it is used`,
|
|
569
|
+
"ja-JP": `送信された${list}を金庫に保管しました。Ta には中身が見えず、使う時に入力されます`
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
SecretsServerTexts.captured = captured;
|
|
573
|
+
function ask(name, purpose, hosts = []) {
|
|
574
|
+
const host = TaLanguageUtil.list(language(), hosts);
|
|
575
|
+
return hosts.length ? TaLanguageUtil.say({
|
|
576
|
+
"zh-CN": `Ta 需要 ${name}(只发往 ${host}):${purpose}`,
|
|
577
|
+
"zh-TW": `Ta 需要 ${name}(只發往 ${host}):${purpose}`,
|
|
578
|
+
"en-US": `Ta needs ${name} (sent to ${host} only): ${purpose}`,
|
|
579
|
+
"ja-JP": `Ta に ${name} が必要です(${host} にのみ送信):${purpose}`
|
|
580
|
+
}) : TaLanguageUtil.say({
|
|
581
|
+
"zh-CN": `Ta 需要 ${name}:${purpose}`,
|
|
582
|
+
"zh-TW": `Ta 需要 ${name}:${purpose}`,
|
|
583
|
+
"en-US": `Ta needs ${name}: ${purpose}`,
|
|
584
|
+
"ja-JP": `Ta に ${name} が必要です:${purpose}`
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
SecretsServerTexts.ask = ask;
|
|
588
|
+
function keyPlaceholder() {
|
|
589
|
+
return TaLanguageUtil.say({
|
|
590
|
+
"zh-CN": "私钥文件的路径,如 ~/.ssh/id_ed25519",
|
|
591
|
+
"zh-TW": "私鑰檔案的路徑,如 ~/.ssh/id_ed25519",
|
|
592
|
+
"en-US": "The path of the private key, as ~/.ssh/id_ed25519",
|
|
593
|
+
"ja-JP": "秘密鍵ファイルのパス(例:~/.ssh/id_ed25519)"
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
SecretsServerTexts.keyPlaceholder = keyPlaceholder;
|
|
597
|
+
function askPlaceholder() {
|
|
598
|
+
return TaLanguageUtil.say({
|
|
599
|
+
"zh-CN": "只存进保险箱,不进对话",
|
|
600
|
+
"zh-TW": "只存進保險箱,不進對話",
|
|
601
|
+
"en-US": "Goes into the vault, not the conversation",
|
|
602
|
+
"ja-JP": "金庫にだけ保管され、会話には入りません"
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
SecretsServerTexts.askPlaceholder = askPlaceholder;
|
|
606
|
+
})(SecretsServerTexts ||= {});
|
|
607
|
+
|
|
608
|
+
// web-src/extension.ts
|
|
609
|
+
var SHELLS = { bash: "command" };
|
|
610
|
+
function activate(pi) {
|
|
611
|
+
let reached;
|
|
612
|
+
const vault = () => reached ??= TaHostUtil.reach(pi.events, NAME).then((found) => found?.service?.vault);
|
|
613
|
+
pi.on("session_start", () => {
|
|
614
|
+
vault();
|
|
615
|
+
});
|
|
616
|
+
pi.on("before_agent_start", async (event) => {
|
|
617
|
+
const found = await vault();
|
|
618
|
+
if (found) {
|
|
619
|
+
event.systemPromptOptions.sections = { ...event.systemPromptOptions.sections, [SECTION]: await found.prompt() };
|
|
620
|
+
}
|
|
621
|
+
});
|
|
622
|
+
pi.on("input", async (event, ctx) => {
|
|
623
|
+
const found = await vault();
|
|
624
|
+
if (!found || !event.text) {
|
|
625
|
+
return { action: "continue" };
|
|
626
|
+
}
|
|
627
|
+
const { text, names } = await found.capture(event.text);
|
|
628
|
+
if (names.length) {
|
|
629
|
+
ctx.ui.notify(SecretsServerTexts.captured(names), "info");
|
|
630
|
+
}
|
|
631
|
+
return text === event.text ? { action: "continue" } : { action: "transform", text, images: event.images };
|
|
632
|
+
});
|
|
633
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
634
|
+
const field = SHELLS[event.toolName];
|
|
635
|
+
if (!field) {
|
|
636
|
+
if (JSON.stringify(event.input ?? {}).includes("TA_SECRET_")) {
|
|
637
|
+
await (await vault())?.fill(event.input, event.toolName, ctx.sessionManager?.getSessionName?.());
|
|
638
|
+
}
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const command = event.input[field];
|
|
642
|
+
if (typeof command !== "string" || !command.includes("TA_SECRET_") && !CommandPrelude.usesSsh(command)) {
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
const found = await vault();
|
|
646
|
+
const prepared = await found?.prepare(command, { cwd: ctx.cwd, conversation: ctx.sessionManager?.getSessionName?.() });
|
|
647
|
+
if (prepared?.block) {
|
|
648
|
+
return { block: true, reason: prepared.block };
|
|
649
|
+
}
|
|
650
|
+
if (prepared?.prelude) {
|
|
651
|
+
event.input[field] = prepared.prelude + command;
|
|
652
|
+
}
|
|
653
|
+
return;
|
|
654
|
+
});
|
|
655
|
+
pi.on("tool_result", async (event) => {
|
|
656
|
+
const found = await vault();
|
|
657
|
+
const scrubber = await found?.scrubber();
|
|
658
|
+
if (!scrubber || scrubber.empty) {
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const content = scrubber.value(event.content);
|
|
662
|
+
const details = scrubber.value(event.details);
|
|
663
|
+
const structuredContent = scrubber.value(event.structuredContent);
|
|
664
|
+
if (content === event.content && details === event.details && structuredContent === event.structuredContent) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
return { content, details, ...event.structuredContent !== undefined ? { structuredContent } : {} };
|
|
668
|
+
});
|
|
669
|
+
pi.on("context", async (event) => {
|
|
670
|
+
const found = await vault();
|
|
671
|
+
const scrubber = await found?.scrubber();
|
|
672
|
+
if (!scrubber || scrubber.empty) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
const messages = scrubber.value(event.messages);
|
|
676
|
+
return messages === event.messages ? undefined : { messages };
|
|
677
|
+
});
|
|
678
|
+
pi.registerTool({
|
|
679
|
+
name: REQUEST_TOOL,
|
|
680
|
+
label: "Ask for a secret",
|
|
681
|
+
description: "Ask the user for a secret the vault lacks (a token, a password, a key, a personal detail such as a phone number): they type it into Ta, it is kept in the vault, and you get its name, $TA_SECRET_<NAME>, to use in bash commands. You never see the value. Use it instead of asking for a secret in the chat.",
|
|
682
|
+
promptSnippet: "Ask the user for a secret, kept in the vault, used by its name",
|
|
683
|
+
parameters: {
|
|
684
|
+
type: "object",
|
|
685
|
+
properties: {
|
|
686
|
+
name: { type: "string", description: "Its name: upper case, digits and _, as GITHUB_TOKEN or DEPLOY_SSH_KEY." },
|
|
687
|
+
purpose: { type: "string", description: 'What it is for, in a short line the user reads: "to push to github.com/acme/app".' },
|
|
688
|
+
kind: {
|
|
689
|
+
type: "string",
|
|
690
|
+
enum: ["credential", "personal", "ssh_key"],
|
|
691
|
+
description: "credential (default): a token, a password, a key; personal: a detail of the user; ssh_key: a private key of SSH, which the user gives as the path of its file."
|
|
692
|
+
},
|
|
693
|
+
hosts: {
|
|
694
|
+
type: "string",
|
|
695
|
+
description: 'The hosts it may go to, as "api.example.com" or "github.com, api.github.com": it then reaches only them (through the proxy of the vault; git over https gets it there by itself). Give them whenever the secret is for a service of the network.'
|
|
696
|
+
}
|
|
697
|
+
},
|
|
698
|
+
required: ["name", "purpose"]
|
|
699
|
+
},
|
|
700
|
+
execute: async (_id, params, _signal, _update, ctx) => {
|
|
701
|
+
const found = await vault();
|
|
702
|
+
if (!found) {
|
|
703
|
+
throw new Error("The vault needs the Ta service.");
|
|
704
|
+
}
|
|
705
|
+
if (!ctx.hasUI) {
|
|
706
|
+
throw new Error("There is no one to ask here: the user adds secrets in Ta, Settings, Vault.");
|
|
707
|
+
}
|
|
708
|
+
const name = SecretNames.normalize(String(params.name ?? ""));
|
|
709
|
+
if (!SecretNames.valid(name)) {
|
|
710
|
+
throw new Error("Give a name of letters, digits and _, as GITHUB_TOKEN.");
|
|
711
|
+
}
|
|
712
|
+
const purpose = String(params.purpose ?? "").trim().slice(0, 200);
|
|
713
|
+
const key = params.kind === "ssh_key";
|
|
714
|
+
const hosts = key ? [] : SecretNames.hosts(params.hosts);
|
|
715
|
+
const value = await ctx.ui.input(SecretsServerTexts.ask(name, purpose, hosts), key ? SecretsServerTexts.keyPlaceholder() : SecretsServerTexts.askPlaceholder()).catch(() => {
|
|
716
|
+
return;
|
|
717
|
+
});
|
|
718
|
+
if (!value?.trim()) {
|
|
719
|
+
throw new Error(`The user did not give ${name}.`);
|
|
720
|
+
}
|
|
721
|
+
if (key) {
|
|
722
|
+
await found.importKey(name, value, purpose);
|
|
723
|
+
return {
|
|
724
|
+
content: [
|
|
725
|
+
{
|
|
726
|
+
type: "text",
|
|
727
|
+
text: `Kept in the vault as the SSH key ${name}: ssh, scp, rsync and git over SSH get it by themselves. Do not name it.`
|
|
728
|
+
}
|
|
729
|
+
],
|
|
730
|
+
details: { name }
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
await found.add({ name, value: value.trim(), kind: params.kind === "personal" ? "personal" : "credential", note: purpose, source: "asked", hosts });
|
|
734
|
+
const handle = SecretNames.handle(name);
|
|
735
|
+
const how = hosts.length ? ` It reaches only ${hosts.join(", ")}: use "${handle}" in a header or a query of a request to them (through Ta's proxy); git over https gets it there by itself.` : ` Use it in bash commands as "${handle}".`;
|
|
736
|
+
return { content: [{ type: "text", text: `Kept in the vault as ${handle}.${how}` }], details: { name } };
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
export {
|
|
741
|
+
activate as default
|
|
742
|
+
};
|