-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathindex.ts
More file actions
145 lines (126 loc) · 4.46 KB
/
index.ts
File metadata and controls
145 lines (126 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { env } from './env';
import './sentry';
import { defaultSDK } from './instrumentation';
defaultSDK.start();
import {
CredentialStoreRegistry,
createDefaultCredentialStores,
type ServerConfig,
} from '@inkeep/agents-core';
import { getLogger } from './logger';
const logger = getLogger('agents-api-init');
import { createEmailService } from '@inkeep/agents-email';
import { Hono } from 'hono';
import { createAgentsHono } from './createApp';
import { createAgentsAuth } from './factory';
import type { SandboxConfig } from './types';
import { recoverOrphanedWorkflows, world } from './workflow/world';
export type { AppConfig, AppVariables } from './types';
// Re-export Hono to ensure it's not tree-shaken (required for Vercel framework detection)
export { Hono };
// Export SandboxConfig type for use in applications
export type {
NativeSandboxConfig,
SandboxConfig,
VercelSandboxConfig,
} from './domains/run/types/executionContext';
// Re-export everything from factory for backward compatibility
export type { SSOProviderConfig, UserAuthConfig } from './factory';
export {
createAgentsApp,
createAgentsHono,
} from './factory';
// Create default configuration
const defaultConfig: ServerConfig = {
port: 3002,
serverOptions: {
requestTimeout: 120000,
keepAliveTimeout: 60000,
keepAlive: true,
},
};
const sandboxConfig: SandboxConfig =
process.env.SANDBOX_VERCEL_TEAM_ID &&
process.env.SANDBOX_VERCEL_PROJECT_ID &&
process.env.SANDBOX_VERCEL_TOKEN
? {
provider: 'vercel',
runtime: 'node22',
timeout: 60000,
vcpus: 4,
teamId: process.env.SANDBOX_VERCEL_TEAM_ID,
projectId: process.env.SANDBOX_VERCEL_PROJECT_ID,
token: process.env.SANDBOX_VERCEL_TOKEN,
}
: { provider: 'native', runtime: 'node22', timeout: 30000, vcpus: 2 };
const socialProviders =
process.env.PUBLIC_GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET
? {
google: {
prompt: 'select_account' as const,
display: 'popup' as const,
clientId: process.env.PUBLIC_GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
},
}
: undefined;
const emailService = createEmailService();
export const auth = createAgentsAuth({ socialProviders }, emailService);
// Create default credential stores
const defaultStores = createDefaultCredentialStores();
const defaultRegistry = new CredentialStoreRegistry(defaultStores);
const app = createAgentsHono({
serverConfig: defaultConfig,
credentialStores: defaultRegistry,
auth,
sandboxConfig,
});
// Start the workflow world worker and recover orphaned workflows.
const workflowWorld = process.env.WORKFLOW_TARGET_WORLD || 'local';
if (workflowWorld === '@workflow/world-postgres' || workflowWorld === 'local') {
const STARTUP_DELAY_MS = 3000; // Wait for Vite/server to start
logger.info(
{ targetWorld: workflowWorld, delayMs: STARTUP_DELAY_MS },
'Scheduling workflow world worker start'
);
setTimeout(async () => {
try {
if (workflowWorld === '@workflow/world-postgres') {
await world.start();
logger.info({}, 'Workflow world worker started successfully');
} else {
logger.info(
{ targetWorld: workflowWorld },
'Workflow world does not require explicit start'
);
}
const recoveredCount = await recoverOrphanedWorkflows();
if (recoveredCount > 0) {
logger.info({ recoveredCount }, 'Recovered orphaned workflow(s)');
}
} catch (err) {
logger.error({ error: err }, 'Failed to start workflow world');
}
}, STARTUP_DELAY_MS);
}
// Start Slack Socket Mode client for local development (when configured)
if (env.ENVIRONMENT === 'development' && env.SLACK_APP_TOKEN) {
const SOCKET_MODE_DELAY_MS = 3000;
logger.info({ delayMs: SOCKET_MODE_DELAY_MS }, 'Scheduling Slack Socket Mode start');
setTimeout(async () => {
try {
const { startSocketMode } = await import('@inkeep/agents-work-apps/slack');
await startSocketMode(env.SLACK_APP_TOKEN as string);
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'MODULE_NOT_FOUND') {
logger.error(
{},
'SLACK_APP_TOKEN is set but @slack/socket-mode is not installed. Run: pnpm add -D @slack/socket-mode (in packages/agents-work-apps)'
);
} else {
logger.error({ error: err }, 'Failed to start Slack Socket Mode');
}
}
}, SOCKET_MODE_DELAY_MS);
}
export default app;