@agi-cli/server 0.1.55

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/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@agi-cli/server",
3
+ "version": "0.1.55",
4
+ "description": "HTTP API server for AGI CLI",
5
+ "type": "module",
6
+ "main": "./src/index.ts",
7
+ "types": "./src/index.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./src/index.ts",
11
+ "types": "./src/index.ts"
12
+ },
13
+ "./runtime/agent-registry": {
14
+ "import": "./src/runtime/agent-registry.ts",
15
+ "types": "./src/runtime/agent-registry.ts"
16
+ },
17
+ "./runtime/ask-service.ts": {
18
+ "import": "./src/runtime/ask-service.ts",
19
+ "types": "./src/runtime/ask-service.ts"
20
+ },
21
+ "./routes/ask.ts": {
22
+ "import": "./src/routes/ask.ts",
23
+ "types": "./src/routes/ask.ts"
24
+ }
25
+ },
26
+ "scripts": {
27
+ "dev": "bun run src/index.ts",
28
+ "test": "bun test",
29
+ "typecheck": "tsc --noEmit"
30
+ },
31
+ "dependencies": {
32
+ "@agi-cli/sdk": "0.1.55",
33
+ "@agi-cli/database": "0.1.55",
34
+ "drizzle-orm": "^0.44.5",
35
+ "hono": "^4.9.9"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bun": "latest",
39
+ "typescript": "^5"
40
+ }
41
+ }
@@ -0,0 +1,28 @@
1
+ import type { AGIEvent } from './types.ts';
2
+
3
+ type Subscriber = (evt: AGIEvent) => void;
4
+
5
+ const subscribers = new Map<string, Set<Subscriber>>(); // sessionId -> subs
6
+
7
+ export function publish(event: AGIEvent) {
8
+ const subs = subscribers.get(event.sessionId);
9
+ if (!subs) return;
10
+ for (const sub of subs) {
11
+ try {
12
+ sub(event);
13
+ } catch {}
14
+ }
15
+ }
16
+
17
+ export function subscribe(sessionId: string, handler: Subscriber) {
18
+ let set = subscribers.get(sessionId);
19
+ if (!set) {
20
+ set = new Set();
21
+ subscribers.set(sessionId, set);
22
+ }
23
+ set.add(handler);
24
+ return () => {
25
+ set?.delete(handler);
26
+ if (set && set.size === 0) subscribers.delete(sessionId);
27
+ };
28
+ }
@@ -0,0 +1,20 @@
1
+ export type AGIEventType =
2
+ | 'session.created'
3
+ | 'session.updated'
4
+ | 'message.created'
5
+ | 'message.part.delta'
6
+ | 'message.completed'
7
+ | 'tool.call'
8
+ | 'tool.delta'
9
+ | 'tool.result'
10
+ | 'plan.updated'
11
+ | 'finish-step'
12
+ | 'usage'
13
+ | 'error'
14
+ | 'heartbeat';
15
+
16
+ export interface AGIEvent<T = unknown> {
17
+ type: AGIEventType;
18
+ sessionId: string;
19
+ payload?: T;
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,183 @@
1
+ import { Hono } from 'hono';
2
+ import { cors } from 'hono/cors';
3
+ import type { ProviderId } from '@agi-cli/sdk';
4
+ import { registerRootRoutes } from './routes/root.ts';
5
+ import { registerOpenApiRoute } from './routes/openapi.ts';
6
+ import { registerSessionsRoutes } from './routes/sessions.ts';
7
+ import { registerSessionMessagesRoutes } from './routes/session-messages.ts';
8
+ import { registerSessionStreamRoute } from './routes/session-stream.ts';
9
+ import { registerAskRoutes } from './routes/ask.ts';
10
+ import { registerConfigRoutes } from './routes/config.ts';
11
+ import { registerGitRoutes } from './routes/git.ts';
12
+
13
+ function initApp() {
14
+ const app = new Hono();
15
+
16
+ // Enable CORS for all localhost ports (for web UI on random ports)
17
+ app.use(
18
+ '*',
19
+ cors({
20
+ origin: (origin) => {
21
+ // Allow all localhost and 127.0.0.1 on any port
22
+ if (
23
+ origin.startsWith('http://localhost:') ||
24
+ origin.startsWith('http://127.0.0.1:')
25
+ ) {
26
+ return origin;
27
+ }
28
+ // Allow common dev ports
29
+ if (
30
+ origin === 'http://localhost:5173' ||
31
+ origin === 'http://localhost:5174'
32
+ ) {
33
+ return origin;
34
+ }
35
+ // Default to allowing the origin (can be restricted in production)
36
+ return origin;
37
+ },
38
+ allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
39
+ allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
40
+ exposeHeaders: ['Content-Length', 'X-Request-Id'],
41
+ credentials: true,
42
+ maxAge: 600,
43
+ }),
44
+ );
45
+
46
+ registerRootRoutes(app);
47
+ registerOpenApiRoute(app);
48
+ registerSessionsRoutes(app);
49
+ registerSessionMessagesRoutes(app);
50
+ registerSessionStreamRoute(app);
51
+ registerAskRoutes(app);
52
+ registerConfigRoutes(app);
53
+ registerGitRoutes(app);
54
+
55
+ return app;
56
+ }
57
+
58
+ const app = initApp();
59
+
60
+ export default {
61
+ port: 0,
62
+ fetch: app.fetch,
63
+ };
64
+
65
+ export function createApp() {
66
+ return app;
67
+ }
68
+
69
+ export type StandaloneAppConfig = {
70
+ provider?: ProviderId;
71
+ model?: string;
72
+ defaultAgent?: string;
73
+ };
74
+
75
+ export function createStandaloneApp(_config?: StandaloneAppConfig) {
76
+ const honoApp = new Hono();
77
+
78
+ // Enable CORS for all localhost ports (for web UI on random ports)
79
+ honoApp.use(
80
+ '*',
81
+ cors({
82
+ origin: (origin) => {
83
+ // Allow all localhost and 127.0.0.1 on any port
84
+ if (
85
+ origin.startsWith('http://localhost:') ||
86
+ origin.startsWith('http://127.0.0.1:')
87
+ ) {
88
+ return origin;
89
+ }
90
+ // Allow common dev ports
91
+ if (
92
+ origin === 'http://localhost:5173' ||
93
+ origin === 'http://localhost:5174'
94
+ ) {
95
+ return origin;
96
+ }
97
+ // Default to allowing the origin
98
+ return origin;
99
+ },
100
+ allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
101
+ allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
102
+ exposeHeaders: ['Content-Length', 'X-Request-Id'],
103
+ credentials: true,
104
+ maxAge: 600,
105
+ }),
106
+ );
107
+
108
+ registerRootRoutes(honoApp);
109
+ registerOpenApiRoute(honoApp);
110
+ registerSessionsRoutes(honoApp);
111
+ registerSessionMessagesRoutes(honoApp);
112
+ registerSessionStreamRoute(honoApp);
113
+ registerAskRoutes(honoApp);
114
+ registerConfigRoutes(honoApp);
115
+ registerGitRoutes(honoApp);
116
+
117
+ return honoApp;
118
+ }
119
+
120
+ export type EmbeddedAppConfig = {
121
+ provider: ProviderId;
122
+ model: string;
123
+ apiKey: string;
124
+ agent?: string;
125
+ };
126
+
127
+ export function createEmbeddedApp(_config: EmbeddedAppConfig) {
128
+ const honoApp = new Hono();
129
+
130
+ // Enable CORS for all localhost ports (for web UI on random ports)
131
+ honoApp.use(
132
+ '*',
133
+ cors({
134
+ origin: (origin) => {
135
+ // Allow all localhost and 127.0.0.1 on any port
136
+ if (
137
+ origin.startsWith('http://localhost:') ||
138
+ origin.startsWith('http://127.0.0.1:')
139
+ ) {
140
+ return origin;
141
+ }
142
+ // Allow common dev ports
143
+ if (
144
+ origin === 'http://localhost:5173' ||
145
+ origin === 'http://localhost:5174'
146
+ ) {
147
+ return origin;
148
+ }
149
+ // Default to allowing the origin
150
+ return origin;
151
+ },
152
+ allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
153
+ allowHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'],
154
+ exposeHeaders: ['Content-Length', 'X-Request-Id'],
155
+ credentials: true,
156
+ maxAge: 600,
157
+ }),
158
+ );
159
+
160
+ registerRootRoutes(honoApp);
161
+ registerOpenApiRoute(honoApp);
162
+ registerSessionsRoutes(honoApp);
163
+ registerSessionMessagesRoutes(honoApp);
164
+ registerSessionStreamRoute(honoApp);
165
+ registerAskRoutes(honoApp);
166
+ registerGitRoutes(honoApp);
167
+
168
+ return honoApp;
169
+ }
170
+
171
+ export {
172
+ resolveAgentConfig,
173
+ defaultToolsForAgent,
174
+ } from './runtime/agent-registry.ts';
175
+ export { composeSystemPrompt } from './runtime/prompt.ts';
176
+ export {
177
+ AskServiceError,
178
+ handleAskRequest,
179
+ deriveStatusFromMessage,
180
+ inferStatus,
181
+ } from './runtime/ask-service.ts';
182
+ export { registerSessionsRoutes } from './routes/sessions.ts';
183
+ export { registerAskRoutes } from './routes/ask.ts';