@ottocode/server 0.1.259 → 0.1.261

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 (69) hide show
  1. package/package.json +4 -3
  2. package/src/index.ts +5 -4
  3. package/src/openapi/register.ts +92 -0
  4. package/src/openapi/route.ts +22 -0
  5. package/src/routes/ask.ts +210 -99
  6. package/src/routes/auth.ts +1701 -626
  7. package/src/routes/branch.ts +281 -90
  8. package/src/routes/config/agents.ts +79 -32
  9. package/src/routes/config/cwd.ts +46 -14
  10. package/src/routes/config/debug.ts +159 -30
  11. package/src/routes/config/defaults.ts +182 -64
  12. package/src/routes/config/main.ts +109 -73
  13. package/src/routes/config/models.ts +304 -137
  14. package/src/routes/config/providers.ts +462 -166
  15. package/src/routes/config/utils.ts +2 -2
  16. package/src/routes/doctor.ts +395 -161
  17. package/src/routes/files.ts +650 -260
  18. package/src/routes/git/branch.ts +143 -52
  19. package/src/routes/git/commit.ts +347 -141
  20. package/src/routes/git/diff.ts +239 -116
  21. package/src/routes/git/init.ts +103 -23
  22. package/src/routes/git/pull.ts +167 -65
  23. package/src/routes/git/push.ts +222 -117
  24. package/src/routes/git/remote.ts +401 -100
  25. package/src/routes/git/staging.ts +502 -141
  26. package/src/routes/git/status.ts +171 -78
  27. package/src/routes/mcp.ts +1129 -404
  28. package/src/routes/openapi.ts +27 -4
  29. package/src/routes/ottorouter.ts +1221 -389
  30. package/src/routes/provider-usage.ts +153 -36
  31. package/src/routes/research.ts +817 -370
  32. package/src/routes/root.ts +50 -6
  33. package/src/routes/session-approval.ts +228 -54
  34. package/src/routes/session-files.ts +265 -134
  35. package/src/routes/session-messages.ts +330 -150
  36. package/src/routes/session-stream.ts +83 -2
  37. package/src/routes/sessions.ts +1830 -780
  38. package/src/routes/skills.ts +849 -161
  39. package/src/routes/terminals.ts +469 -103
  40. package/src/routes/tunnel.ts +394 -118
  41. package/src/runtime/agent/runner-reasoning.ts +38 -3
  42. package/src/runtime/agent/runner.ts +1 -0
  43. package/src/runtime/ask/service.ts +1 -0
  44. package/src/runtime/message/compaction-limits.ts +3 -3
  45. package/src/runtime/provider/reasoning.ts +18 -7
  46. package/src/runtime/session/db-operations.ts +4 -3
  47. package/src/runtime/utils/token.ts +7 -2
  48. package/src/tools/adapter.ts +21 -0
  49. package/src/openapi/paths/ask.ts +0 -81
  50. package/src/openapi/paths/auth.ts +0 -687
  51. package/src/openapi/paths/branch.ts +0 -102
  52. package/src/openapi/paths/config.ts +0 -485
  53. package/src/openapi/paths/doctor.ts +0 -165
  54. package/src/openapi/paths/files.ts +0 -236
  55. package/src/openapi/paths/git.ts +0 -690
  56. package/src/openapi/paths/mcp.ts +0 -339
  57. package/src/openapi/paths/messages.ts +0 -103
  58. package/src/openapi/paths/ottorouter.ts +0 -594
  59. package/src/openapi/paths/provider-usage.ts +0 -59
  60. package/src/openapi/paths/research.ts +0 -227
  61. package/src/openapi/paths/session-approval.ts +0 -93
  62. package/src/openapi/paths/session-extras.ts +0 -336
  63. package/src/openapi/paths/session-files.ts +0 -91
  64. package/src/openapi/paths/sessions.ts +0 -210
  65. package/src/openapi/paths/skills.ts +0 -377
  66. package/src/openapi/paths/stream.ts +0 -26
  67. package/src/openapi/paths/terminals.ts +0 -226
  68. package/src/openapi/paths/tunnel.ts +0 -163
  69. package/src/openapi/spec.ts +0 -73
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ottocode/server",
3
- "version": "0.1.259",
3
+ "version": "0.1.261",
4
4
  "description": "HTTP API server for ottocode",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -61,8 +61,9 @@
