@atomicmail/langchain 0.3.14
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 +21 -0
- package/README.md +77 -0
- package/esm/_dnt.polyfills.d.ts +101 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +127 -0
- package/esm/langchain/mod.d.ts +23 -0
- package/esm/langchain/mod.d.ts.map +1 -0
- package/esm/langchain/mod.js +157 -0
- package/esm/lib/agent/auth/agent-auth-http.d.ts +26 -0
- package/esm/lib/agent/auth/agent-auth-http.d.ts.map +1 -0
- package/esm/lib/agent/auth/agent-auth-http.js +85 -0
- package/esm/lib/agent/auth/agent-jwt.d.ts +12 -0
- package/esm/lib/agent/auth/agent-jwt.d.ts.map +1 -0
- package/esm/lib/agent/auth/agent-jwt.js +27 -0
- package/esm/lib/agent/auth/agent-pow.d.ts +5 -0
- package/esm/lib/agent/auth/agent-pow.d.ts.map +1 -0
- package/esm/lib/agent/auth/agent-pow.js +49 -0
- package/esm/lib/agent/jmap/agent-help-content.d.ts +2 -0
- package/esm/lib/agent/jmap/agent-help-content.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-help-content.js +2 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-limits.d.ts +27 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-limits.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-limits.js +166 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-upload.d.ts +24 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-upload.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-jmap-blob-upload.js +104 -0
- package/esm/lib/agent/jmap/agent-jmap-email-charset.d.ts +8 -0
- package/esm/lib/agent/jmap/agent-jmap-email-charset.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-jmap-email-charset.js +61 -0
- package/esm/lib/agent/jmap/agent-jmap-verify.d.ts +9 -0
- package/esm/lib/agent/jmap/agent-jmap-verify.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-jmap-verify.js +50 -0
- package/esm/lib/agent/jmap/agent-jmap.d.ts +89 -0
- package/esm/lib/agent/jmap/agent-jmap.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-jmap.js +373 -0
- package/esm/lib/agent/jmap/agent-vars.d.ts +30 -0
- package/esm/lib/agent/jmap/agent-vars.d.ts.map +1 -0
- package/esm/lib/agent/jmap/agent-vars.js +96 -0
- package/esm/lib/agent/jmap/help-content/auth.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/auth.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/auth.js +33 -0
- package/esm/lib/agent/jmap/help-content/cron.d.ts +6 -0
- package/esm/lib/agent/jmap/help-content/cron.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/cron.js +159 -0
- package/esm/lib/agent/jmap/help-content/index.d.ts +6 -0
- package/esm/lib/agent/jmap/help-content/index.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/index.js +61 -0
- package/esm/lib/agent/jmap/help-content/installation.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/installation.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/installation.js +47 -0
- package/esm/lib/agent/jmap/help-content/jmap-cheatsheet.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/jmap-cheatsheet.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/jmap-cheatsheet.js +230 -0
- package/esm/lib/agent/jmap/help-content/multi-account.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/multi-account.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/multi-account.js +49 -0
- package/esm/lib/agent/jmap/help-content/overview.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/overview.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/overview.js +49 -0
- package/esm/lib/agent/jmap/help-content/presets.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/presets.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/presets.js +51 -0
- package/esm/lib/agent/jmap/help-content/tools.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/tools.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/tools.js +49 -0
- package/esm/lib/agent/jmap/help-content/troubleshooting.d.ts +2 -0
- package/esm/lib/agent/jmap/help-content/troubleshooting.d.ts.map +1 -0
- package/esm/lib/agent/jmap/help-content/troubleshooting.js +65 -0
- package/esm/lib/agent/session/agent-credentials-store.d.ts +45 -0
- package/esm/lib/agent/session/agent-credentials-store.d.ts.map +1 -0
- package/esm/lib/agent/session/agent-credentials-store.js +121 -0
- package/esm/lib/agent/session/agent-resolve-config.d.ts +29 -0
- package/esm/lib/agent/session/agent-resolve-config.d.ts.map +1 -0
- package/esm/lib/agent/session/agent-resolve-config.js +71 -0
- package/esm/lib/agent/session/agent-session-for-dir.d.ts +8 -0
- package/esm/lib/agent/session/agent-session-for-dir.d.ts.map +1 -0
- package/esm/lib/agent/session/agent-session-for-dir.js +33 -0
- package/esm/lib/agent/session/agent-session.d.ts +89 -0
- package/esm/lib/agent/session/agent-session.d.ts.map +1 -0
- package/esm/lib/agent/session/agent-session.js +320 -0
- package/esm/lib/agent/session/inbox-id-to-mailbox-email.d.ts +6 -0
- package/esm/lib/agent/session/inbox-id-to-mailbox-email.d.ts.map +1 -0
- package/esm/lib/agent/session/inbox-id-to-mailbox-email.js +24 -0
- package/esm/lib/core/consts.d.ts +17 -0
- package/esm/lib/core/consts.d.ts.map +1 -0
- package/esm/lib/core/consts.js +28 -0
- package/esm/lib/core/messages.d.ts +6 -0
- package/esm/lib/core/messages.d.ts.map +1 -0
- package/esm/lib/core/messages.js +19 -0
- package/esm/lib/core/read-npm-package-readme.d.ts +6 -0
- package/esm/lib/core/read-npm-package-readme.d.ts.map +1 -0
- package/esm/lib/core/read-npm-package-readme.js +81 -0
- package/esm/lib/core/shared-assets.d.ts +6 -0
- package/esm/lib/core/shared-assets.d.ts.map +1 -0
- package/esm/lib/core/shared-assets.js +46 -0
- package/esm/lib/core/types.d.ts +2 -0
- package/esm/lib/core/types.d.ts.map +1 -0
- package/esm/lib/core/types.js +2 -0
- package/esm/lib/core/utils.d.ts +12 -0
- package/esm/lib/core/utils.d.ts.map +1 -0
- package/esm/lib/core/utils.js +29 -0
- package/esm/lib/mod.d.ts +20 -0
- package/esm/lib/mod.d.ts.map +1 -0
- package/esm/lib/mod.js +19 -0
- package/esm/lib/network/auth-client.d.ts +57 -0
- package/esm/lib/network/auth-client.d.ts.map +1 -0
- package/esm/lib/network/auth-client.js +210 -0
- package/esm/package.json +3 -0
- package/package.json +51 -0
- package/presets/list_inbox.json +46 -0
- package/presets/reply.json +97 -0
- package/presets/send_mail.json +70 -0
- package/presets/send_mail_attachment.json +92 -0
- package/presets/send_mail_blob_attachment.json +74 -0
- package/shared/consts.json +11 -0
- package/shared/fixtures/pow_vectors.json +32 -0
- package/shared/help/fragments/inbox_cron_agent_prompt.md +1 -0
- package/shared/help/fragments/post_register_cron_reminder.md +5 -0
- package/shared/help/readme_stub.md +3 -0
- package/shared/help/topics/auth.md +8 -0
- package/shared/help/topics/cron.md +217 -0
- package/shared/help/topics/installation.md +35 -0
- package/shared/help/topics/jmap_cheatsheet.md +19 -0
- package/shared/help/topics/multi_account.md +9 -0
- package/shared/help/topics/overview.md +27 -0
- package/shared/help/topics/presets.md +12 -0
- package/shared/help/topics/tools.md +16 -0
- package/shared/help/topics/troubleshooting.md +6 -0
- package/shared/manifest.json +31 -0
- package/shared/messages/errors.json +68 -0
- package/shared/messages/hints.json +8 -0
- package/shared/presets/list_inbox.json +46 -0
- package/shared/presets/reply.json +97 -0
- package/shared/presets/send_mail.json +70 -0
- package/shared/presets/send_mail_attachment.json +92 -0
- package/shared/presets/send_mail_blob_attachment.json +74 -0
- package/shared/skill/SKILL.template.md +202 -0
- package/shared/skill/manifest.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Atomic Mail
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: Use @atomicmail/langchain to run Atomic Mail register, jmap_request, and help as LangChain tools.
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# @atomicmail/langchain
|
|
6
|
+
|
|
7
|
+
`@atomicmail/langchain` exposes Atomic Mail as LangChain tools while reusing the
|
|
8
|
+
same shared runtime used by MCP and AgentSkill. It provides both:
|
|
9
|
+
|
|
10
|
+
- a ready-to-use tools array (`createAtomicMailTools`)
|
|
11
|
+
- a toolkit class (`AtomicMailToolkit`)
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @atomicmail/langchain
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Tool surfaces
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createAtomicMailTools, AtomicMailToolkit } from "@atomicmail/langchain";
|
|
23
|
+
|
|
24
|
+
const tools = await createAtomicMailTools();
|
|
25
|
+
|
|
26
|
+
const toolkit = await AtomicMailToolkit.create();
|
|
27
|
+
const registerTool = toolkit.registerTool;
|
|
28
|
+
const jmapTool = toolkit.jmapRequestTool;
|
|
29
|
+
const helpTool = toolkit.helpTool;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Available tools
|
|
33
|
+
|
|
34
|
+
| Tool | Purpose |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `register` | PoW signup / idempotent register with optional `forced` and `credentials_dir`. |
|
|
37
|
+
| `jmap_request` | Run JMAP request from `ops` or `ops_file` with vars and optional attachments. |
|
|
38
|
+
| `help` | Return built-in docs topics bundled with the package. |
|
|
39
|
+
|
|
40
|
+
## Behavior parity guarantees
|
|
41
|
+
|
|
42
|
+
The LangChain wrapper enforces the same core behavior as MCP and AgentSkill:
|
|
43
|
+
|
|
44
|
+
- register idempotency and `forced` semantics are delegated to shared `AgentSession.register`
|
|
45
|
+
- exactly one of `ops` or `ops_file` is required for `jmap_request`
|
|
46
|
+
- `dry_run` with attachments is rejected
|
|
47
|
+
- user vars are validated with `^[A-Z][A-Z0-9_]*$`
|
|
48
|
+
- post-register flow includes cron guidance (`help` topic `cron`)
|
|
49
|
+
|
|
50
|
+
## Credentials and environment
|
|
51
|
+
|
|
52
|
+
Defaults match the rest of the stack:
|
|
53
|
+
|
|
54
|
+
- credential directory: `ATOMIC_MAIL_CREDENTIALS_DIR` or `~/.atomicmail`
|
|
55
|
+
- auth API: `ATOMIC_MAIL_AUTH_URL`
|
|
56
|
+
- JMAP API: `ATOMIC_MAIL_API_URL`
|
|
57
|
+
- PoW salt: `ATOMIC_MAIL_SCRYPT_SALT`
|
|
58
|
+
- API key override: `ATOMIC_MAIL_API_KEY`
|
|
59
|
+
|
|
60
|
+
`credentials_dir` can be passed per tool call for multi-account use.
|
|
61
|
+
|
|
62
|
+
## Example
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { createAtomicMailTools } from "@atomicmail/langchain";
|
|
66
|
+
|
|
67
|
+
const [register, jmapRequest, help] = await createAtomicMailTools();
|
|
68
|
+
|
|
69
|
+
await register.invoke({ username: "myagent" });
|
|
70
|
+
|
|
71
|
+
const inbox = await jmapRequest.invoke({
|
|
72
|
+
ops_file: "list_inbox.json",
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const docs = await help.invoke({ topic: "presets" });
|
|
76
|
+
console.log(inbox, docs);
|
|
77
|
+
```
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on [import-meta-ponyfill](https://github.com/gaubee/import-meta-ponyfill),
|
|
3
|
+
* but instead of using npm to install additional dependencies,
|
|
4
|
+
* this approach manually consolidates cjs/mjs/d.ts into a single file.
|
|
5
|
+
*
|
|
6
|
+
* Note that this code might be imported multiple times
|
|
7
|
+
* (for example, both dnt.test.polyfills.ts and dnt.polyfills.ts contain this code;
|
|
8
|
+
* or Node.js might dynamically clear the cache and then force a require).
|
|
9
|
+
* Therefore, it's important to avoid redundant writes to global objects.
|
|
10
|
+
* Additionally, consider that commonjs is used alongside esm,
|
|
11
|
+
* so the two ponyfill functions are stored independently in two separate global objects.
|
|
12
|
+
*/
|
|
13
|
+
import { createRequire } from "node:module";
|
|
14
|
+
import { type URL } from "node:url";
|
|
15
|
+
declare global {
|
|
16
|
+
interface ImportMeta {
|
|
17
|
+
/** A string representation of the fully qualified module URL. When the
|
|
18
|
+
* module is loaded locally, the value will be a file URL (e.g.
|
|
19
|
+
* `file:///path/module.ts`).
|
|
20
|
+
*
|
|
21
|
+
* You can also parse the string as a URL to determine more information about
|
|
22
|
+
* how the current module was loaded. For example to determine if a module was
|
|
23
|
+
* local or not:
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* const url = new URL(import.meta.url);
|
|
27
|
+
* if (url.protocol === "file:") {
|
|
28
|
+
* console.log("this module was loaded locally");
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
url: string;
|
|
33
|
+
/**
|
|
34
|
+
* A function that returns resolved specifier as if it would be imported
|
|
35
|
+
* using `import(specifier)`.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* console.log(import.meta.resolve("./foo.js"));
|
|
39
|
+
* // file:///dev/foo.js
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @param specifier The module specifier to resolve relative to `parent`.
|
|
43
|
+
* @param parent The absolute parent module URL to resolve from.
|
|
44
|
+
* @returns The absolute (`file:`) URL string for the resolved module.
|
|
45
|
+
*/
|
|
46
|
+
resolve(specifier: string, parent?: string | URL | undefined): string;
|
|
47
|
+
/** A flag that indicates if the current module is the main module that was
|
|
48
|
+
* called when starting the program under Deno.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* if (import.meta.main) {
|
|
52
|
+
* // this was loaded as the main module, maybe do some bootstrapping
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
main: boolean;
|
|
57
|
+
/** The absolute path of the current module.
|
|
58
|
+
*
|
|
59
|
+
* This property is only provided for local modules (ie. using `file://` URLs).
|
|
60
|
+
*
|
|
61
|
+
* Example:
|
|
62
|
+
* ```
|
|
63
|
+
* // Unix
|
|
64
|
+
* console.log(import.meta.filename); // /home/alice/my_module.ts
|
|
65
|
+
*
|
|
66
|
+
* // Windows
|
|
67
|
+
* console.log(import.meta.filename); // C:\alice\my_module.ts
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
filename: string;
|
|
71
|
+
/** The absolute path of the directory containing the current module.
|
|
72
|
+
*
|
|
73
|
+
* This property is only provided for local modules (ie. using `file://` URLs).
|
|
74
|
+
*
|
|
75
|
+
* * Example:
|
|
76
|
+
* ```
|
|
77
|
+
* // Unix
|
|
78
|
+
* console.log(import.meta.dirname); // /home/alice
|
|
79
|
+
*
|
|
80
|
+
* // Windows
|
|
81
|
+
* console.log(import.meta.dirname); // C:\alice
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
dirname: string;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
type NodeRequest = ReturnType<typeof createRequire>;
|
|
88
|
+
type NodeModule = NonNullable<NodeRequest["main"]>;
|
|
89
|
+
interface ImportMetaPonyfillCommonjs {
|
|
90
|
+
(require: NodeRequest, module: NodeModule): ImportMeta;
|
|
91
|
+
}
|
|
92
|
+
interface ImportMetaPonyfillEsmodule {
|
|
93
|
+
(importMeta: ImportMeta): ImportMeta;
|
|
94
|
+
}
|
|
95
|
+
interface ImportMetaPonyfill extends ImportMetaPonyfillCommonjs, ImportMetaPonyfillEsmodule {
|
|
96
|
+
}
|
|
97
|
+
export declare let import_meta_ponyfill_commonjs: ImportMetaPonyfillCommonjs;
|
|
98
|
+
export declare let import_meta_ponyfill_esmodule: ImportMetaPonyfillEsmodule;
|
|
99
|
+
export declare let import_meta_ponyfill: ImportMetaPonyfill;
|
|
100
|
+
export {};
|
|
101
|
+
//# sourceMappingURL=_dnt.polyfills.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_dnt.polyfills.d.ts","sourceRoot":"","sources":["../src/_dnt.polyfills.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAgC,KAAK,GAAG,EAAE,MAAM,UAAU,CAAC;AAGlE,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,UAAU;QAClB;;;;;;;;;;;;;;WAcG;QACH,GAAG,EAAE,MAAM,CAAC;QACZ;;;;;;;;;;;;WAYG;QACH,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,GAAG,MAAM,CAAC;QACtE;;;;;;;;WAQG;QACH,IAAI,EAAE,OAAO,CAAC;QAEd;;;;;;;;;;;;WAYG;QACH,QAAQ,EAAE,MAAM,CAAC;QAEjB;;;;;;;;;;;;WAYG;QACH,OAAO,EAAE,MAAM,CAAC;KACjB;CACF;AAED,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC;AACpD,KAAK,UAAU,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;AACnD,UAAU,0BAA0B;IAClC,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,UAAU,CAAC;CACxD;AACD,UAAU,0BAA0B;IAClC,CAAC,UAAU,EAAE,UAAU,GAAG,UAAU,CAAC;CACtC;AACD,UAAU,kBACR,SAAQ,0BAA0B,EAAE,0BAA0B;CAC/D;AAiBD,eAAO,IAAI,6BAA6B,EA2BnC,0BAA0B,CAAC;AAMhC,eAAO,IAAI,6BAA6B,EA4DnC,0BAA0B,CAAC;AAMhC,eAAO,IAAI,oBAAoB,EAoB1B,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on [import-meta-ponyfill](https://github.com/gaubee/import-meta-ponyfill),
|
|
3
|
+
* but instead of using npm to install additional dependencies,
|
|
4
|
+
* this approach manually consolidates cjs/mjs/d.ts into a single file.
|
|
5
|
+
*
|
|
6
|
+
* Note that this code might be imported multiple times
|
|
7
|
+
* (for example, both dnt.test.polyfills.ts and dnt.polyfills.ts contain this code;
|
|
8
|
+
* or Node.js might dynamically clear the cache and then force a require).
|
|
9
|
+
* Therefore, it's important to avoid redundant writes to global objects.
|
|
10
|
+
* Additionally, consider that commonjs is used alongside esm,
|
|
11
|
+
* so the two ponyfill functions are stored independently in two separate global objects.
|
|
12
|
+
*/
|
|
13
|
+
//@ts-ignore
|
|
14
|
+
import { createRequire } from "node:module";
|
|
15
|
+
//@ts-ignore
|
|
16
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
17
|
+
//@ts-ignore
|
|
18
|
+
import { dirname } from "node:path";
|
|
19
|
+
const defineGlobalPonyfill = (symbolFor, fn) => {
|
|
20
|
+
if (!Reflect.has(globalThis, Symbol.for(symbolFor))) {
|
|
21
|
+
Object.defineProperty(globalThis, Symbol.for(symbolFor), {
|
|
22
|
+
configurable: true,
|
|
23
|
+
get() {
|
|
24
|
+
return fn;
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
export let import_meta_ponyfill_commonjs = (Reflect.get(globalThis, Symbol.for("import-meta-ponyfill-commonjs")) ??
|
|
30
|
+
(() => {
|
|
31
|
+
const moduleImportMetaWM = new WeakMap();
|
|
32
|
+
return (require, module) => {
|
|
33
|
+
let importMetaCache = moduleImportMetaWM.get(module);
|
|
34
|
+
if (importMetaCache == null) {
|
|
35
|
+
const importMeta = Object.assign(Object.create(null), {
|
|
36
|
+
url: pathToFileURL(module.filename).href,
|
|
37
|
+
main: require.main == module,
|
|
38
|
+
resolve: (specifier, parentURL = importMeta.url) => {
|
|
39
|
+
return pathToFileURL((importMeta.url === parentURL
|
|
40
|
+
? require
|
|
41
|
+
: createRequire(parentURL))
|
|
42
|
+
.resolve(specifier)).href;
|
|
43
|
+
},
|
|
44
|
+
filename: module.filename,
|
|
45
|
+
dirname: module.path,
|
|
46
|
+
});
|
|
47
|
+
moduleImportMetaWM.set(module, importMeta);
|
|
48
|
+
importMetaCache = importMeta;
|
|
49
|
+
}
|
|
50
|
+
return importMetaCache;
|
|
51
|
+
};
|
|
52
|
+
})());
|
|
53
|
+
defineGlobalPonyfill("import-meta-ponyfill-commonjs", import_meta_ponyfill_commonjs);
|
|
54
|
+
export let import_meta_ponyfill_esmodule = (Reflect.get(globalThis, Symbol.for("import-meta-ponyfill-esmodule")) ??
|
|
55
|
+
((importMeta) => {
|
|
56
|
+
const resolveFunStr = String(importMeta.resolve);
|
|
57
|
+
const shimWs = new WeakSet();
|
|
58
|
+
//@ts-ignore
|
|
59
|
+
const mainUrl = ("file:///" + process.argv[1].replace(/\\/g, "/"))
|
|
60
|
+
.replace(/\/{3,}/, "///");
|
|
61
|
+
const commonShim = (importMeta) => {
|
|
62
|
+
if (typeof importMeta.main !== "boolean") {
|
|
63
|
+
importMeta.main = importMeta.url === mainUrl;
|
|
64
|
+
}
|
|
65
|
+
if (typeof importMeta.filename !== "string") {
|
|
66
|
+
importMeta.filename = fileURLToPath(importMeta.url);
|
|
67
|
+
importMeta.dirname = dirname(importMeta.filename);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
if (
|
|
71
|
+
// v16.2.0+, v14.18.0+: Add support for WHATWG URL object to parentURL parameter.
|
|
72
|
+
resolveFunStr === "undefined" ||
|
|
73
|
+
// v20.0.0+, v18.19.0+"" This API now returns a string synchronously instead of a Promise.
|
|
74
|
+
resolveFunStr.startsWith("async")
|
|
75
|
+
// enable by --experimental-import-meta-resolve flag
|
|
76
|
+
) {
|
|
77
|
+
import_meta_ponyfill_esmodule = (importMeta) => {
|
|
78
|
+
if (!shimWs.has(importMeta)) {
|
|
79
|
+
shimWs.add(importMeta);
|
|
80
|
+
const importMetaUrlRequire = {
|
|
81
|
+
url: importMeta.url,
|
|
82
|
+
require: createRequire(importMeta.url),
|
|
83
|
+
};
|
|
84
|
+
importMeta.resolve = function resolve(specifier, parentURL = importMeta.url) {
|
|
85
|
+
return pathToFileURL((importMetaUrlRequire.url === parentURL
|
|
86
|
+
? importMetaUrlRequire.require
|
|
87
|
+
: createRequire(parentURL)).resolve(specifier)).href;
|
|
88
|
+
};
|
|
89
|
+
commonShim(importMeta);
|
|
90
|
+
}
|
|
91
|
+
return importMeta;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
/// native support
|
|
96
|
+
import_meta_ponyfill_esmodule = (importMeta) => {
|
|
97
|
+
if (!shimWs.has(importMeta)) {
|
|
98
|
+
shimWs.add(importMeta);
|
|
99
|
+
commonShim(importMeta);
|
|
100
|
+
}
|
|
101
|
+
return importMeta;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return import_meta_ponyfill_esmodule(importMeta);
|
|
105
|
+
}));
|
|
106
|
+
defineGlobalPonyfill("import-meta-ponyfill-esmodule", import_meta_ponyfill_esmodule);
|
|
107
|
+
export let import_meta_ponyfill = ((...args) => {
|
|
108
|
+
const _MODULE = (() => {
|
|
109
|
+
if (typeof require === "function" && typeof module === "object") {
|
|
110
|
+
return "commonjs";
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
// eval("typeof import.meta");
|
|
114
|
+
return "esmodule";
|
|
115
|
+
}
|
|
116
|
+
})();
|
|
117
|
+
if (_MODULE === "commonjs") {
|
|
118
|
+
//@ts-ignore
|
|
119
|
+
import_meta_ponyfill = (r, m) => import_meta_ponyfill_commonjs(r, m);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
//@ts-ignore
|
|
123
|
+
import_meta_ponyfill = (im) => import_meta_ponyfill_esmodule(im);
|
|
124
|
+
}
|
|
125
|
+
//@ts-ignore
|
|
126
|
+
return import_meta_ponyfill(...args);
|
|
127
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import "../_dnt.polyfills.js";
|
|
2
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
3
|
+
import { AgentSession, type ResolvedAgentConfig } from "../lib/mod.js";
|
|
4
|
+
export interface AtomicMailToolkitContext {
|
|
5
|
+
defaultConfig: ResolvedAgentConfig;
|
|
6
|
+
defaultSession: AgentSession;
|
|
7
|
+
}
|
|
8
|
+
export interface AtomicMailToolkitOptions {
|
|
9
|
+
context?: AtomicMailToolkitContext;
|
|
10
|
+
}
|
|
11
|
+
export declare function buildAtomicMailTools(context: AtomicMailToolkitContext): DynamicStructuredTool[];
|
|
12
|
+
export declare function createAtomicMailTools(options?: AtomicMailToolkitOptions): Promise<DynamicStructuredTool[]>;
|
|
13
|
+
export declare class AtomicMailToolkit {
|
|
14
|
+
readonly tools: DynamicStructuredTool[];
|
|
15
|
+
private readonly context;
|
|
16
|
+
private constructor();
|
|
17
|
+
static create(options?: AtomicMailToolkitOptions): Promise<AtomicMailToolkit>;
|
|
18
|
+
get registerTool(): DynamicStructuredTool;
|
|
19
|
+
get jmapRequestTool(): DynamicStructuredTool;
|
|
20
|
+
get helpTool(): DynamicStructuredTool;
|
|
21
|
+
dispose(): void;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sourceRoot":"","sources":["../../src/langchain/mod.ts"],"names":[],"mappings":"AACA,OAAO,sBAAsB,CAAC;AAG9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAG9D,OAAO,EACL,YAAY,EAOZ,KAAK,mBAAmB,EAIzB,MAAM,eAAe,CAAC;AAGvB,MAAM,WAAW,wBAAwB;IACvC,aAAa,EAAE,mBAAmB,CAAC;IACnC,cAAc,EAAE,YAAY,CAAC;CAC9B;AAED,MAAM,WAAW,wBAAwB;IACvC,OAAO,CAAC,EAAE,wBAAwB,CAAC;CACpC;AAiKD,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,wBAAwB,GAChC,qBAAqB,EAAE,CAMzB;AAED,wBAAsB,qBAAqB,CACzC,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,qBAAqB,EAAE,CAAC,CAGlC;AAED,qBAAa,iBAAiB;IAC5B,QAAQ,CAAC,KAAK,EAAE,qBAAqB,EAAE,CAAC;IACxC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IAEnD,OAAO;WAKM,MAAM,CACjB,OAAO,GAAE,wBAA6B,GACrC,OAAO,CAAC,iBAAiB,CAAC;IAK7B,IAAI,YAAY,IAAI,qBAAqB,CAExC;IAED,IAAI,eAAe,IAAI,qBAAqB,CAE3C;IAED,IAAI,QAAQ,IAAI,qBAAqB,CAEpC;IAED,OAAO,IAAI,IAAI;CAGhB"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
// Atomic Mail LangChain toolkit and tool factories.
|
|
2
|
+
import "../_dnt.polyfills.js";
|
|
3
|
+
import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { AgentSession, createAgentSessionForCredentialDir, DEFAULT_JMAP_USING, getHelp, HELP_TOPIC_LIST, readOpsFile, resolveAgentConfigFromEnv, runJmapRequest, sharedError, USER_VAR_KEY_RE, } from "../lib/mod.js";
|
|
6
|
+
import { postRegisterCronReminder } from "../lib/agent/jmap/help-content/cron.js";
|
|
7
|
+
async function createDefaultContext() {
|
|
8
|
+
const defaultConfig = await resolveAgentConfigFromEnv();
|
|
9
|
+
const defaultSession = await AgentSession.create({
|
|
10
|
+
authUrl: defaultConfig.authUrl,
|
|
11
|
+
apiUrl: defaultConfig.apiUrl,
|
|
12
|
+
scryptSalt: defaultConfig.scryptSalt,
|
|
13
|
+
apiKey: defaultConfig.apiKey,
|
|
14
|
+
inboxId: defaultConfig.inboxId,
|
|
15
|
+
credentialDir: defaultConfig.credentialDir,
|
|
16
|
+
files: defaultConfig.files,
|
|
17
|
+
});
|
|
18
|
+
return { defaultConfig, defaultSession };
|
|
19
|
+
}
|
|
20
|
+
async function resolveToolkitSession(ctx, credentialsDir, mode) {
|
|
21
|
+
if (!credentialsDir) {
|
|
22
|
+
return ctx.defaultSession;
|
|
23
|
+
}
|
|
24
|
+
return await createAgentSessionForCredentialDir(credentialsDir, {
|
|
25
|
+
authUrl: ctx.defaultConfig.authUrl,
|
|
26
|
+
apiUrl: ctx.defaultConfig.apiUrl,
|
|
27
|
+
scryptSalt: ctx.defaultConfig.scryptSalt,
|
|
28
|
+
}, { requireCredentials: mode === "jmap" });
|
|
29
|
+
}
|
|
30
|
+
function buildRegisterTool(ctx) {
|
|
31
|
+
return new DynamicStructuredTool({
|
|
32
|
+
name: "register",
|
|
33
|
+
description: "PoW signup; writes credentials. Usernames are 5-21 chars. " +
|
|
34
|
+
"Idempotent for same username and stored inbox; different username is " +
|
|
35
|
+
"rejected unless forced=true is provided. After success, arrange hourly " +
|
|
36
|
+
"inbox polling per runtime (help topic cron).",
|
|
37
|
+
schema: z.object({
|
|
38
|
+
username: z.string().min(5).max(21),
|
|
39
|
+
credentials_dir: z.string().optional(),
|
|
40
|
+
forced: z.boolean().optional(),
|
|
41
|
+
}),
|
|
42
|
+
func: async ({ username, credentials_dir, forced }) => {
|
|
43
|
+
const session = await resolveToolkitSession(ctx, credentials_dir, "register");
|
|
44
|
+
const result = await session.register(username, { forced });
|
|
45
|
+
return JSON.stringify({
|
|
46
|
+
...result,
|
|
47
|
+
_next: [postRegisterCronReminder],
|
|
48
|
+
}, null, 2);
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function buildJmapRequestTool(ctx) {
|
|
53
|
+
return new DynamicStructuredTool({
|
|
54
|
+
name: "jmap_request",
|
|
55
|
+
description: "JMAP method-call batch with automatic auth. Exactly one of: " +
|
|
56
|
+
"ops (JSON string) or ops_file (preset path). Supports vars " +
|
|
57
|
+
"placeholder substitution and optional local-file attachments.",
|
|
58
|
+
schema: z.object({
|
|
59
|
+
credentials_dir: z.string().optional(),
|
|
60
|
+
using: z.array(z.string()).default([...DEFAULT_JMAP_USING]),
|
|
61
|
+
ops: z.string().optional(),
|
|
62
|
+
ops_file: z.string().optional(),
|
|
63
|
+
vars: z
|
|
64
|
+
.record(z.string().regex(USER_VAR_KEY_RE), z.string())
|
|
65
|
+
.optional(),
|
|
66
|
+
dry_run: z.boolean().optional(),
|
|
67
|
+
attachments: z
|
|
68
|
+
.array(z.object({
|
|
69
|
+
path: z.string(),
|
|
70
|
+
filename: z.string().optional(),
|
|
71
|
+
content_type: z.string().optional(),
|
|
72
|
+
}))
|
|
73
|
+
.optional(),
|
|
74
|
+
}),
|
|
75
|
+
func: async ({ credentials_dir, using, ops, ops_file, vars, dry_run, attachments, }) => {
|
|
76
|
+
if (ops && ops_file) {
|
|
77
|
+
throw new Error(sharedError("mcp_ops_mutually_exclusive"));
|
|
78
|
+
}
|
|
79
|
+
if (!ops && !ops_file) {
|
|
80
|
+
throw new Error(sharedError("mcp_ops_required"));
|
|
81
|
+
}
|
|
82
|
+
if (dry_run === true && attachments && attachments.length > 0) {
|
|
83
|
+
throw new Error(sharedError("cli_dry_run_with_attachment"));
|
|
84
|
+
}
|
|
85
|
+
const session = await resolveToolkitSession(ctx, credentials_dir, "jmap");
|
|
86
|
+
const rawOps = ops_file
|
|
87
|
+
? await readOpsFile(session.credentialDir, ops_file)
|
|
88
|
+
: ops;
|
|
89
|
+
const sourceLabel = ops_file ? `ops_file '${ops_file}'` : "ops";
|
|
90
|
+
const { ok, status, bodyText } = await runJmapRequest({
|
|
91
|
+
session,
|
|
92
|
+
opsJson: rawOps,
|
|
93
|
+
defaultUsing: using,
|
|
94
|
+
sourceLabel,
|
|
95
|
+
vars,
|
|
96
|
+
dryRun: dry_run,
|
|
97
|
+
attachments: attachments?.map((item) => ({
|
|
98
|
+
path: item.path,
|
|
99
|
+
filename: item.filename,
|
|
100
|
+
contentType: item.content_type,
|
|
101
|
+
})),
|
|
102
|
+
});
|
|
103
|
+
if (!ok) {
|
|
104
|
+
throw new Error(`JMAP request failed (HTTP ${status}): ${bodyText}`);
|
|
105
|
+
}
|
|
106
|
+
return bodyText;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function buildHelpTool() {
|
|
111
|
+
return new DynamicStructuredTool({
|
|
112
|
+
name: "help",
|
|
113
|
+
description: "Built-in Atomic Mail docs. Call early and often. Topics: " +
|
|
114
|
+
`${HELP_TOPIC_LIST.join(", ")}, readme.`,
|
|
115
|
+
schema: z.object({
|
|
116
|
+
topic: z.string().optional(),
|
|
117
|
+
}),
|
|
118
|
+
func: async ({ topic }) => {
|
|
119
|
+
return await getHelp(topic, "skill");
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
export function buildAtomicMailTools(context) {
|
|
124
|
+
return [
|
|
125
|
+
buildRegisterTool(context),
|
|
126
|
+
buildJmapRequestTool(context),
|
|
127
|
+
buildHelpTool(),
|
|
128
|
+
];
|
|
129
|
+
}
|
|
130
|
+
export async function createAtomicMailTools(options = {}) {
|
|
131
|
+
const toolkit = await AtomicMailToolkit.create(options);
|
|
132
|
+
return toolkit.tools;
|
|
133
|
+
}
|
|
134
|
+
export class AtomicMailToolkit {
|
|
135
|
+
tools;
|
|
136
|
+
context;
|
|
137
|
+
constructor(context) {
|
|
138
|
+
this.context = context;
|
|
139
|
+
this.tools = buildAtomicMailTools(context);
|
|
140
|
+
}
|
|
141
|
+
static async create(options = {}) {
|
|
142
|
+
const context = options.context ?? (await createDefaultContext());
|
|
143
|
+
return new AtomicMailToolkit(context);
|
|
144
|
+
}
|
|
145
|
+
get registerTool() {
|
|
146
|
+
return this.tools[0];
|
|
147
|
+
}
|
|
148
|
+
get jmapRequestTool() {
|
|
149
|
+
return this.tools[1];
|
|
150
|
+
}
|
|
151
|
+
get helpTool() {
|
|
152
|
+
return this.tools[2];
|
|
153
|
+
}
|
|
154
|
+
dispose() {
|
|
155
|
+
this.context.defaultSession.destroy();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare function fetchChallenge(authUrl: string): Promise<{
|
|
2
|
+
challengeJWT: string;
|
|
3
|
+
challenge: string;
|
|
4
|
+
difficulty: number;
|
|
5
|
+
}>;
|
|
6
|
+
export interface SessionResponse {
|
|
7
|
+
sessionJWT: string;
|
|
8
|
+
apiKey?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function exchangeSession(authUrl: string, body: {
|
|
11
|
+
challengeJWT: string;
|
|
12
|
+
powHex: string;
|
|
13
|
+
nonce: string;
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
username?: string;
|
|
16
|
+
}): Promise<SessionResponse>;
|
|
17
|
+
export declare function fetchCapability(authUrl: string, sessionJWT: string): Promise<string>;
|
|
18
|
+
export interface PerformPoWInput {
|
|
19
|
+
authUrl: string;
|
|
20
|
+
scryptSalt: string;
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
username?: string;
|
|
23
|
+
onPowProgress?: (nonce: bigint) => void;
|
|
24
|
+
}
|
|
25
|
+
export declare function performPoWAndSession(input: PerformPoWInput): Promise<SessionResponse>;
|
|
26
|
+
//# sourceMappingURL=agent-auth-http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-auth-http.d.ts","sourceRoot":"","sources":["../../../../src/lib/agent/auth/agent-auth-http.ts"],"names":[],"mappings":"AAKA,wBAAsB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC,CA8BD;AAED,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,wBAAsB,eAAe,CACnC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE;IACJ,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,GACA,OAAO,CAAC,eAAe,CAAC,CA8B1B;AAED,wBAAsB,eAAe,CACnC,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,CAejB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAED,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,eAAe,GACrB,OAAO,CAAC,eAAe,CAAC,CAgB1B"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// auth-service HTTP: challenge → session → capability.
|
|
2
|
+
import { decodeJwtPayload } from "./agent-jwt.js";
|
|
3
|
+
import { solvePow } from "./agent-pow.js";
|
|
4
|
+
export async function fetchChallenge(authUrl) {
|
|
5
|
+
const res = await fetch(`${authUrl}/api/v1/challenge`, {
|
|
6
|
+
method: "POST",
|
|
7
|
+
});
|
|
8
|
+
const text = await res.text();
|
|
9
|
+
if (!res.ok) {
|
|
10
|
+
throw new Error(`auth-service /api/v1/challenge returned ${res.status}: ${text}`);
|
|
11
|
+
}
|
|
12
|
+
const challengeJWT = readBearerToken(res.headers.get("Authorization"), "Challenge response missing Authorization bearer token.");
|
|
13
|
+
const payload = decodeJwtPayload(challengeJWT);
|
|
14
|
+
if (typeof payload.jti !== "string" ||
|
|
15
|
+
typeof payload.difficulty !== "number") {
|
|
16
|
+
throw new Error("Challenge JWT payload malformed (missing jti or difficulty).");
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
challengeJWT,
|
|
20
|
+
challenge: payload.jti,
|
|
21
|
+
difficulty: payload.difficulty,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export async function exchangeSession(authUrl, body) {
|
|
25
|
+
const { challengeJWT, ...payload } = body;
|
|
26
|
+
const res = await fetch(`${authUrl}/api/v1/session`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
Authorization: `Bearer ${challengeJWT}`,
|
|
31
|
+
},
|
|
32
|
+
body: JSON.stringify(payload),
|
|
33
|
+
});
|
|
34
|
+
const text = await res.text();
|
|
35
|
+
if (!res.ok) {
|
|
36
|
+
throw new Error(`auth-service /api/v1/session returned ${res.status}: ${text}`);
|
|
37
|
+
}
|
|
38
|
+
const sessionJWT = readBearerToken(res.headers.get("Authorization"), "Session response missing Authorization bearer token.");
|
|
39
|
+
let data = {};
|
|
40
|
+
if (text.trim().length > 0) {
|
|
41
|
+
try {
|
|
42
|
+
data = JSON.parse(text);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
throw new Error("auth-service /api/v1/session returned non-JSON body.");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
sessionJWT,
|
|
50
|
+
apiKey: typeof data.apiKey === "string" ? data.apiKey : undefined,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export async function fetchCapability(authUrl, sessionJWT) {
|
|
54
|
+
const res = await fetch(`${authUrl}/api/v1/capability`, {
|
|
55
|
+
method: "POST",
|
|
56
|
+
headers: { Authorization: `Bearer ${sessionJWT}` },
|
|
57
|
+
});
|
|
58
|
+
const text = await res.text();
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
throw new Error(`auth-service /api/v1/capability returned ${res.status}: ${text}`);
|
|
61
|
+
}
|
|
62
|
+
return readBearerToken(res.headers.get("Authorization"), "Capability response missing Authorization bearer token.");
|
|
63
|
+
}
|
|
64
|
+
export async function performPoWAndSession(input) {
|
|
65
|
+
const { authUrl, scryptSalt } = input;
|
|
66
|
+
const { challengeJWT, challenge, difficulty } = await fetchChallenge(authUrl);
|
|
67
|
+
const { powHex, nonce } = await solvePow(challenge, difficulty, scryptSalt, input.onPowProgress);
|
|
68
|
+
return exchangeSession(authUrl, {
|
|
69
|
+
challengeJWT,
|
|
70
|
+
powHex,
|
|
71
|
+
nonce,
|
|
72
|
+
apiKey: input.apiKey,
|
|
73
|
+
username: input.username,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function readBearerToken(headerValue, missingError) {
|
|
77
|
+
if (!headerValue) {
|
|
78
|
+
throw new Error(missingError);
|
|
79
|
+
}
|
|
80
|
+
const match = /^\s*Bearer\s+(.+?)\s*$/i.exec(headerValue);
|
|
81
|
+
if (!match || !match[1]) {
|
|
82
|
+
throw new Error("Authorization header must use Bearer scheme.");
|
|
83
|
+
}
|
|
84
|
+
return match[1];
|
|
85
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const SESSION_SAFETY_MARGIN_MS = 60000;
|
|
2
|
+
export declare const CAPABILITY_SAFETY_MARGIN_MS = 20000;
|
|
3
|
+
export interface JwtPayload {
|
|
4
|
+
exp?: number;
|
|
5
|
+
iat?: number;
|
|
6
|
+
jti?: string;
|
|
7
|
+
inboxId?: string;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
export declare function decodeJwtPayload<T = JwtPayload>(jwt: string): T;
|
|
11
|
+
export declare function isJwtExpired(jwt: string, marginMs: number): boolean;
|
|
12
|
+
//# sourceMappingURL=agent-jwt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-jwt.d.ts","sourceRoot":"","sources":["../../../../src/lib/agent/auth/agent-jwt.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,wBAAwB,QAAS,CAAC;AAC/C,eAAO,MAAM,2BAA2B,QAAS,CAAC;AAElD,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,wBAAgB,gBAAgB,CAAC,CAAC,GAAG,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,CAc/D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAQnE"}
|