@curie-agent/daemon 0.2.5

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 (43) hide show
  1. package/LICENSE +179 -0
  2. package/dist/src/approval-tracker.d.ts +42 -0
  3. package/dist/src/approval-tracker.d.ts.map +1 -0
  4. package/dist/src/approval-tracker.js +78 -0
  5. package/dist/src/approval-tracker.js.map +1 -0
  6. package/dist/src/auth.d.ts +17 -0
  7. package/dist/src/auth.d.ts.map +1 -0
  8. package/dist/src/auth.js +94 -0
  9. package/dist/src/auth.js.map +1 -0
  10. package/dist/src/channel-manager.d.ts +50 -0
  11. package/dist/src/channel-manager.d.ts.map +1 -0
  12. package/dist/src/channel-manager.js +184 -0
  13. package/dist/src/channel-manager.js.map +1 -0
  14. package/dist/src/daemon-app.d.ts +88 -0
  15. package/dist/src/daemon-app.d.ts.map +1 -0
  16. package/dist/src/daemon-app.js +496 -0
  17. package/dist/src/daemon-app.js.map +1 -0
  18. package/dist/src/index.d.ts +19 -0
  19. package/dist/src/index.d.ts.map +1 -0
  20. package/dist/src/index.js +26 -0
  21. package/dist/src/index.js.map +1 -0
  22. package/dist/src/jsonrpc-handler.d.ts +47 -0
  23. package/dist/src/jsonrpc-handler.d.ts.map +1 -0
  24. package/dist/src/jsonrpc-handler.js +2205 -0
  25. package/dist/src/jsonrpc-handler.js.map +1 -0
  26. package/dist/src/server.d.ts +49 -0
  27. package/dist/src/server.d.ts.map +1 -0
  28. package/dist/src/server.js +204 -0
  29. package/dist/src/server.js.map +1 -0
  30. package/dist/src/slash-cd.d.ts +38 -0
  31. package/dist/src/slash-cd.d.ts.map +1 -0
  32. package/dist/src/slash-cd.js +93 -0
  33. package/dist/src/slash-cd.js.map +1 -0
  34. package/dist/src/static-files.d.ts +7 -0
  35. package/dist/src/static-files.d.ts.map +1 -0
  36. package/dist/src/static-files.js +73 -0
  37. package/dist/src/static-files.js.map +1 -0
  38. package/dist/src/ws-handler.d.ts +21 -0
  39. package/dist/src/ws-handler.d.ts.map +1 -0
  40. package/dist/src/ws-handler.js +119 -0
  41. package/dist/src/ws-handler.js.map +1 -0
  42. package/dist/tsconfig.tsbuildinfo +1 -0
  43. package/package.json +37 -0