61
61
  "typecheck": "tsc --noEmit"
62
62
  },
63
63
  "dependencies": {
64
- "@ottocode/database": "0.1.259",
65
- "@ottocode/sdk": "0.1.259",
64
+ "@ottocode/database": "0.1.261",
65
+ "@ottocode/sdk": "0.1.261",
66
+ "@hono/zod-openapi": "^1.1.5",
66
67
  "ai-sdk-ollama": "^3.8.3",
67
68
  "drizzle-orm": "^0.44.5",
68
69
  "hono": "^4.9.9",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Hono } from 'hono';
1
+ import { OpenAPIHono } from '@hono/zod-openapi';
2
+ import type { BlankEnv } from 'hono/types';
2
3
  import { cors } from 'hono/cors';
3
4
  import type { ProviderId, AuthInfo } from '@ottocode/sdk';
4
5
  import { TerminalManager } from '@ottocode/sdk';
@@ -34,7 +35,7 @@ setTerminalManager(globalTerminalManager);
34
35
  installAiSdkWarningHandler();
35
36
 
36
37
  function initApp() {
37
- const app = new Hono();
38
+ const app = new OpenAPIHono<BlankEnv>();
38
39
 
39
40
  // Enable CORS for localhost and local network access
40
41
  app.use(
@@ -110,7 +111,7 @@ export type StandaloneAppConfig = {
110
111
  };
111
112
 
112
113
  export function createStandaloneApp(_config?: StandaloneAppConfig) {
113
- const honoApp = new Hono();
114
+ const honoApp = new OpenAPIHono<BlankEnv>();
114
115
 
115
116
  honoApp.use(
116
117
  '*',
@@ -203,7 +204,7 @@ export type EmbeddedAppConfig = {
203
204
  };
204
205
 
205
206
  export function createEmbeddedApp(config: EmbeddedAppConfig = {}) {
206
- const honoApp = new Hono();
207
+ const honoApp = new OpenAPIHono<BlankEnv>();
207
208
 
208
209
  // Store injected config in Hono context for routes to access
209
210
  // Config can be empty - routes will fall back to files/env
@@ -0,0 +1,92 @@
1
+ import type { OpenAPIHono, RouteConfig } from '@hono/zod-openapi';
2
+ import { z } from '@hono/zod-openapi';
3
+ import type {
4
+ MediaTypeObject,
5
+ OperationObject,
6
+ ReferenceObject,
7
+ RequestBodyObject,
8
+ ResponseObject,
9
+ } from 'openapi3-ts/oas30';
10
+ import { schemas } from './schemas';
11
+
12
+ export type HttpMethod = RouteConfig['method'];
13
+
14
+ function isReferenceObject(value: unknown): value is ReferenceObject {
15
+ return Boolean(
16
+ value &&
17
+ typeof value === 'object' &&
18
+ '$ref' in value &&
19
+ typeof (value as { $ref?: unknown }).$ref === 'string',
20
+ );
21
+ }
22
+
23
+ function schemaToZod(schema: unknown) {
24
+ if (!schema || isReferenceObject(schema)) {
25
+ return z.any().openapi(schema as Record<string, unknown>);
26
+ }
27
+ return z.any().openapi(schema as Record<string, unknown>);
28
+ }
29
+
30
+ function convertContent(content: ResponseObject['content']) {
31
+ if (!content) return undefined;
32
+ return Object.fromEntries(
33
+ Object.entries(content).map(([contentType, mediaType]) => [
34
+ contentType,
35
+ {
36
+ ...(mediaType as MediaTypeObject),
37
+ schema: schemaToZod((mediaType as MediaTypeObject).schema),
38
+ },
39
+ ]),
40
+ );
41
+ }
42
+
43
+ function convertRequestBody(
44
+ requestBody: OperationObject['requestBody'],
45
+ ): RouteConfig['request'] extends { body?: infer Body } ? Body : never {
46
+ if (!requestBody || isReferenceObject(requestBody))
47
+ return requestBody as never;
48
+ const body = requestBody as RequestBodyObject;
49
+ return {
50
+ ...body,
51
+ content: convertContent(body.content) ?? {},
52
+ } as never;
53
+ }
54
+
55
+ function convertResponses(
56
+ operation: OperationObject,
57
+ ): RouteConfig['responses'] {
58
+ return Object.fromEntries(
59
+ Object.entries(operation.responses ?? {}).map(([status, response]) => {
60
+ if (isReferenceObject(response)) return [status, response];
61
+ return [
62
+ status,
63
+ {
64
+ ...(response as ResponseObject),
65
+ content: convertContent((response as ResponseObject).content),
66
+ },
67
+ ];
68
+ }),
69
+ ) as RouteConfig['responses'];
70
+ }
71
+
72
+ export function buildRouteConfig(
73
+ method: HttpMethod,
74
+ path: string,
75
+ operation: OperationObject,
76
+ ): RouteConfig {
77
+ return {
78
+ ...operation,
79
+ method,
80
+ path,
81
+ request: {
82
+ body: convertRequestBody(operation.requestBody),
83
+ },
84
+ responses: convertResponses(operation),
85
+ } as RouteConfig;
86
+ }
87
+
88
+ export function registerOpenApiComponents(app: OpenAPIHono) {
89
+ for (const [name, schema] of Object.entries(schemas)) {
90
+ app.openAPIRegistry.registerComponent('schemas', name, schema as never);
91
+ }
92
+ }
@@ -0,0 +1,22 @@
1
+ import { createRoute, type OpenAPIHono } from '@hono/zod-openapi';
2
+ import type { Handler, Hono } from 'hono';
3
+ import type { OperationObject } from 'openapi3-ts/oas30';
4
+ import { buildRouteConfig, type HttpMethod } from './register.ts';
5
+
6
+ type InlineRouteConfig = OperationObject & {
7
+ method: HttpMethod;
8
+ path: string;
9
+ };
10
+
11
+ export function openApiRoute(
12
+ app: Hono,
13
+ route: InlineRouteConfig,
14
+ handler: Handler,
15
+ ) {
16
+ const openApiApp = app as OpenAPIHono;
17
+ const { method, path, ...operation } = route;
18
+ return openApiApp.openapi(
19
+ createRoute(buildRouteConfig(method, path, operation)),
20
+ handler as never,
21
+ );
22
+ }
package/src/routes/ask.ts CHANGED
@@ -8,116 +8,227 @@ import { handleAskRequest } from '../runtime/ask/service.ts';
8
8
  import { serializeError } from '../runtime/errors/api-error.ts';
9
9
  import { logger } from '@ottocode/sdk';
10
10
  import type { EmbeddedAppConfig } from '../index.ts';
11
+ import { openApiRoute } from '../openapi/route.ts';
11
12
 
12
13
  export function registerAskRoutes(app: Hono) {
13
- app.post('/v1/ask', async (c) => {
14
- const projectRoot = c.req.query('project') || process.cwd();
15
- const body = (await c.req.json().catch(() => ({}))) as Record<
16
- string,
17
- unknown
18
- >;
19
- const prompt = typeof body.prompt === 'string' ? body.prompt : '';
20
- if (!prompt.trim().length) {
21
- return c.json({ error: 'Prompt is required.' }, 400);
22
- }
23
-
24
- const embeddedConfig = (
25
- c as unknown as {
26
- get: (key: 'embeddedConfig') => EmbeddedAppConfig | undefined;
14
+ openApiRoute(
15
+ app,
16
+ {
17
+ method: 'post',
18
+ path: '/v1/ask',
19
+ tags: ['ask'],
20
+ operationId: 'ask',
21
+ summary: 'Send a prompt using the ask service',
22
+ description:
23
+ 'Streamlined endpoint used by the CLI to send prompts and receive assistant responses. Creates sessions as needed and reuses the last session when requested.',
24
+ parameters: [
25
+ {
26
+ in: 'query',
27
+ name: 'project',
28
+ required: false,
29
+ schema: {
30
+ type: 'string',
31
+ },
32
+ description:
33
+ 'Project root override (defaults to current working directory).',
34
+ },
35
+ ],
36
+ requestBody: {
37
+ required: true,
38
+ content: {
39
+ 'application/json': {
40
+ schema: {
41
+ type: 'object',
42
+ required: ['prompt'],
43
+ properties: {
44
+ prompt: {
45
+ type: 'string',
46
+ description: 'User prompt to send to the assistant.',
47
+ },
48
+ agent: {
49
+ type: 'string',
50
+ description: 'Optional agent name to use for this request.',
51
+ },
52
+ provider: {
53
+ $ref: '#/components/schemas/Provider',
54
+ description:
55
+ 'Optional provider override. When omitted the agent and config defaults apply.',
56
+ },
57
+ model: {
58
+ type: 'string',
59
+ description:
60
+ 'Optional model override for the selected provider.',
61
+ },
62
+ reasoningText: {
63
+ type: 'boolean',
64
+ description:
65
+ 'Enable extended thinking / reasoning for models that support it.',
66
+ },
67
+ reasoningLevel: {
68
+ type: 'string',
69
+ enum: ['minimal', 'low', 'medium', 'high', 'max', 'xhigh'],
70
+ description:
71
+ 'Optional reasoning intensity override for supported providers/models.',
72
+ },
73
+ sessionId: {
74
+ type: 'string',
75
+ description: 'Send the prompt to a specific session.',
76
+ },
77
+ last: {
78
+ type: 'boolean',
79
+ description:
80
+ 'If true, reuse the most recent session for the project.',
81
+ },
82
+ jsonMode: {
83
+ type: 'boolean',
84
+ description:
85
+ 'Request structured JSON output when supported by the agent.',
86
+ },
87
+ },
88
+ },
89
+ },
90
+ },
91
+ },
92
+ responses: {
93
+ '202': {
94
+ description: 'Accepted',
95
+ content: {
96
+ 'application/json': {
97
+ schema: {
98
+ $ref: '#/components/schemas/AskResponse',
99
+ },
100
+ },
101
+ },
102
+ },
103
+ '400': {
104
+ description: 'Bad Request',
105
+ content: {
106
+ 'application/json': {
107
+ schema: {
108
+ type: 'object',
109
+ properties: {
110
+ error: {
111
+ type: 'string',
112
+ },
113
+ },
114
+ required: ['error'],
115
+ },
116
+ },
117
+ },
118
+ },
119
+ },
120
+ },
121
+ async (c) => {
122
+ const projectRoot = c.req.query('project') || process.cwd();
123
+ const body = (await c.req.json().catch(() => ({}))) as Record<
124
+ string,
125
+ unknown
126
+ >;
127
+ const prompt = typeof body.prompt === 'string' ? body.prompt : '';
128
+ if (!prompt.trim().length) {
129
+ return c.json({ error: 'Prompt is required.' }, 400);
27
130
  }
28
- ).get('embeddedConfig');
29
131
 
30
- // Hybrid fallback: Use embedded config if provided, otherwise fall back to files/env
31
- let injectableConfig: InjectableConfig | undefined;
32
- let injectableCredentials: InjectableCredentials | undefined;
33
- let skipFileConfig = false;
132
+ const embeddedConfig = (
133
+ c as unknown as {
134
+ get: (key: 'embeddedConfig') => EmbeddedAppConfig | undefined;
135
+ }
136
+ ).get('embeddedConfig');
34
137
 
35
- if (embeddedConfig && Object.keys(embeddedConfig).length > 0) {
36
- // Has embedded config - build injectable config from it
37
- const defaults = embeddedConfig.defaults;
38
- const hasDefaults =
39
- defaults ||
40
- embeddedConfig.provider ||
41
- embeddedConfig.model ||
42
- embeddedConfig.agent;
138
+ // Hybrid fallback: Use embedded config if provided, otherwise fall back to files/env
139
+ let injectableConfig: InjectableConfig | undefined;
140
+ let injectableCredentials: InjectableCredentials | undefined;
141
+ let skipFileConfig = false;
43
142
 
44
- if (hasDefaults) {
45
- injectableConfig = {
46
- provider: defaults?.provider ?? embeddedConfig.provider,
47
- model: defaults?.model ?? embeddedConfig.model,
48
- agent: defaults?.agent ?? embeddedConfig.agent,
49
- };
50
- }
143
+ if (embeddedConfig && Object.keys(embeddedConfig).length > 0) {
144
+ // Has embedded config - build injectable config from it
145
+ const defaults = embeddedConfig.defaults;
146
+ const hasDefaults =
147
+ defaults ||
148
+ embeddedConfig.provider ||
149
+ embeddedConfig.model ||
150
+ embeddedConfig.agent;
51
151
 
52
- // Convert embedded auth to injectable credentials
53
- const hasAuth = embeddedConfig.auth || embeddedConfig.apiKey;
54
- if (hasAuth) {
55
- if (embeddedConfig.auth) {
56
- injectableCredentials = {} as InjectableCredentials;
57
- for (const [provider, auth] of Object.entries(embeddedConfig.auth)) {
58
- if ('apiKey' in auth) {
59
- (injectableCredentials as Record<string, { apiKey: string }>)[
60
- provider
61
- ] = { apiKey: auth.apiKey };
62
- }
63
- }
64
- } else if (embeddedConfig.apiKey && embeddedConfig.provider) {
65
- injectableCredentials = {
66
- [embeddedConfig.provider]: { apiKey: embeddedConfig.apiKey },
152
+ if (hasDefaults) {
153
+ injectableConfig = {
154
+ provider: defaults?.provider ?? embeddedConfig.provider,
155
+ model: defaults?.model ?? embeddedConfig.model,
156
+ agent: defaults?.agent ?? embeddedConfig.agent,
67
157
  };
68
158
  }
69
159
 
70
- // Only skip file config if we have credentials injected
71
- skipFileConfig = true;
160
+ // Convert embedded auth to injectable credentials
161
+ const hasAuth = embeddedConfig.auth || embeddedConfig.apiKey;
162
+ if (hasAuth) {
163
+ if (embeddedConfig.auth) {
164
+ injectableCredentials = {} as InjectableCredentials;
165
+ for (const [provider, auth] of Object.entries(
166
+ embeddedConfig.auth,
167
+ )) {
168
+ if ('apiKey' in auth) {
169
+ (injectableCredentials as Record<string, { apiKey: string }>)[
170
+ provider
171
+ ] = { apiKey: auth.apiKey };
172
+ }
173
+ }
174
+ } else if (embeddedConfig.apiKey && embeddedConfig.provider) {
175
+ injectableCredentials = {
176
+ [embeddedConfig.provider]: { apiKey: embeddedConfig.apiKey },
177
+ };
178
+ }
179
+
180
+ // Only skip file config if we have credentials injected
181
+ skipFileConfig = true;
182
+ }
183
+ // If no auth provided, skipFileConfig stays false -> will use ensureProviderEnv -> auth.json fallback
72
184
  }
73
- // If no auth provided, skipFileConfig stays false -> will use ensureProviderEnv -> auth.json fallback
74
- }
75
185
 
76
- const request: AskServerRequest = {
77
- projectRoot,
78
- prompt,
79
- agent: typeof body.agent === 'string' ? body.agent : undefined,
80
- provider: typeof body.provider === 'string' ? body.provider : undefined,
81
- model: typeof body.model === 'string' ? body.model : undefined,
82
- reasoningText:
83
- typeof body.reasoningText === 'boolean'
84
- ? body.reasoningText
85
- : undefined,
86
- reasoningLevel:
87
- typeof body.reasoningLevel === 'string'
88
- ? (body.reasoningLevel as AskServerRequest['reasoningLevel'])
89
- : undefined,
90
- sessionId:
91
- typeof body.sessionId === 'string' ? body.sessionId : undefined,
92
- last: Boolean(body.last),
93
- jsonMode: Boolean(body.jsonMode),
94
- skipFileConfig:
95
- skipFileConfig ||
96
- (typeof body.skipFileConfig === 'boolean'
97
- ? body.skipFileConfig
98
- : false),
99
- config:
100
- injectableConfig ||
101
- (body.config && typeof body.config === 'object'
102
- ? (body.config as InjectableConfig)
103
- : undefined),
104
- credentials:
105
- injectableCredentials ||
106
- (body.credentials && typeof body.credentials === 'object'
107
- ? (body.credentials as InjectableCredentials)
108
- : undefined),
109
- agentPrompt:
110
- typeof body.agentPrompt === 'string' ? body.agentPrompt : undefined,
111
- tools: Array.isArray(body.tools) ? body.tools : undefined,
112
- };
186
+ const request: AskServerRequest = {
187
+ projectRoot,
188
+ prompt,
189
+ agent: typeof body.agent === 'string' ? body.agent : undefined,
190
+ provider: typeof body.provider === 'string' ? body.provider : undefined,
191
+ model: typeof body.model === 'string' ? body.model : undefined,
192
+ reasoningText:
193
+ typeof body.reasoningText === 'boolean'
194
+ ? body.reasoningText
195
+ : undefined,
196
+ reasoningLevel:
197
+ typeof body.reasoningLevel === 'string'
198
+ ? (body.reasoningLevel as AskServerRequest['reasoningLevel'])
199
+ : undefined,
200
+ sessionId:
201
+ typeof body.sessionId === 'string' ? body.sessionId : undefined,
202
+ last: Boolean(body.last),
203
+ jsonMode: Boolean(body.jsonMode),
204
+ skipFileConfig:
205
+ skipFileConfig ||
206
+ (typeof body.skipFileConfig === 'boolean'
207
+ ? body.skipFileConfig
208
+ : false),
209
+ config:
210
+ injectableConfig ||
211
+ (body.config && typeof body.config === 'object'
212
+ ? (body.config as InjectableConfig)
213
+ : undefined),
214
+ credentials:
215
+ injectableCredentials ||
216
+ (body.credentials && typeof body.credentials === 'object'
217
+ ? (body.credentials as InjectableCredentials)
218
+ : undefined),
219
+ agentPrompt:
220
+ typeof body.agentPrompt === 'string' ? body.agentPrompt : undefined,
221
+ tools: Array.isArray(body.tools) ? body.tools : undefined,
222
+ };
113
223
 
114
- try {
115
- const response = await handleAskRequest(request);
116
- return c.json(response, 202);
117
- } catch (err) {
118
- logger.error('Ask request failed', err);
119
- const errorResponse = serializeError(err);
120
- return c.json(errorResponse, errorResponse.error.status || 400);
121
- }
122
- });
224
+ try {
225
+ const response = await handleAskRequest(request);
226
+ return c.json(response, 202);
227
+ } catch (err) {
228
+ logger.error('Ask request failed', err);
229
+ const errorResponse = serializeError(err);
230
+ return c.json(errorResponse, errorResponse.error.status || 400);
231
+ }
232
+ },
233
+ );
123
234
  }