@agentorg/cli 0.0.5 → 0.0.6

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.
Files changed (206) hide show
  1. package/README.md +18 -102
  2. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.d.ts +2 -7
  3. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.js +4 -144
  4. package/dist/node_modules/@agentorg/hub/dist/src/index.d.ts +1 -3
  5. package/dist/node_modules/@agentorg/hub/dist/src/index.js +1 -3
  6. package/dist/node_modules/@agentorg/hub/dist/src/router.d.ts +6 -57
  7. package/dist/node_modules/@agentorg/hub/dist/src/router.js +38 -413
  8. package/dist/node_modules/@agentorg/hub/dist/src/state.d.ts +17 -317
  9. package/dist/node_modules/@agentorg/hub/dist/src/state.js +42 -536
  10. package/dist/node_modules/@agentorg/hub/dist/src/validate.d.ts +1 -3
  11. package/dist/node_modules/@agentorg/hub/dist/src/validate.js +3 -15
  12. package/dist/node_modules/@agentorg/hub/package.json +0 -3
  13. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.d.ts +132 -0
  14. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.js +105 -0
  15. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.d.ts +42 -0
  16. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.js +53 -0
  17. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.d.ts +15 -0
  18. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.js +14 -0
  19. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.d.ts +16 -286
  20. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.js +17 -159
  21. package/dist/node_modules/@agentorg/interfaces/dist/src/index.d.ts +6 -8
  22. package/dist/node_modules/@agentorg/interfaces/dist/src/index.js +6 -8
  23. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.d.ts +2 -2
  24. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.js +2 -2
  25. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.d.ts +272 -0
  26. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.js +185 -0
  27. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.d.ts +88 -0
  28. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.js +78 -0
  29. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.d.ts +39 -0
  30. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.js +43 -0
  31. package/dist/node_modules/@agentorg/manager/dist/src/index.d.ts +1 -0
  32. package/dist/node_modules/@agentorg/manager/dist/src/index.js +1 -0
  33. package/dist/node_modules/@agentorg/manager/dist/src/manager.d.ts +21 -0
  34. package/dist/node_modules/@agentorg/manager/dist/src/manager.js +528 -0
  35. package/dist/node_modules/@agentorg/manager/dist/src/skills.d.ts +25 -0
  36. package/dist/node_modules/@agentorg/manager/dist/src/skills.js +145 -0
  37. package/dist/node_modules/@agentorg/manager/package.json +30 -0
  38. package/dist/node_modules/@agentorg/mcp/dist/src/connection.d.ts +33 -0
  39. package/dist/node_modules/@agentorg/mcp/dist/src/connection.js +290 -0
  40. package/dist/node_modules/@agentorg/mcp/dist/src/index.d.ts +1 -0
  41. package/dist/node_modules/@agentorg/mcp/dist/src/index.js +1 -0
  42. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.d.ts +50 -0
  43. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.js +69 -0
  44. package/dist/node_modules/@agentorg/mcp/dist/src/provider.d.ts +22 -0
  45. package/dist/node_modules/@agentorg/mcp/dist/src/provider.js +167 -0
  46. package/dist/node_modules/@agentorg/mcp/package.json +31 -0
  47. package/dist/node_modules/@agentorg/sdk/dist/src/cli.js +3 -23
  48. package/dist/node_modules/@agentorg/sdk/dist/src/client.d.ts +4 -25
  49. package/dist/node_modules/@agentorg/sdk/dist/src/client.js +5 -55
  50. package/dist/node_modules/@agentorg/sdk/dist/src/errors.d.ts +1 -1
  51. package/dist/node_modules/@agentorg/sdk/dist/src/errors.js +3 -3
  52. package/dist/node_modules/@agentorg/sdk/dist/src/index.d.ts +2 -2
  53. package/dist/node_modules/@agentorg/sdk/dist/src/index.js +1 -1
  54. package/dist/node_modules/@agentorg/sdk/dist/src/interface.d.ts +6 -34
  55. package/dist/node_modules/@agentorg/sdk/dist/src/interface.js +3 -24
  56. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.d.ts +8 -17
  57. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.js +15 -87
  58. package/dist/node_modules/@agentorg/sdk/dist/src/module.d.ts +10 -76
  59. package/dist/node_modules/@agentorg/sdk/dist/src/module.js +3 -23
  60. package/dist/node_modules/@agentorg/sdk/dist/src/person-commands.js +0 -5
  61. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.d.ts +3 -27
  62. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.js +5 -100
  63. package/dist/node_modules/@agentorg/sdk/dist/src/testing.d.ts +7 -8
  64. package/dist/node_modules/@agentorg/sdk/dist/src/testing.js +12 -24
  65. package/dist/node_modules/@agentorg/sdk/package.json +4 -37
  66. package/dist/node_modules/@agentorg/shell/dist/app.css +3435 -739
  67. package/dist/node_modules/@agentorg/shell/dist/app.js +204 -202
  68. package/dist/node_modules/@agentorg/shell/package.json +18 -10
  69. package/dist/node_modules/@agentorg/wire/dist/src/frames.d.ts +2 -3
  70. package/dist/node_modules/@agentorg/wire/dist/src/frames.js +2 -3
  71. package/dist/node_modules/@agentorg/wire/dist/src/ops.d.ts +5 -34
  72. package/dist/node_modules/@agentorg/wire/dist/src/ops.js +2 -17
  73. package/dist/node_modules/assistant/dist/src/agent.d.ts +62 -0
  74. package/dist/node_modules/assistant/dist/src/agent.js +226 -0
  75. package/dist/node_modules/assistant/dist/src/engine.d.ts +23 -0
  76. package/dist/node_modules/assistant/dist/src/engine.js +88 -0
  77. package/dist/node_modules/assistant/dist/src/history.d.ts +97 -0
  78. package/dist/node_modules/assistant/dist/src/history.js +235 -0
  79. package/dist/node_modules/assistant/dist/src/local-store.d.ts +3 -0
  80. package/dist/node_modules/assistant/dist/src/local-store.js +58 -0
  81. package/dist/node_modules/assistant/dist/src/provider.d.ts +21 -0
  82. package/dist/node_modules/assistant/dist/src/provider.js +231 -0
  83. package/dist/node_modules/assistant/dist/src/settings.d.ts +17 -0
  84. package/dist/node_modules/assistant/dist/src/settings.js +43 -0
  85. package/dist/node_modules/assistant/dist/src/tools.d.ts +74 -0
  86. package/dist/node_modules/assistant/dist/src/tools.js +123 -0
  87. package/dist/node_modules/assistant/package.json +29 -0
  88. package/dist/node_modules/chatgpt/dist/src/account.d.ts +52 -0
  89. package/dist/node_modules/chatgpt/dist/src/account.js +270 -0
  90. package/dist/node_modules/chatgpt/dist/src/encoding.d.ts +7 -0
  91. package/dist/node_modules/chatgpt/dist/src/encoding.js +20 -0
  92. package/dist/node_modules/chatgpt/dist/src/file-store.d.ts +9 -0
  93. package/dist/node_modules/chatgpt/dist/src/file-store.js +68 -0
  94. package/dist/node_modules/chatgpt/dist/src/index.d.ts +1 -0
  95. package/dist/node_modules/chatgpt/dist/src/index.js +1 -0
  96. package/dist/node_modules/chatgpt/dist/src/jwt.d.ts +15 -0
  97. package/dist/node_modules/chatgpt/dist/src/jwt.js +37 -0
  98. package/dist/node_modules/chatgpt/dist/src/oauth.d.ts +43 -0
  99. package/dist/node_modules/chatgpt/dist/src/oauth.js +104 -0
  100. package/dist/node_modules/chatgpt/dist/src/program.d.ts +23 -0
  101. package/dist/node_modules/chatgpt/dist/src/program.js +41 -0
  102. package/dist/node_modules/chatgpt/dist/src/responses.d.ts +54 -0
  103. package/dist/node_modules/chatgpt/dist/src/responses.js +300 -0
  104. package/dist/node_modules/chatgpt/dist/src/serve.d.ts +65 -0
  105. package/dist/node_modules/chatgpt/dist/src/serve.js +46 -0
  106. package/dist/node_modules/chatgpt/package.json +33 -0
  107. package/dist/src/api.d.ts +5 -34
  108. package/dist/src/api.js +6 -68
  109. package/dist/src/cli.js +3 -7
  110. package/dist/src/gateway.d.ts +6 -7
  111. package/dist/src/gateway.js +39 -285
  112. package/dist/src/index.d.ts +4 -5
  113. package/dist/src/index.js +3 -4
  114. package/dist/src/notes.d.ts +11 -0
  115. package/dist/src/notes.js +64 -0
  116. package/dist/src/oauth.d.ts +16 -0
  117. package/dist/src/oauth.js +46 -0
  118. package/dist/src/programs.d.ts +29 -0
  119. package/dist/src/programs.js +171 -0
  120. package/package.json +3 -11
  121. package/dist/bundled/index.json +0 -6
  122. package/dist/bundled/sha256-0e91cc3d0e9762b81abc2427b5496b20f383abcd6fb97d928a8d50e956b54d1f.agentorg +0 -0
  123. package/dist/bundled/sha256-1f64ca62a0209d767d6aa6c4feb50b0653d60f10bebbc301f79bd3a37e0153c1.agentorg +0 -0
  124. package/dist/bundled/sha256-4055f8fddf6b90f3d356543f554b2a9e9a2c4a8827a8774f8f87952dc1692331.agentorg +0 -0
  125. package/dist/bundled/sha256-e356ea3c1899138a8494baf521556831006f38534fa9e92ae873e7e6956c3a46.agentorg +0 -0
  126. package/dist/node_modules/@agentorg/hub/dist/src/build.d.ts +0 -17
  127. package/dist/node_modules/@agentorg/hub/dist/src/build.js +0 -120
  128. package/dist/node_modules/@agentorg/hub/dist/src/logs.d.ts +0 -33
  129. package/dist/node_modules/@agentorg/hub/dist/src/logs.js +0 -162
  130. package/dist/node_modules/@agentorg/hub/dist/src/net.d.ts +0 -41
  131. package/dist/node_modules/@agentorg/hub/dist/src/net.js +0 -265
  132. package/dist/node_modules/@agentorg/hub/dist/src/processes.d.ts +0 -78
  133. package/dist/node_modules/@agentorg/hub/dist/src/processes.js +0 -485
  134. package/dist/node_modules/@agentorg/hub/dist/src/storage.d.ts +0 -38
  135. package/dist/node_modules/@agentorg/hub/dist/src/storage.js +0 -194
  136. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.d.ts +0 -6
  137. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.js +0 -7
  138. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.d.ts +0 -191
  139. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.js +0 -136
  140. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.d.ts +0 -47
  141. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.js +0 -52
  142. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.d.ts +0 -35
  143. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.js +0 -37
  144. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.d.ts +0 -23
  145. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.js +0 -31
  146. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.d.ts +0 -14
  147. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.js +0 -26
  148. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.d.ts +0 -27
  149. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.js +0 -26
  150. package/dist/node_modules/@agentorg/kit/dist/src/chat.d.ts +0 -37
  151. package/dist/node_modules/@agentorg/kit/dist/src/chat.js +0 -131
  152. package/dist/node_modules/@agentorg/kit/dist/src/components.d.ts +0 -94
  153. package/dist/node_modules/@agentorg/kit/dist/src/components.js +0 -66
  154. package/dist/node_modules/@agentorg/kit/dist/src/icons.d.ts +0 -17
  155. package/dist/node_modules/@agentorg/kit/dist/src/icons.js +0 -30
  156. package/dist/node_modules/@agentorg/kit/dist/src/index.d.ts +0 -6
  157. package/dist/node_modules/@agentorg/kit/dist/src/index.js +0 -10
  158. package/dist/node_modules/@agentorg/kit/dist/src/markdown.d.ts +0 -9
  159. package/dist/node_modules/@agentorg/kit/dist/src/markdown.js +0 -14
  160. package/dist/node_modules/@agentorg/kit/dist/src/resizable.d.ts +0 -5
  161. package/dist/node_modules/@agentorg/kit/dist/src/resizable.js +0 -8
  162. package/dist/node_modules/@agentorg/kit/dist/src/styles.d.ts +0 -11
  163. package/dist/node_modules/@agentorg/kit/dist/src/styles.js +0 -166
  164. package/dist/node_modules/@agentorg/kit/package.json +0 -44
  165. package/dist/node_modules/@agentorg/sdk/dist/frame/runtime.js +0 -753
  166. package/dist/node_modules/@agentorg/sdk/dist/host/runner.js +0 -21963
  167. package/dist/node_modules/@agentorg/sdk/dist/src/build.d.ts +0 -2
  168. package/dist/node_modules/@agentorg/sdk/dist/src/build.js +0 -2
  169. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.d.ts +0 -24
  170. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.js +0 -247
  171. package/dist/node_modules/@agentorg/sdk/dist/src/describe.d.ts +0 -13
  172. package/dist/node_modules/@agentorg/sdk/dist/src/describe.js +0 -16
  173. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.d.ts +0 -8
  174. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.js +0 -64
  175. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.d.ts +0 -1
  176. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.js +0 -35
  177. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.d.ts +0 -57
  178. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.js +0 -293
  179. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.d.ts +0 -124
  180. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.js +0 -16
  181. package/dist/node_modules/@agentorg/sdk/dist/src/frame.d.ts +0 -25
  182. package/dist/node_modules/@agentorg/sdk/dist/src/frame.js +0 -248
  183. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.d.ts +0 -1
  184. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.js +0 -60
  185. package/dist/node_modules/@agentorg/sdk/dist/src/host.d.ts +0 -25
  186. package/dist/node_modules/@agentorg/sdk/dist/src/host.js +0 -92
  187. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.d.ts +0 -2
  188. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.js +0 -2
  189. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.d.ts +0 -56
  190. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.js +0 -165
  191. package/dist/node_modules/@agentorg/sdk/dist/src/package.d.ts +0 -60
  192. package/dist/node_modules/@agentorg/sdk/dist/src/package.js +0 -538
  193. package/dist/node_modules/@agentorg/sdk/dist/src/tar.d.ts +0 -7
  194. package/dist/node_modules/@agentorg/sdk/dist/src/tar.js +0 -71
  195. package/dist/node_modules/@agentorg/shell/dist/agent-bindings.js +0 -59
  196. package/dist/node_modules/@agentorg/shell/src/agent-bindings.ts +0 -107
  197. package/dist/src/frames.d.ts +0 -23
  198. package/dist/src/frames.js +0 -78
  199. package/dist/src/layout.d.ts +0 -44
  200. package/dist/src/layout.js +0 -90
  201. package/dist/src/model.d.ts +0 -54
  202. package/dist/src/model.js +0 -299
  203. package/dist/src/rules.d.ts +0 -71
  204. package/dist/src/rules.js +0 -157
  205. package/dist/src/transfer.d.ts +0 -30
  206. package/dist/src/transfer.js +0 -110