package/LICENSE ADDED
@@ -0,0 +1,179 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ revisions are patently meant to be an adaptation, but does not
43
+ include a work that is merely a copy of the Work except a Derivative
44
+ Work.
45
+
46
+ "Contribution" shall mean any work of authorship, including
47
+ the original version of the Work and any modifications or additions
48
+ to that Work or Derivative Works thereof, that is intentionally
49
+ submitted to the Licensor for inclusion in the Work by the copyright owner
50
+ or by an individual or Legal Entity authorized to submit on behalf of
51
+ the copyright owner. For the purposes of this definition, "submitted"
52
+ means any form of electronic, verbal, or written communication sent
53
+ to the Licensor or its representatives, including but not limited to
54
+ communication on electronic mailing lists, source code control systems,
55
+ and issue tracking systems that are managed by, or on behalf of, the
56
+ Licensor for the purpose of discussing and improving the Work, but
57
+ excluding communication that is conspicuously marked or otherwise
58
+ designated in writing by the copyright owner as "Not a Contribution."
59
+
60
+ "Contributor" shall mean Licensor and any individual or Legal Entity
61
+ on behalf of whom a Contribution has been received by Licensor and
62
+ subsequently incorporated within the Work.
63
+
64
+ 2. Grant of Copyright License. Subject to the terms and conditions of
65
+ this License, each Contributor hereby grants to You a perpetual,
66
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
67
+ copyright license to reproduce, prepare Derivative Works of,
68
+ publicly display, publicly perform, sublicense, and distribute the
69
+ Work and such Derivative Works in Source or Object form.
70
+
71
+ 3. Grant of Patent License. Subject to the terms and conditions of
72
+ this License, each Contributor hereby grants to You a perpetual,
73
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
74
+ (except as stated in this section) patent license to make, have made,
75
+ use, offer to sell, sell, import, and otherwise transfer the Work,
76
+ where such license applies only to those patent claims licensable
77
+ by such Contributor that are necessarily infringed by their
78
+ Contribution(s) alone or by combination of their Contribution(s)
79
+ with the Work to which such Contribution(s) was submitted. If You
80
+ institute patent litigation against any entity (including a
81
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
82
+ or a Contribution incorporated within the Work constitutes direct
83
+ or contributory patent infringement, then any patent licenses
84
+ granted to You under this License for that Work shall terminate
85
+ as of the date such litigation is filed.
86
+
87
+ 4. Redistribution. You may reproduce and distribute copies of the
88
+ Work or Derivative Works thereof in any medium, with or without
89
+ modifications, and in Source or Object form, provided that You
90
+ meet the following conditions:
91
+
92
+ (a) You must give any other recipients of the Work or
93
+ Derivative Works a copy of this License; and
94
+
95
+ (b) You must cause any modified files to carry prominent notices
96
+ stating that You changed the files; and
97
+
98
+ (c) You must retain, in the Source form of any Derivative Works
99
+ that You distribute, all copyright, patent, trademark, and
100
+ attribution notices from the Source form of the Work,
101
+ excluding those notices that do not pertain to any part of
102
+ the Derivative Works; and
103
+
104
+ (d) If the Work includes a "NOTICE" text file as part of its
105
+ distribution, then any Derivative Works that You distribute must
106
+ include a readable copy of the attribution notices contained
107
+ within such NOTICE file, excluding any notices that do not
108
+ pertain to any part of the Derivative Works, in at least one
109
+ of the following places: within a NOTICE text file distributed
110
+ as part of the Derivative Works; within the Source form or
111
+ documentation, if provided along with the Derivative Works; or,
112
+ within a display generated by the Derivative Works, if and
113
+ wherever such third-party notices normally appear. The contents
114
+ of the NOTICE file are for informational purposes only and
115
+ do not modify the License. You may add Your own attribution
116
+ notices within Derivative Works that You distribute, alongside
117
+ or as an addendum to the NOTICE text from the Work, provided
118
+ that such additional attribution notices cannot be construed
119
+ as modifying the License.
120
+
121
+ You may add Your own copyright statement to Your modifications and
122
+ may provide additional or different license terms and conditions
123
+ for use, reproduction, or distribution of Your modifications, or
124
+ for any such Derivative Works as a whole, provided Your use,
125
+ reproduction, and distribution of the Work otherwise complies with
126
+ the conditions stated in this License.
127
+
128
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
129
+ any Contribution intentionally submitted for inclusion in the Work
130
+ by You to the Licensor shall be under the terms and conditions of
131
+ this License, without any additional terms or conditions.
132
+ Notwithstanding the above, nothing herein shall supersede or modify
133
+ the terms of any separate license agreement you may have executed
134
+ with Licensor regarding such Contributions.
135
+
136
+ 6. Trademarks. This License does not grant permission to use the trade
137
+ names, trademarks, service marks, or product names of the Licensor,
138
+ except as required for reasonable and customary use in describing the
139
+ origin of the Work and reproducing the content of the NOTICE file.
140
+
141
+ 7. Limitation of Liability. Unless required by applicable law or
142
+ agreed to in writing, Licensor provides the Work (and each
143
+ Contributor provides its Contributions) on an "AS IS" BASIS,
144
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
145
+ implied, including, without limitation, any warranties or conditions
146
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
147
+ PARTICULAR PURPOSE. You are solely responsible for determining the
148
+ appropriateness of using or redistributing the Work and assume any
149
+ risks associated with Your exercise of permissions under this License.
150
+
151
+ 8. Governing Law. This License shall be governed by the laws of the
152
+ State of New York, USA, excluding its choice of law rules.
153
+
154
+ 9. Accepting Warranty or Additional Liability. While redistributing
155
+ the Work or Derivative Works thereof, You may choose to offer,
156
+ and charge a fee for, acceptance of support, warranty, indemnity,
157
+ or other liability obligations and/or rights consistent with this
158
+ License. However, in accepting such obligations, You may act only
159
+ on Your own behalf and on Your sole responsibility, not on behalf
160
+ of any other Contributor, and You agree to indemnify, defend, and
161
+ hold each Contributor harmless for any liability incurred by, or
162
+ claims asserted against, such Contributor by reason of your
163
+ exercising any rights or permissions given by this License.
164
+
165
+ END OF TERMS AND CONDITIONS
166
+
167
+ Copyright 2025 curie-agent contributors
168
+
169
+ Licensed under the Apache License, Version 2.0 (the "License");
170
+ you may not use this file except in compliance with the License.
171
+ You may obtain a copy of the License at
172
+
173
+ http://www.apache.org/licenses/LICENSE-2.0
174
+
175
+ Unless required by applicable law or agreed to in writing, software
176
+ distributed under the License is distributed on an "AS IS" BASIS,
177
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
178
+ See the License for the specific language governing permissions and
179
+ limitations under the License.
@@ -0,0 +1,42 @@
1
+ import type { EventBus } from '@curie-agent/core';
2
+ /**
3
+ * Tracks pending approval requests. TurnLoop calls register() when it
4
+ * needs user approval; clients resolve via decide() RPC.
5
+ */
6
+ export declare class ApprovalTracker {
7
+ private eventBus;
8
+ private pending;
9
+ constructor(eventBus: EventBus);
10
+ /**
11
+ * Register a pending approval. Returns a promise that resolves
12
+ * when the user approves/rejects.
13
+ */
14
+ register(params: {
15
+ toolCallId: string;
16
+ name: string;
17
+ input: Record<string, unknown>;
18
+ sessionId: string;
19
+ channelId: string;
20
+ }): Promise<boolean>;
21
+ /**
22
+ * Resolve a pending approval. First call wins; subsequent calls
23
+ * return { decided: true, decision: 'already-decided' }.
24
+ */
25
+ decide(toolCallId: string, decision: 'allow' | 'deny'): {
26
+ decided: boolean;
27
+ decision: string;
28
+ };
29
+ /** List all pending approvals. */
30
+ list(sessionId?: string): Array<{
31
+ toolCallId: string;
32
+ name: string;
33
+ input: Record<string, unknown>;
34
+ sessionId: string;
35
+ channelId: string;
36
+ createdAt: number;
37
+ }>;
38
+ /** Remove stale entries (cleanup). */
39
+ clear(): void;
40
+ get pendingCount(): number;
41
+ }
42
+ //# sourceMappingURL=approval-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-tracker.d.ts","sourceRoot":"","sources":["../../src/approval-tracker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAS,MAAM,mBAAmB,CAAC;AAczD;;;GAGG;AACH,qBAAa,eAAe;IAGd,OAAO,CAAC,QAAQ;IAF5B,OAAO,CAAC,OAAO,CAAsC;gBAEjC,QAAQ,EAAE,QAAQ;IAEtC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE;QACf,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC/B,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBpB;;;OAGG;IACH,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;IAmB9F,kCAAkC;IAClC,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;QAC9B,UAAU,EAAE,MAAM,CAAC;QACnB,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC/B,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;QAClB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;IAyBF,sCAAsC;IACtC,KAAK;IASL,IAAI,YAAY,IAAI,MAAM,CAEzB;CACF"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Tracks pending approval requests. TurnLoop calls register() when it
3
+ * needs user approval; clients resolve via decide() RPC.
4
+ */
5
+ export class ApprovalTracker {
6
+ eventBus;
7
+ pending = new Map();
8
+ constructor(eventBus) {
9
+ this.eventBus = eventBus;
10
+ }
11
+ /**
12
+ * Register a pending approval. Returns a promise that resolves
13
+ * when the user approves/rejects.
14
+ */
15
+ register(params) {
16
+ const { toolCallId, name, input, sessionId, channelId } = params;
17
+ const id = crypto.randomUUID();
18
+ return new Promise((resolve, reject) => {
19
+ this.pending.set(toolCallId, {
20
+ toolCallId, name, input, sessionId, channelId,
21
+ resolve, reject, decided: false, createdAt: Date.now(),
22
+ });
23
+ this.eventBus.emit({
24
+ type: 'approval-request',
25
+ id, toolCallId, name, input, timestamp: Date.now(),
26
+ });
27
+ });
28
+ }
29
+ /**
30
+ * Resolve a pending approval. First call wins; subsequent calls
31
+ * return { decided: true, decision: 'already-decided' }.
32
+ */
33
+ decide(toolCallId, decision) {
34
+ const entry = this.pending.get(toolCallId);
35
+ if (!entry || entry.decided) {
36
+ return { decided: true, decision: 'already-decided' };
37
+ }
38
+ entry.decided = true;
39
+ entry.resolve(decision === 'allow');
40
+ const id = crypto.randomUUID();
41
+ this.eventBus.emit({
42
+ type: 'approval-decision',
43
+ id, toolCallId, decision, timestamp: Date.now(),
44
+ });
45
+ this.pending.delete(toolCallId);
46
+ return { decided: true, decision };
47
+ }
48
+ /** List all pending approvals. */
49
+ list(sessionId) {
50
+ const result = [];
51
+ for (const entry of this.pending.values()) {
52
+ if (sessionId && entry.sessionId !== sessionId)
53
+ continue;
54
+ result.push({
55
+ toolCallId: entry.toolCallId,
56
+ name: entry.name,
57
+ input: entry.input,
58
+ sessionId: entry.sessionId,
59
+ channelId: entry.channelId,
60
+ createdAt: entry.createdAt,
61
+ });
62
+ }
63
+ return result;
64
+ }
65
+ /** Remove stale entries (cleanup). */
66
+ clear() {
67
+ for (const [toolCallId, entry] of this.pending) {
68
+ if (!entry.decided) {
69
+ entry.reject(new Error('cleared'));
70
+ }
71
+ }
72
+ this.pending.clear();
73
+ }
74
+ get pendingCount() {
75
+ return this.pending.size;
76
+ }
77
+ }
78
+ //# sourceMappingURL=approval-tracker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"approval-tracker.js","sourceRoot":"","sources":["../../src/approval-tracker.ts"],"names":[],"mappings":"AAcA;;;GAGG;AACH,MAAM,OAAO,eAAe;IAGN;IAFZ,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IAErD,YAAoB,QAAkB;QAAlB,aAAQ,GAAR,QAAQ,CAAU;IAAG,CAAC;IAE1C;;;OAGG;IACH,QAAQ,CAAC,MAMR;QACC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;QACjE,MAAM,EAAE,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC;QAE/B,OAAO,IAAI,OAAO,CAAU,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC9C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE;gBAC3B,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;gBAC7C,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;aACvD,CAAC,CAAC;YAEH,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;gBACjB,IAAI,EAAE,kBAAkB;gBACxB,EAAE,EAAE,UAAU,E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S,EAAE,KAAK,CAAC,SAAS;gBAC1B,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,SAAS,EAAE,KAAK,CAAC,SAAS;aAC3B,CAAC,CAAC;QACL,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,sCAAsC;IACtC,KAAK;QACH,KAAK,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAC/C,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;gBACnB,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;YACrC,CAAC;QACH,CAAC;QACD,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;IAC3B,CAAC;CACF"}
@@ -0,0 +1,17 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ export declare function generateToken(): string;
3
+ export declare function saveToken(token: string): void;
4
+ export declare function loadToken(): string | undefined;
5
+ /** Ensure a token exists, generating one if needed. */
6
+ export declare function ensureToken(): string;
7
+ export declare function validateToken(req: IncomingMessage): boolean;
8
+ export declare function validateTokenWs(url: URL): boolean;
9
+ /** Validate token from either Authorization header, query param, or cookies. */
10
+ export declare function validateTokenHttp(req: IncomingMessage): boolean;
11
+ /** Reject with 401 JSON. */
12
+ export declare function rejectUnauthorized(res: ServerResponse): void;
13
+ /** Add CORS headers. */
14
+ export declare function setCorsHeaders(res: ServerResponse): void;
15
+ /** Handle CORS preflight. */
16
+ export declare function handleCorsPreflight(res: ServerResponse): void;
17
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAKjE,wBAAgB,aAAa,IAAI,MAAM,CAItC;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAE7C;AAED,wBAAgB,SAAS,IAAI,MAAM,GAAG,SAAS,CAkB9C;AAED,uDAAuD;AACvD,wBAAgB,WAAW,IAAI,MAAM,CAOpC;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAI3D;AAED,wBAAgB,eAAe,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAGjD;AAED,gFAAgF;AAChF,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAyB/D;AAED,4BAA4B;AAC5B,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAG5D;AAED,wBAAwB;AACxB,wBAAgB,cAAc,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAIxD;AAED,6BAA6B;AAC7B,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAI7D"}
@@ -0,0 +1,94 @@
1
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ const DAEMON_TOKEN_FILE = join(homedir(), '.curie-agent', 'daemon.token');
5
+ const SETTINGS_FILE = join(homedir(), '.curie-agent', 'settings.json');
6
+ export function generateToken() {
7
+ const buf = new Uint8Array(16);
8
+ crypto.getRandomValues(buf);
9
+ return Array.from(buf).map((b) => b.toString(16).padStart(2, '0')).join('');
10
+ }
11
+ export function saveToken(token) {
12
+ writeFileSync(DAEMON_TOKEN_FILE, token, { mode: 0o600 });
13
+ }
14
+ export function loadToken() {
15
+ if (process.env.NODE_ENV !== 'test' && existsSync(SETTINGS_FILE)) {
16
+ try {
17
+ const rawSettings = readFileSync(SETTINGS_FILE, 'utf-8');
18
+ const parsed = JSON.parse(rawSettings);
19
+ const token = parsed.daemon_token;
20
+ if (typeof token === 'string' && token.trim().length > 0) {
21
+ return token.trim();
22
+ }
23
+ }
24
+ catch {
25
+ // ignore JSON parse errors
26
+ }
27
+ }
28
+ if (existsSync(DAEMON_TOKEN_FILE)) {
29
+ return readFileSync(DAEMON_TOKEN_FILE, 'utf-8').trim();
30
+ }
31
+ return undefined;
32
+ }
33
+ /** Ensure a token exists, generating one if needed. */
34
+ export function ensureToken() {
35
+ let token = loadToken();
36
+ if (!token) {
37
+ token = generateToken();
38
+ saveToken(token);
39
+ }
40
+ return token;
41
+ }
42
+ export function validateToken(req) {
43
+ const auth = req.headers.authorization ?? '';
44
+ const token = ensureToken();
45
+ return auth === `Bearer ${token}`;
46
+ }
47
+ export function validateTokenWs(url) {
48
+ const token = ensureToken();
49
+ return url.searchParams.get('token') === token;
50
+ }
51
+ /** Validate token from either Authorization header, query param, or cookies. */
52
+ export function validateTokenHttp(req) {
53
+ const auth = req.headers.authorization ?? '';
54
+ const token = ensureToken();
55
+ if (auth === `Bearer ${token}`)
56
+ return true;
57
+ // Also accept token from query param (for browser access: ?token=...)
58
+ const url = req.url ?? '';
59
+ const parsed = new URL(url, `http://${req.headers.host ?? 'localhost'}`);
60
+ if (parsed.searchParams.get('token') === token)
61
+ return true;
62
+ // Check Cookie header (e.g. curie_token=<token>)
63
+ const cookieHeader = req.headers.cookie ?? '';
64
+ const cookies = cookieHeader.split(';');
65
+ for (const cookie of cookies) {
66
+ const parts = cookie.trim().split('=');
67
+ if (parts.length >= 2) {
68
+ const name = parts[0];
69
+ const val = parts.slice(1).join('=');
70
+ if (name === 'curie_token' && val === token) {
71
+ return true;
72
+ }
73
+ }
74
+ }
75
+ return false;
76
+ }
77
+ /** Reject with 401 JSON. */
78
+ export function rejectUnauthorized(res) {
79
+ res.writeHead(401, { 'Content-Type': 'application/json' });
80
+ res.end(JSON.stringify({ error: 'Unauthorized', message: 'Invalid or missing token' }));
81
+ }
82
+ /** Add CORS headers. */
83
+ export function setCorsHeaders(res) {
84
+ res.setHeader('Access-Control-Allow-Origin', '*');
85
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
86
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
87
+ }
88
+ /** Handle CORS preflight. */
89
+ export function handleCorsPreflight(res) {
90
+ setCorsHeaders(res);
91
+ res.writeHead(204);
92
+ res.end();
93
+ }
94
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,MAAM,iBAAiB,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,cAAc,EAAE,cAAc,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,cAAc,EAAE,eAAe,CAAC,CAAC;AAEvE,MAAM,UAAU,aAAa;IAC3B,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC/B,MAAM,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;IAC5B,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC9E,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,aAAa,CAAC,iBAAiB,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,MAAM,UAAU,SAAS;IACvB,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KA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AM,UAAU,kBAAkB,CAAC,GAAmB;IACpD,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAC,CAAC;IAC3D,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,0BAA0B,EAAE,CAAC,CAAC,CAAC;AAC1F,CAAC;AAED,wBAAwB;AACxB,MAAM,UAAU,cAAc,CAAC,GAAmB;IAChD,GAAG,CAAC,SAAS,CAAC,6BAA6B,EAAE,GAAG,CAAC,CAAC;IAClD,GAAG,CAAC,SAAS,CAAC,8BAA8B,EAAE,oBAAoB,CAAC,CAAC;IACpE,GAAG,CAAC,SAAS,CAAC,8BAA8B,EAAE,6BAA6B,CAAC,CAAC;AAC/E,CAAC;AAED,6BAA6B;AAC7B,MAAM,UAAU,mBAAmB,CAAC,GAAmB;IACrD,cAAc,CAAC,GAAG,CAAC,CAAC;IACpB,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IACnB,GAAG,CAAC,GAAG,EAAE,CAAC;AACZ,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { EventBus, SessionStore, SettingsManager, Tool } from '@curie-agent/core';
2
+ import type { ChannelEntry } from '@curie-agent/core';
3
+ import type { ProviderFactory } from './server.js';
4
+ import { ApprovalTracker } from './approval-tracker.js';
5
+ /** Maintains per-channel TurnLoop instances and session mapping. */
6
+ export declare class ChannelManager {
7
+ private eventBus;
8
+ private sessionStore;
9
+ private settingsManager;
10
+ private createProvider?;
11
+ private tools;
12
+ private approvalTracker?;
13
+ private systemPrompt?;
14
+ private registry;
15
+ private router;
16
+ private turnLoops;
17
+ constructor(eventBus: EventBus, sessionStore: SessionStore, settingsManager: SettingsManager, createProvider?: ProviderFactory | undefined, tools?: Tool[], approvalTracker?: ApprovalTracker | undefined, systemPrompt?: string | undefined);
18
+ /**
19
+ * Send a message on a channel. Creates or reuses the channel's TurnLoop.
20
+ * Bridges turn loop events to the shared daemon EventBus.
21
+ */
22
+ send(channelId: string, text: string, cwd?: string, type?: string): Promise<{
23
+ status: string;
24
+ sessionId: string;
25
+ events: number;
26
+ }>;
27
+ /** Cancel a running turn for a channel. */
28
+ cancel(channelId: string): void;
29
+ /** Get or create the main CLI channel. */
30
+ getOrCreateMain(): ChannelEntry;
31
+ /** Handle incoming Telegram message — resolve channel. */
32
+ routeTelegramMessage(params: {
33
+ chatId: string;
34
+ userId: string;
35
+ isGroup: boolean;
36
+ chatTitle?: string;
37
+ }): {
38
+ channelId: string;
39
+ sessionId: string;
40
+ } | null;
41
+ /** Get the Telegram chat ID for a channel. */
42
+ getTelegramChatId(channelId: string): string | undefined;
43
+ /** List all channels. */
44
+ listChannels(): ChannelEntry[];
45
+ /** Get a channel by ID. */
46
+ getChannel(channelId: string): ChannelEntry | undefined;
47
+ /** Clean up all turn loops (on shutdown). */
48
+ cleanup(): void;
49
+ }
50
+ //# sourceMappingURL=channel-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"channel-manager.d.ts","sourceRoot":"","sources":["../../src/channel-manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,QAAQ,EAAS,YAAY,EAAE,eAAe,EACf,IAAI,EACpC,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,mBAAmB,CAAC;AACnE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,oEAAoE;AACpE,qBAAa,cAAc;IAMvB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,YAAY;IACpB,OAAO,CAAC,eAAe;IACvB,OAAO,CAAC,cAAc,CAAC;IACvB,OAAO,CAAC,KAAK;IACb,OAAO,CAAC,eAAe,CAAC;IACxB,OAAO,CAAC,YAAY,CAAC;IAXvB,OAAO,CAAC,QAAQ,CAAkB;IAClC,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