@@ -0,0 +1,68 @@
1
+ import { chmodSync, closeSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
2
+ import { join } from "node:path";
3
+ /**
4
+ * The sign-in, in one private file, read each time so nothing is held that the file no longer says.
5
+ * Each change is on disk before it is acknowledged, since a refresh token that rotated can't be had
6
+ * again. A file that can't be read is removed, and the person signs in again; nothing of it is ever
7
+ * logged, a parse error's text included.
8
+ */
9
+ export function fileStore(dir, log) {
10
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
11
+ chmodSync(dir, 0o700);
12
+ const path = join(dir, "sign-in.json");
13
+ const read = () => {
14
+ const values = new Map();
15
+ let raw;
16
+ try {
17
+ raw = readFileSync(path, "utf8");
18
+ }
19
+ catch (error) {
20
+ if (error.code === "ENOENT")
21
+ return values;
22
+ throw error;
23
+ }
24
+ try {
25
+ for (const [key, value] of Object.entries(JSON.parse(raw)))
26
+ values.set(key, value);
27
+ }
28
+ catch {
29
+ rmSync(path);
30
+ log.error("The sign-in file couldn't be read, so it was removed: sign in again.");
31
+ }
32
+ return values;
33
+ };
34
+ const save = (values) => {
35
+ const temporary = `${path}.tmp`;
36
+ writeFileSync(temporary, JSON.stringify(Object.fromEntries(values)), {
37
+ mode: 0o600,
38
+ flush: true,
39
+ });
40
+ renameSync(temporary, path);
41
+ const folder = openSync(dir, "r");
42
+ try {
43
+ fsyncSync(folder);
44
+ }
45
+ finally {
46
+ closeSync(folder);
47
+ }
48
+ };
49
+ return {
50
+ get: async ({ key }) => {
51
+ const values = read();
52
+ return values.has(key) ? { value: values.get(key) } : {};
53
+ },
54
+ set: async ({ key, value }) => {
55
+ const values = read();
56
+ values.set(key, value);
57
+ save(values);
58
+ return {};
59
+ },
60
+ delete: async ({ key }) => {
61
+ const values = read();
62
+ const deleted = values.delete(key);
63
+ if (deleted)
64
+ save(values);
65
+ return { deleted };
66
+ },
67
+ };
68
+ }
@@ -0,0 +1 @@
1
+ export { type ChatGPT, type ChatGPTOptions, startChatGPT } from "./program.js";
@@ -0,0 +1 @@
1
+ export { startChatGPT } from "./program.js";
@@ -0,0 +1,15 @@
1
+ import type { webcrypto } from "node:crypto";
2
+ export type Claims = Record<string, unknown>;
3
+ export type Jwk = webcrypto.JsonWebKey & {
4
+ kid?: string;
5
+ };
6
+ /**
7
+ * An id_token's claims, once its RS256 signature checks against one of the issuer's keys and its
8
+ * issuer, audience, expiry and (when given) nonce are right. Anything else throws.
9
+ */
10
+ export declare function verifyIdToken(token: string, keys: Jwk[], expect: {
11
+ issuer: string;
12
+ audience: string;
13
+ nonce?: string;
14
+ now: number;
15
+ }): Promise<Claims>;
@@ -0,0 +1,37 @@
1
+ import { fromBase64url } from "./encoding.js";
2
+ function part(text) {
3
+ if (!text)
4
+ throw new Error("malformed token");
5
+ const value = JSON.parse(new TextDecoder().decode(fromBase64url(text)));
6
+ if (typeof value !== "object" || value === null)
7
+ throw new Error("malformed token");
8
+ return value;
9
+ }
10
+ /**
11
+ * An id_token's claims, once its RS256 signature checks against one of the issuer's keys and its
12
+ * issuer, audience, expiry and (when given) nonce are right. Anything else throws.
13
+ */
14
+ export async function verifyIdToken(token, keys, expect) {
15
+ const [header, payload, signature] = token.split(".");
16
+ const head = part(header);
17
+ if (head.alg !== "RS256")
18
+ throw new Error("unexpected signing algorithm");
19
+ const jwk = keys.find((key) => key.kty === "RSA" && (head.kid === undefined || key.kid === head.kid));
20
+ if (!jwk || signature === undefined)
21
+ throw new Error("no matching signing key");
22
+ const key = await crypto.subtle.importKey("jwk", { kty: jwk.kty, n: jwk.n, e: jwk.e, alg: "RS256", ext: true }, { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, false, ["verify"]);
23
+ const signed = new TextEncoder().encode(`${header}.${payload}`);
24
+ if (!(await crypto.subtle.verify("RSASSA-PKCS1-v1_5", key, fromBase64url(signature), signed)))
25
+ throw new Error("bad signature");
26
+ const claims = part(payload);
27
+ const audience = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
28
+ if (claims.iss !== expect.issuer)
29
+ throw new Error("unexpected issuer");
30
+ if (!audience.includes(expect.audience))
31
+ throw new Error("unexpected audience");
32
+ if (typeof claims.exp !== "number" || claims.exp * 1000 < expect.now - 60_000)
33
+ throw new Error("expired");
34
+ if (expect.nonce !== undefined && claims.nonce !== expect.nonce)
35
+ throw new Error("unexpected nonce");
36
+ return claims;
37
+ }
@@ -0,0 +1,43 @@
1
+ /** OpenAI's Sign in with ChatGPT endpoints and grants, for plan usage by an open-source app. */
2
+ import type { Jwk } from "./jwt.js";
3
+ export declare const ISSUER = "https://auth.openai.com";
4
+ export declare const AUTHORIZE = "https://auth.openai.com/api/accounts/authorize";
5
+ export declare const TOKEN = "https://auth.openai.com/api/accounts/oauth/token";
6
+ /** The audience of the tokens: the Responses API. */
7
+ export declare const RESOURCE = "https://api.openai.com/v1";
8
+ /** The scope that lets the tokens draw on the person's ChatGPT plan. */
9
+ export declare const PLAN_SCOPE = "chatgpt.tokens.use.direct";
10
+ export declare const SCOPES = "openid profile email offline_access resource.invoke chatgpt.tokens.use.direct";
11
+ /** The client id of a first sign-in; OpenAI then issues this app's own `oaiapp_…` id. */
12
+ export declare const DYNAMIC_CLIENT = "dynamic_agent_client";
13
+ export declare const AGENT_NAME = "AgentOrg";
14
+ export type Fetch = (request: Request) => Promise<Response>;
15
+ /** What a grant gives. `expiresAt` is in milliseconds. */
16
+ export interface Grant {
17
+ accessToken: string;
18
+ refreshToken: string;
19
+ idToken?: string;
20
+ expiresAt: number;
21
+ scopes: string[];
22
+ }
23
+ /** A word from a server, fit to show: at most 64 characters of letters, digits and `_.-[]`. */
24
+ export declare const word: (value: unknown) => string;
25
+ /** The token endpoint refused a grant, with OAuth's error code. */
26
+ export declare class GrantError extends Error {
27
+ readonly code: string;
28
+ readonly status: number;
29
+ constructor(code: string, status: number);
30
+ }
31
+ /** Refresh errors after which the refresh token can't be used again. */
32
+ export declare const TERMINAL: Set<string>;
33
+ /** An authorization-code or refresh-token grant, at the token endpoint. */
34
+ export declare function grant(fetch: Fetch, params: Record<string, string>, now: number): Promise<Grant>;
35
+ /** The issuer's signing keys and revocation endpoint, from its OpenID configuration. */
36
+ export interface Issuer {
37
+ keys: Jwk[];
38
+ revocation?: string;
39
+ }
40
+ /** Read the issuer's configuration and keys; anything short of a usable key set throws. */
41
+ export declare function discover(fetch: Fetch): Promise<Issuer>;
42
+ /** Revoke a refresh token: true when the issuer confirmed it. Retried on 5xx and network errors. */
43
+ export declare function revoke(fetch: Fetch, issuer: Issuer, clientId: string, refreshToken: string): Promise<boolean>;
@@ -0,0 +1,104 @@
1
+ export const ISSUER = "https://auth.openai.com";
2
+ export const AUTHORIZE = `${ISSUER}/api/accounts/authorize`;
3
+ export const TOKEN = `${ISSUER}/api/accounts/oauth/token`;
4
+ const DISCOVERY = `${ISSUER}/.well-known/openid-configuration`;
5
+ /** The audience of the tokens: the Responses API. */
6
+ export const RESOURCE = "https://api.openai.com/v1";
7
+ /** The scope that lets the tokens draw on the person's ChatGPT plan. */
8
+ export const PLAN_SCOPE = "chatgpt.tokens.use.direct";
9
+ export const SCOPES = `openid profile email offline_access resource.invoke ${PLAN_SCOPE}`;
10
+ /** The client id of a first sign-in; OpenAI then issues this app's own `oaiapp_…` id. */
11
+ export const DYNAMIC_CLIENT = "dynamic_agent_client";
12
+ export const AGENT_NAME = "AgentOrg";
13
+ /** A word from a server, fit to show: at most 64 characters of letters, digits and `_.-[]`. */
14
+ export const word = (value) => typeof value === "string" ? value.replace(/[^A-Za-z0-9_.[\]-]/g, "").slice(0, 64) : "";
15
+ /** The token endpoint refused a grant, with OAuth's error code. */
16
+ export class GrantError extends Error {
17
+ code;
18
+ status;
19
+ constructor(code, status) {
20
+ super(`The token endpoint refused the grant (${status} ${code}).`);
21
+ this.code = code;
22
+ this.status = status;
23
+ }
24
+ }
25
+ /** Refresh errors after which the refresh token can't be used again. */
26
+ export const TERMINAL = new Set([
27
+ "invalid_grant",
28
+ "invalid_refresh_token",
29
+ "token_expired",
30
+ "refresh_token_expired",
31
+ "refresh_token_invalidated",
32
+ "refresh_token_reused",
33
+ ]);
34
+ async function json(response) {
35
+ try {
36
+ const value = await response.json();
37
+ return typeof value === "object" && value !== null ? value : {};
38
+ }
39
+ catch {
40
+ return {};
41
+ }
42
+ }
43
+ const form = (url, params) => new Request(url, {
44
+ method: "POST",
45
+ headers: { "content-type": "application/x-www-form-urlencoded", accept: "application/json" },
46
+ body: new URLSearchParams(params),
47
+ });
48
+ /** An authorization-code or refresh-token grant, at the token endpoint. */
49
+ export async function grant(fetch, params, now) {
50
+ const response = await fetch(form(TOKEN, { ...params, resource: RESOURCE }));
51
+ const body = await json(response);
52
+ if (!response.ok)
53
+ throw new GrantError(word(body.error) || "error", response.status);
54
+ const { access_token, refresh_token, id_token, expires_in, scope } = body;
55
+ if (typeof access_token !== "string" || typeof refresh_token !== "string")
56
+ throw new GrantError("invalid_response", response.status);
57
+ return {
58
+ accessToken: access_token,
59
+ refreshToken: refresh_token,
60
+ ...(typeof id_token === "string" ? { idToken: id_token } : {}),
61
+ expiresAt: now + (typeof expires_in === "number" ? expires_in : 3600) * 1000,
62
+ scopes: typeof scope === "string" ? scope.split(" ").filter(Boolean) : [],
63
+ };
64
+ }
65
+ /** Read the issuer's configuration and keys; anything short of a usable key set throws. */
66
+ export async function discover(fetch) {
67
+ const found = await fetch(new Request(DISCOVERY));
68
+ const config = await json(found);
69
+ if (!found.ok || config.issuer !== ISSUER || typeof config.jwks_uri !== "string")
70
+ throw new Error("unexpected OpenID configuration");
71
+ const fetched = await fetch(new Request(config.jwks_uri));
72
+ const jwks = await json(fetched);
73
+ const keys = Array.isArray(jwks.keys) ? jwks.keys : [];
74
+ if (!fetched.ok || keys.length === 0)
75
+ throw new Error("no signing keys");
76
+ return {
77
+ keys,
78
+ ...(typeof config.revocation_endpoint === "string"
79
+ ? { revocation: config.revocation_endpoint }
80
+ : {}),
81
+ };
82
+ }
83
+ /** Revoke a refresh token: true when the issuer confirmed it. Retried on 5xx and network errors. */
84
+ export async function revoke(fetch, issuer, clientId, refreshToken) {
85
+ if (!issuer.revocation)
86
+ return false;
87
+ const params = { token: refreshToken, token_type_hint: "refresh_token", client_id: clientId };
88
+ for (let attempt = 0;; attempt++) {
89
+ try {
90
+ const response = await fetch(form(issuer.revocation, params));
91
+ await response.body?.cancel();
92
+ if (response.ok)
93
+ return true;
94
+ if (response.status < 500)
95
+ return false;
96
+ }
97
+ catch {
98
+ // A network failure: try again while the token is still in hand.
99
+ }
100
+ if (attempt === 2)
101
+ return false;
102
+ await new Promise((resolve) => setTimeout(resolve, 250 * 2 ** attempt));
103
+ }
104
+ }
@@ -0,0 +1,23 @@
1
+ import type { Log } from "@agentorg/sdk";
2
+ export interface ChatGPTOptions {
3
+ hubUrl: string;
4
+ /** Its own Actor. It needs no slots: no `storage`, no `net`. */
5
+ actorId: string;
6
+ credential: string;
7
+ /** A private folder of its own, which holds the sign-in. */
8
+ dir: string;
9
+ log?: Log;
10
+ /** How a request leaves, instead of the network. For tests. */
11
+ fetch?: (request: Request, init: RequestInit) => Promise<Response>;
12
+ }
13
+ export interface ChatGPT {
14
+ /** Stop every response under way and leave the Hub. The sign-in stays. */
15
+ close(): Promise<void>;
16
+ }
17
+ /**
18
+ * Run ChatGPT as a program: an Actor exporting `account.login@1` as `login` and `llm.session@1` as
19
+ * `session`, with its sign-in in its own file and the network directly. It uses the person's plan,
20
+ * never API billing. Only https goes out, and a redirect comes back as it is, never followed with a
21
+ * request's token or form.
22
+ */
23
+ export declare function startChatGPT(options: ChatGPTOptions): Promise<ChatGPT>;
@@ -0,0 +1,41 @@
1
+ import { accountLogin, llmSession } from "@agentorg/interfaces";
2
+ import { connect } from "@agentorg/sdk/client";
3
+ import { fileStore } from "./file-store.js";
4
+ import { serveChatGPT } from "./serve.js";
5
+ const quiet = { info() { }, warn() { }, error() { } };
6
+ /**
7
+ * Run ChatGPT as a program: an Actor exporting `account.login@1` as `login` and `llm.session@1` as
8
+ * `session`, with its sign-in in its own file and the network directly. It uses the person's plan,
9
+ * never API billing. Only https goes out, and a redirect comes back as it is, never followed with a
10
+ * request's token or form.
11
+ */
12
+ export async function startChatGPT(options) {
13
+ const log = options.log ?? quiet;
14
+ const store = fileStore(options.dir, log);
15
+ const send = options.fetch ?? ((request, init) => fetch(request, init));
16
+ const outbound = async (request) => {
17
+ if (new URL(request.url).protocol !== "https:")
18
+ throw new Error("Only https addresses are reached.");
19
+ // Given beside the request, not by a copy of it: a copy's link to its signal can be collected,
20
+ // and a cancel would then wait for OpenAI's first byte.
21
+ return send(request, { redirect: "manual" });
22
+ };
23
+ let session;
24
+ const client = await connect({
25
+ hubUrl: options.hubUrl,
26
+ actorId: options.actorId,
27
+ credential: options.credential,
28
+ exports: { login: accountLogin, session: llmSession },
29
+ log,
30
+ start(ctx) {
31
+ session = serveChatGPT(ctx.exports, { fetch: outbound, store, log });
32
+ },
33
+ });
34
+ return {
35
+ async close() {
36
+ session?.stop();
37
+ client.close();
38
+ await client.closed;
39
+ },
40
+ };
41
+ }
@@ -0,0 +1,54 @@
1
+ /** Inference on the person's ChatGPT plan: the Responses API, streamed, with nothing stored. */
2
+ import type { llmSession } from "@agentorg/interfaces";
3
+ import { Refused } from "@agentorg/sdk";
4
+ import type { z } from "zod";
5
+ import { type Account } from "./account.js";
6
+ import { type Fetch } from "./oauth.js";
7
+ export declare const USAGE_SETTINGS = "https://chatgpt.com/settings/usage";
8
+ type RespondInput = z.output<typeof llmSession.methods.respond.input>;
9
+ /**
10
+ * OpenAI's error, as a refusal: by its code first, then its HTTP status when there is one (an error
11
+ * inside the stream has none). Only sanitized words of OpenAI's reply are shown.
12
+ */
13
+ export declare function refusalOf(code: string | undefined, at?: {
14
+ status?: number;
15
+ requestId?: string | null;
16
+ param?: string;
17
+ }): Refused;
18
+ export declare function createSession(options: {
19
+ fetch: Fetch;
20
+ account: Account;
21
+ /** Streamed text, for the one Actor whose request it answers. */
22
+ delta(caller: string, requestId: string, text: string): void;
23
+ }): {
24
+ /** Stop every response still running. */
25
+ stop(): void;
26
+ /** Forget the model list, so it is read again for the next account. */
27
+ reset(): void;
28
+ models(): Promise<{
29
+ models: {
30
+ id: string;
31
+ name: string;
32
+ }[];
33
+ }>;
34
+ cancel({ requestId }: {
35
+ requestId: string;
36
+ }, caller: string): {};
37
+ respond(input: RespondInput, caller: string): Promise<{
38
+ output: ({
39
+ type: "message";
40
+ content: string;
41
+ role: "assistant";
42
+ } | {
43
+ type: "function_call";
44
+ callId: string;
45
+ name: string;
46
+ arguments: string;
47
+ })[];
48
+ usage?: {
49
+ inputTokens: number;
50
+ outputTokens: number;
51
+ } | undefined;
52
+ }>;
53
+ };
54
+ export {};