@gmickel/gno 1.16.0 → 1.18.0
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/README.md +34 -18
- package/assets/skill/SKILL.md +25 -6
- package/assets/skill/mcp-reference.md +21 -0
- package/package.json +2 -2
- package/src/app/context-agent-projection.ts +303 -0
- package/src/app/context-format.ts +249 -0
- package/src/app/context-runtime-contract.ts +325 -0
- package/src/app/context-runtime-input.ts +362 -0
- package/src/app/context-runtime-types.ts +65 -0
- package/src/app/context-runtime.ts +170 -0
- package/src/app/context-surface.ts +145 -0
- package/src/cli/commands/context-build.ts +149 -0
- package/src/cli/commands/context-verify.ts +90 -0
- package/src/cli/commands/daemon.ts +69 -2
- package/src/cli/commands/models/pull.ts +13 -3
- package/src/cli/commands/status.ts +2 -0
- package/src/cli/detach.ts +37 -20
- package/src/cli/options.ts +4 -0
- package/src/cli/program.ts +252 -27
- package/src/config/index.ts +3 -0
- package/src/config/types.ts +37 -0
- package/src/core/context-budget.ts +461 -0
- package/src/core/context-capsule-index-schema.ts +15 -0
- package/src/core/context-capsule-retrieval-schema.ts +81 -0
- package/src/core/context-capsule-schema.ts +473 -0
- package/src/core/context-capsule-validation.ts +416 -0
- package/src/core/context-capsule-verification.ts +218 -0
- package/src/core/context-capsule.ts +439 -0
- package/src/core/context-compiler.ts +513 -0
- package/src/core/context-evidence-metadata.ts +33 -0
- package/src/core/context-evidence.ts +495 -0
- package/src/core/context-facets.ts +163 -0
- package/src/core/context-guidance.ts +69 -0
- package/src/core/context-scope.ts +32 -0
- package/src/core/context-verifier-canonical.ts +90 -0
- package/src/core/context-verifier-input.ts +66 -0
- package/src/core/context-verifier.ts +447 -0
- package/src/core/job-manager.ts +19 -0
- package/src/core/mutation-generations.ts +33 -0
- package/src/core/sections.ts +63 -0
- package/src/llm/cache.ts +13 -3
- package/src/llm/nodeLlamaCpp/adapter.ts +10 -1
- package/src/llm/nodeLlamaCpp/lifecycle.ts +71 -0
- package/src/mcp/context.ts +161 -0
- package/src/mcp/http-security.ts +477 -0
- package/src/mcp/http-session.ts +272 -0
- package/src/mcp/http-transport.ts +370 -0
- package/src/mcp/resources/index.ts +141 -134
- package/src/mcp/server.ts +28 -82
- package/src/mcp/tools/add-collection.ts +3 -1
- package/src/mcp/tools/capture.ts +3 -0
- package/src/mcp/tools/clear-collection-embeddings.ts +2 -0
- package/src/mcp/tools/context.ts +230 -0
- package/src/mcp/tools/embed.ts +62 -52
- package/src/mcp/tools/index-cmd.ts +88 -74
- package/src/mcp/tools/index.ts +49 -2
- package/src/mcp/tools/remove-collection.ts +2 -0
- package/src/mcp/tools/status.ts +11 -0
- package/src/mcp/tools/sync.ts +16 -14
- package/src/mcp/tools/workspace-write.ts +7 -3
- package/src/pipeline/chunk-lookup.ts +33 -0
- package/src/pipeline/hybrid.ts +79 -57
- package/src/pipeline/types.ts +14 -0
- package/src/sdk/client.ts +68 -6
- package/src/sdk/index.ts +21 -0
- package/src/sdk/types.ts +24 -0
- package/src/serve/background-runtime.ts +12 -211
- package/src/serve/context-capsule.ts +136 -0
- package/src/serve/context.ts +10 -1
- package/src/serve/embed-scheduler.ts +74 -43
- package/src/serve/index.ts +9 -0
- package/src/serve/jobs.ts +78 -80
- package/src/serve/public/components/HealthCenter.tsx +74 -1
- package/src/serve/public/globals.built.css +1 -1
- package/src/serve/public/pages/Dashboard.tsx +1 -0
- package/src/serve/resident-admission.ts +159 -0
- package/src/serve/resident-background-work.ts +39 -0
- package/src/serve/resident-request.ts +55 -0
- package/src/serve/resident-runtime.ts +490 -0
- package/src/serve/resident-status.ts +96 -0
- package/src/serve/routes/api.ts +265 -167
- package/src/serve/routes/mcp.ts +69 -0
- package/src/serve/server.ts +212 -35
- package/src/serve/status-model.ts +51 -0
- package/src/serve/status.ts +5 -0
- package/src/store/sqlite/adapter.ts +64 -29
|
@@ -1,46 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
import { resolve } from "node:path";
|
|
1
|
+
/** Backwards-compatible entrypoint for the shared resident runtime. */
|
|
3
2
|
|
|
4
3
|
import type { Config } from "../config/types";
|
|
5
4
|
import type { SyncResult } from "../ingestion";
|
|
6
|
-
import type {
|
|
5
|
+
import type { SqliteAdapter } from "../store/sqlite/adapter";
|
|
7
6
|
import type { EmbedResult, EmbedScheduler } from "./embed-scheduler";
|
|
8
7
|
import type { ContextHolder } from "./routes/api";
|
|
9
|
-
import type {
|
|
10
|
-
CollectionWatchCallbacks,
|
|
11
|
-
CollectionWatchService,
|
|
12
|
-
} from "./watch-service";
|
|
8
|
+
import type { CollectionWatchService } from "./watch-service";
|
|
13
9
|
|
|
14
|
-
import { getIndexDbPath } from "../app/constants";
|
|
15
|
-
import { INDEX_NAME_REQUIREMENTS, isValidIndexName } from "../app/index-name";
|
|
16
10
|
import {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
loadConfig,
|
|
22
|
-
} from "../config";
|
|
23
|
-
import { defaultSyncService } from "../ingestion";
|
|
24
|
-
import { withContentTypeRules } from "../ingestion";
|
|
25
|
-
import { getActivePreset } from "../llm/registry";
|
|
26
|
-
import { SqliteAdapter } from "../store/sqlite/adapter";
|
|
27
|
-
import {
|
|
28
|
-
createServerContext,
|
|
29
|
-
type CreateServerContextOptions,
|
|
30
|
-
disposeServerContext,
|
|
31
|
-
type ServerContext,
|
|
32
|
-
} from "./context";
|
|
33
|
-
import { createEmbedScheduler } from "./embed-scheduler";
|
|
34
|
-
import { CollectionWatchService as DefaultCollectionWatchService } from "./watch-service";
|
|
11
|
+
startResidentRuntime,
|
|
12
|
+
type ResidentRuntimeDeps,
|
|
13
|
+
type ResidentRuntimeOptions,
|
|
14
|
+
} from "./resident-runtime";
|
|
35
15
|
|
|
36
|
-
export
|
|
37
|
-
|
|
38
|
-
index?: string;
|
|
39
|
-
requireCollections?: boolean;
|
|
40
|
-
offline?: boolean;
|
|
41
|
-
eventBus?: DocumentEventBus | null;
|
|
42
|
-
watchCallbacks?: CollectionWatchCallbacks;
|
|
43
|
-
}
|
|
16
|
+
export type BackgroundRuntimeOptions = ResidentRuntimeOptions;
|
|
17
|
+
export type BackgroundRuntimeDeps = ResidentRuntimeDeps;
|
|
44
18
|
|
|
45
19
|
export interface BackgroundRuntime {
|
|
46
20
|
store: SqliteAdapter;
|
|
@@ -48,16 +22,13 @@ export interface BackgroundRuntime {
|
|
|
48
22
|
actualConfigPath: string;
|
|
49
23
|
ctxHolder: ContextHolder;
|
|
50
24
|
scheduler: EmbedScheduler;
|
|
51
|
-
eventBus: DocumentEventBus | null;
|
|
25
|
+
eventBus: import("./doc-events").DocumentEventBus | null;
|
|
52
26
|
watchService: CollectionWatchService;
|
|
53
27
|
syncAll(options?: {
|
|
54
28
|
gitPull?: boolean;
|
|
55
29
|
runUpdateCmd?: boolean;
|
|
56
30
|
triggerEmbed?: boolean;
|
|
57
|
-
}): Promise<{
|
|
58
|
-
syncResult: SyncResult;
|
|
59
|
-
embedResult: EmbedResult | null;
|
|
60
|
-
}>;
|
|
31
|
+
}): Promise<{ syncResult: SyncResult; embedResult: EmbedResult | null }>;
|
|
61
32
|
dispose(): Promise<void>;
|
|
62
33
|
}
|
|
63
34
|
|
|
@@ -65,179 +36,9 @@ export type BackgroundRuntimeResult =
|
|
|
65
36
|
| { success: true; runtime: BackgroundRuntime }
|
|
66
37
|
| { success: false; error: string };
|
|
67
38
|
|
|
68
|
-
type BackgroundRuntimeDeps = {
|
|
69
|
-
isInitialized?: typeof isInitialized;
|
|
70
|
-
loadConfig?: typeof loadConfig;
|
|
71
|
-
getConfigPaths?: typeof getConfigPaths;
|
|
72
|
-
ensureDirectories?: typeof ensureDirectories;
|
|
73
|
-
storeFactory?: () => SqliteAdapter;
|
|
74
|
-
createServerContext?: (
|
|
75
|
-
store: SqliteAdapter,
|
|
76
|
-
config: Config,
|
|
77
|
-
options?: CreateServerContextOptions
|
|
78
|
-
) => Promise<ServerContext>;
|
|
79
|
-
disposeServerContext?: (ctx: ServerContext) => Promise<void>;
|
|
80
|
-
createEmbedScheduler?: typeof createEmbedScheduler;
|
|
81
|
-
syncAllService?: typeof defaultSyncService.syncAll;
|
|
82
|
-
watchServiceFactory?: (options: {
|
|
83
|
-
collections: Config["collections"];
|
|
84
|
-
store: SqliteAdapter;
|
|
85
|
-
scheduler: EmbedScheduler | null;
|
|
86
|
-
eventBus?: DocumentEventBus | null;
|
|
87
|
-
callbacks?: CollectionWatchCallbacks;
|
|
88
|
-
syncOptions?: Parameters<typeof withContentTypeRules>[0];
|
|
89
|
-
}) => CollectionWatchService;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
39
|
export async function startBackgroundRuntime(
|
|
93
40
|
options: BackgroundRuntimeOptions = {},
|
|
94
41
|
deps: BackgroundRuntimeDeps = {}
|
|
95
42
|
): Promise<BackgroundRuntimeResult> {
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
success: false,
|
|
99
|
-
error: `Invalid index name: ${INDEX_NAME_REQUIREMENTS}.`,
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
const syncAllService = deps.syncAllService
|
|
103
|
-
? (...args: Parameters<typeof defaultSyncService.syncAll>) =>
|
|
104
|
-
deps.syncAllService!(...args)
|
|
105
|
-
: defaultSyncService.syncAll.bind(defaultSyncService);
|
|
106
|
-
const initialized = await (deps.isInitialized ?? isInitialized)(
|
|
107
|
-
options.configPath
|
|
108
|
-
);
|
|
109
|
-
if (!initialized) {
|
|
110
|
-
return { success: false, error: "GNO not initialized. Run: gno init" };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const configResult = await (deps.loadConfig ?? loadConfig)(
|
|
114
|
-
options.configPath
|
|
115
|
-
);
|
|
116
|
-
if (!configResult.ok) {
|
|
117
|
-
return { success: false, error: configResult.error.message };
|
|
118
|
-
}
|
|
119
|
-
for (const warning of formatConfigWarnings(configResult.warnings)) {
|
|
120
|
-
console.warn(warning);
|
|
121
|
-
}
|
|
122
|
-
const config = configResult.value;
|
|
123
|
-
|
|
124
|
-
if (options.requireCollections && config.collections.length === 0) {
|
|
125
|
-
return {
|
|
126
|
-
success: false,
|
|
127
|
-
error: "No collections configured. Run: gno collection add <path>",
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
await (deps.ensureDirectories ?? ensureDirectories)();
|
|
132
|
-
|
|
133
|
-
const store = deps.storeFactory ? deps.storeFactory() : new SqliteAdapter();
|
|
134
|
-
const dbPath = getIndexDbPath(options.index);
|
|
135
|
-
const paths = (deps.getConfigPaths ?? getConfigPaths)();
|
|
136
|
-
const actualConfigPath = resolve(options.configPath ?? paths.configFile);
|
|
137
|
-
store.setConfigPath(actualConfigPath);
|
|
138
|
-
|
|
139
|
-
const openResult = await store.open(dbPath, config.ftsTokenizer);
|
|
140
|
-
if (!openResult.ok) {
|
|
141
|
-
return { success: false, error: openResult.error.message };
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const syncCollResult = await store.syncCollections(config.collections);
|
|
145
|
-
if (!syncCollResult.ok) {
|
|
146
|
-
await store.close();
|
|
147
|
-
return { success: false, error: syncCollResult.error.message };
|
|
148
|
-
}
|
|
149
|
-
const syncCtxResult = await store.syncContexts(config.contexts ?? []);
|
|
150
|
-
if (!syncCtxResult.ok) {
|
|
151
|
-
await store.close();
|
|
152
|
-
return { success: false, error: syncCtxResult.error.message };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const ctx = await (deps.createServerContext ?? createServerContext)(
|
|
156
|
-
store,
|
|
157
|
-
config,
|
|
158
|
-
{
|
|
159
|
-
offline: options.offline ?? false,
|
|
160
|
-
}
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
const ctxHolder: ContextHolder = {
|
|
164
|
-
current: ctx,
|
|
165
|
-
config,
|
|
166
|
-
scheduler: null,
|
|
167
|
-
eventBus: options.eventBus ?? null,
|
|
168
|
-
watchService: null,
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
const scheduler = (deps.createEmbedScheduler ?? createEmbedScheduler)({
|
|
172
|
-
db: store.getRawDb(),
|
|
173
|
-
getEmbedPort: () => ctxHolder.current.embedPort,
|
|
174
|
-
getVectorIndex: () => ctxHolder.current.vectorIndex,
|
|
175
|
-
getModelUri: () => getActivePreset(ctxHolder.config).embed,
|
|
176
|
-
});
|
|
177
|
-
ctxHolder.scheduler = scheduler;
|
|
178
|
-
ctxHolder.current.scheduler = scheduler;
|
|
179
|
-
ctxHolder.current.eventBus = options.eventBus ?? null;
|
|
180
|
-
|
|
181
|
-
const watchService = (
|
|
182
|
-
deps.watchServiceFactory ??
|
|
183
|
-
((watchOptions) => new DefaultCollectionWatchService(watchOptions))
|
|
184
|
-
)({
|
|
185
|
-
collections: config.collections,
|
|
186
|
-
store,
|
|
187
|
-
scheduler,
|
|
188
|
-
eventBus: options.eventBus ?? null,
|
|
189
|
-
callbacks: options.watchCallbacks,
|
|
190
|
-
syncOptions: withContentTypeRules({}, config),
|
|
191
|
-
});
|
|
192
|
-
watchService.start();
|
|
193
|
-
ctxHolder.watchService = watchService;
|
|
194
|
-
ctxHolder.current.watchService = watchService;
|
|
195
|
-
|
|
196
|
-
let disposed = false;
|
|
197
|
-
|
|
198
|
-
return {
|
|
199
|
-
success: true,
|
|
200
|
-
runtime: {
|
|
201
|
-
store,
|
|
202
|
-
config,
|
|
203
|
-
actualConfigPath,
|
|
204
|
-
ctxHolder,
|
|
205
|
-
scheduler,
|
|
206
|
-
eventBus: options.eventBus ?? null,
|
|
207
|
-
watchService,
|
|
208
|
-
async syncAll(syncOptions = {}) {
|
|
209
|
-
const syncResult = await syncAllService(
|
|
210
|
-
config.collections,
|
|
211
|
-
store,
|
|
212
|
-
withContentTypeRules(
|
|
213
|
-
{
|
|
214
|
-
gitPull: syncOptions.gitPull,
|
|
215
|
-
runUpdateCmd: syncOptions.runUpdateCmd,
|
|
216
|
-
},
|
|
217
|
-
config
|
|
218
|
-
)
|
|
219
|
-
);
|
|
220
|
-
|
|
221
|
-
let embedResult: EmbedResult | null = null;
|
|
222
|
-
if (syncOptions.triggerEmbed !== false) {
|
|
223
|
-
embedResult = await scheduler.triggerNow();
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
return { syncResult, embedResult };
|
|
227
|
-
},
|
|
228
|
-
async dispose() {
|
|
229
|
-
if (disposed) {
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
disposed = true;
|
|
233
|
-
await Promise.resolve(watchService.dispose());
|
|
234
|
-
options.eventBus?.close();
|
|
235
|
-
scheduler.dispose();
|
|
236
|
-
await (deps.disposeServerContext ?? disposeServerContext)(
|
|
237
|
-
ctxHolder.current
|
|
238
|
-
);
|
|
239
|
-
await store.close();
|
|
240
|
-
},
|
|
241
|
-
},
|
|
242
|
-
};
|
|
43
|
+
return startResidentRuntime(options, deps);
|
|
243
44
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/** REST adapters for the shared Context Capsule application boundary. */
|
|
2
|
+
|
|
3
|
+
import type { GnoContextErrorCode } from "../sdk/types";
|
|
4
|
+
import type { ServerContext } from "./context";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
formatContextCapsuleMarkdown,
|
|
8
|
+
formatContextCapsuleVerificationMarkdown,
|
|
9
|
+
} from "../app/context-format";
|
|
10
|
+
import {
|
|
11
|
+
buildContextCapsule,
|
|
12
|
+
canonicalBuiltContextCapsuleJson,
|
|
13
|
+
canonicalVerifiedContextCapsuleJson,
|
|
14
|
+
validateContextCapsuleBuildInput,
|
|
15
|
+
verifyContextCapsuleRuntime,
|
|
16
|
+
} from "../app/context-runtime";
|
|
17
|
+
import {
|
|
18
|
+
contextSurfaceError,
|
|
19
|
+
parseContextBuildSurfaceInput,
|
|
20
|
+
parseContextVerifySurfaceInput,
|
|
21
|
+
} from "../app/context-surface";
|
|
22
|
+
import { ContextCapsuleContractError } from "../core/context-capsule";
|
|
23
|
+
|
|
24
|
+
const JSON_HEADERS = { "content-type": "application/json; charset=utf-8" };
|
|
25
|
+
const MARKDOWN_HEADERS = {
|
|
26
|
+
"content-type": "text/markdown; charset=utf-8",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const parseJsonBody = async (request: Request): Promise<unknown> => {
|
|
30
|
+
try {
|
|
31
|
+
return await request.json();
|
|
32
|
+
} catch {
|
|
33
|
+
throw new ContextCapsuleContractError("invalid_input", "Invalid JSON body");
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type ContextRestErrorCode = GnoContextErrorCode | "runtime_error";
|
|
38
|
+
|
|
39
|
+
export const CONTEXT_REST_ERROR_STATUS = {
|
|
40
|
+
invalid_goal: 400,
|
|
41
|
+
invalid_budget: 400,
|
|
42
|
+
invalid_filter: 400,
|
|
43
|
+
invalid_uri: 400,
|
|
44
|
+
invalid_input: 400,
|
|
45
|
+
identity_mismatch: 400,
|
|
46
|
+
no_evidence: 404,
|
|
47
|
+
tokenizer_unavailable: 503,
|
|
48
|
+
chunk_coordinate_mismatch: 409,
|
|
49
|
+
stored_provenance_mismatch: 409,
|
|
50
|
+
index_snapshot_mismatch: 409,
|
|
51
|
+
index_changed_during_compile: 409,
|
|
52
|
+
context_changed_during_compile: 409,
|
|
53
|
+
capsule_mutated_during_verify: 409,
|
|
54
|
+
context_changed_during_verify: 409,
|
|
55
|
+
index_changed_during_verify: 409,
|
|
56
|
+
retrieval_failed: 500,
|
|
57
|
+
chunk_load_failed: 500,
|
|
58
|
+
collection_load_failed: 500,
|
|
59
|
+
content_load_failed: 500,
|
|
60
|
+
context_load_failed: 500,
|
|
61
|
+
document_load_failed: 500,
|
|
62
|
+
index_snapshot_failed: 500,
|
|
63
|
+
runtime_error: 500,
|
|
64
|
+
} as const satisfies Record<ContextRestErrorCode, number>;
|
|
65
|
+
|
|
66
|
+
export const contextRestStatusForCode = (code: string): number =>
|
|
67
|
+
code in CONTEXT_REST_ERROR_STATUS
|
|
68
|
+
? CONTEXT_REST_ERROR_STATUS[code as ContextRestErrorCode]
|
|
69
|
+
: 500;
|
|
70
|
+
|
|
71
|
+
const errorResponse = (error: unknown): Response => {
|
|
72
|
+
const publicError = contextSurfaceError(error);
|
|
73
|
+
return new Response(JSON.stringify({ error: publicError }), {
|
|
74
|
+
status: contextRestStatusForCode(publicError.code),
|
|
75
|
+
headers: JSON_HEADERS,
|
|
76
|
+
});
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const handleContextBuild = async (
|
|
80
|
+
context: ServerContext,
|
|
81
|
+
request: Request
|
|
82
|
+
): Promise<Response> => {
|
|
83
|
+
try {
|
|
84
|
+
const { input, format } = parseContextBuildSurfaceInput(
|
|
85
|
+
await parseJsonBody(request),
|
|
86
|
+
context.indexName
|
|
87
|
+
);
|
|
88
|
+
validateContextCapsuleBuildInput(
|
|
89
|
+
input,
|
|
90
|
+
context.indexName,
|
|
91
|
+
context.config.collections.map((collection) => collection.name)
|
|
92
|
+
);
|
|
93
|
+
const capsule = await buildContextCapsule(input, {
|
|
94
|
+
store: context.store,
|
|
95
|
+
config: context.config,
|
|
96
|
+
indexName: context.indexName,
|
|
97
|
+
vectorIndex: context.vectorIndex,
|
|
98
|
+
embedPort: context.embedPort,
|
|
99
|
+
rerankPort: context.rerankPort,
|
|
100
|
+
});
|
|
101
|
+
return format === "md"
|
|
102
|
+
? new Response(formatContextCapsuleMarkdown(capsule), {
|
|
103
|
+
headers: MARKDOWN_HEADERS,
|
|
104
|
+
})
|
|
105
|
+
: new Response(canonicalBuiltContextCapsuleJson(capsule), {
|
|
106
|
+
headers: JSON_HEADERS,
|
|
107
|
+
});
|
|
108
|
+
} catch (error) {
|
|
109
|
+
return errorResponse(error);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const handleContextVerify = async (
|
|
114
|
+
context: ServerContext,
|
|
115
|
+
request: Request
|
|
116
|
+
): Promise<Response> => {
|
|
117
|
+
try {
|
|
118
|
+
const { capsule, format } = parseContextVerifySurfaceInput(
|
|
119
|
+
await parseJsonBody(request)
|
|
120
|
+
);
|
|
121
|
+
const receipt = await verifyContextCapsuleRuntime(capsule, {
|
|
122
|
+
store: context.store,
|
|
123
|
+
config: context.config,
|
|
124
|
+
indexName: context.indexName,
|
|
125
|
+
});
|
|
126
|
+
return format === "md"
|
|
127
|
+
? new Response(formatContextCapsuleVerificationMarkdown(receipt), {
|
|
128
|
+
headers: MARKDOWN_HEADERS,
|
|
129
|
+
})
|
|
130
|
+
: new Response(canonicalVerifiedContextCapsuleJson(receipt), {
|
|
131
|
+
headers: JSON_HEADERS,
|
|
132
|
+
});
|
|
133
|
+
} catch (error) {
|
|
134
|
+
return errorResponse(error);
|
|
135
|
+
}
|
|
136
|
+
};
|
package/src/serve/context.ts
CHANGED
|
@@ -19,6 +19,8 @@ import type { DocumentEventBus } from "./doc-events";
|
|
|
19
19
|
import type { EmbedScheduler } from "./embed-scheduler";
|
|
20
20
|
import type { CollectionWatchService } from "./watch-service";
|
|
21
21
|
|
|
22
|
+
import { DEFAULT_INDEX_NAME } from "../app/constants";
|
|
23
|
+
import { canonicalizeIndexName } from "../app/index-name";
|
|
22
24
|
import { LlmAdapter } from "../llm/nodeLlamaCpp/adapter";
|
|
23
25
|
import { resolveDownloadPolicy } from "../llm/policy";
|
|
24
26
|
import { getActivePreset } from "../llm/registry";
|
|
@@ -64,6 +66,8 @@ export function resetDownloadState(): void {
|
|
|
64
66
|
export interface ServerContext {
|
|
65
67
|
store: SqliteAdapter;
|
|
66
68
|
config: Config;
|
|
69
|
+
/** Canonical identity of the already-open resident store. */
|
|
70
|
+
indexName: string;
|
|
67
71
|
vectorIndex: VectorIndexPort | null;
|
|
68
72
|
embedPort: EmbeddingPort | null;
|
|
69
73
|
expandPort: GenerationPort | null;
|
|
@@ -82,6 +86,7 @@ export interface ServerContext {
|
|
|
82
86
|
|
|
83
87
|
export interface CreateServerContextOptions {
|
|
84
88
|
offline?: boolean;
|
|
89
|
+
indexName?: string;
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
/**
|
|
@@ -197,6 +202,7 @@ export async function createServerContext(
|
|
|
197
202
|
return {
|
|
198
203
|
store,
|
|
199
204
|
config,
|
|
205
|
+
indexName: canonicalizeIndexName(options.indexName ?? DEFAULT_INDEX_NAME),
|
|
200
206
|
vectorIndex,
|
|
201
207
|
embedPort,
|
|
202
208
|
expandPort,
|
|
@@ -242,5 +248,8 @@ export async function reloadServerContext(
|
|
|
242
248
|
options: CreateServerContextOptions = {}
|
|
243
249
|
): Promise<ServerContext> {
|
|
244
250
|
await disposeServerContext(ctx);
|
|
245
|
-
return createServerContext(ctx.store, newConfig ?? ctx.config,
|
|
251
|
+
return createServerContext(ctx.store, newConfig ?? ctx.config, {
|
|
252
|
+
...options,
|
|
253
|
+
indexName: options.indexName ?? ctx.indexName,
|
|
254
|
+
});
|
|
246
255
|
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import type { Database } from "bun:sqlite";
|
|
9
9
|
|
|
10
|
+
import type { ModelLease } from "../llm/nodeLlamaCpp/lifecycle";
|
|
10
11
|
import type { EmbeddingPort } from "../llm/types";
|
|
11
12
|
import type { VectorIndexPort } from "../store/vector";
|
|
12
13
|
|
|
@@ -46,6 +47,9 @@ export interface EmbedSchedulerDeps {
|
|
|
46
47
|
getVectorIndex: () => VectorIndexPort | null;
|
|
47
48
|
/** Getter for current model URI (survives preset changes) */
|
|
48
49
|
getModelUri: () => string;
|
|
50
|
+
acquireModelLease?: () => ModelLease;
|
|
51
|
+
onEmbedded?: (result: EmbedResult) => void;
|
|
52
|
+
embedBacklogFn?: typeof embedBacklog;
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
@@ -63,7 +67,7 @@ export interface EmbedScheduler {
|
|
|
63
67
|
getState(): EmbedSchedulerState;
|
|
64
68
|
|
|
65
69
|
/** Cleanup on server shutdown */
|
|
66
|
-
dispose(): void
|
|
70
|
+
dispose(): Promise<void>;
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
/**
|
|
@@ -71,7 +75,15 @@ export interface EmbedScheduler {
|
|
|
71
75
|
* Uses getters to resolve dependencies at execution time (survives context reloads).
|
|
72
76
|
*/
|
|
73
77
|
export function createEmbedScheduler(deps: EmbedSchedulerDeps): EmbedScheduler {
|
|
74
|
-
const {
|
|
78
|
+
const {
|
|
79
|
+
db,
|
|
80
|
+
getEmbedPort,
|
|
81
|
+
getVectorIndex,
|
|
82
|
+
getModelUri,
|
|
83
|
+
acquireModelLease,
|
|
84
|
+
onEmbedded,
|
|
85
|
+
embedBacklogFn = embedBacklog,
|
|
86
|
+
} = deps;
|
|
75
87
|
|
|
76
88
|
// State
|
|
77
89
|
let pendingCount = 0; // Track pending triggers (not actual docIds - we embed full backlog)
|
|
@@ -81,6 +93,7 @@ export function createEmbedScheduler(deps: EmbedSchedulerDeps): EmbedScheduler {
|
|
|
81
93
|
let firstPendingAt: number | null = null;
|
|
82
94
|
let nextRunAt: number | null = null; // Accurate timer due time
|
|
83
95
|
let disposed = false;
|
|
96
|
+
let currentRun: Promise<EmbedResult | null> | null = null;
|
|
84
97
|
let lastRunAt: number | null = null;
|
|
85
98
|
let lastResult: EmbedResult | null = null;
|
|
86
99
|
|
|
@@ -103,20 +116,25 @@ export function createEmbedScheduler(deps: EmbedSchedulerDeps): EmbedScheduler {
|
|
|
103
116
|
return { embedded: 0, errors: 0 };
|
|
104
117
|
}
|
|
105
118
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
119
|
+
const lease = acquireModelLease?.();
|
|
120
|
+
try {
|
|
121
|
+
const result = await embedBacklogFn({
|
|
122
|
+
statsPort: stats,
|
|
123
|
+
embedPort,
|
|
124
|
+
vectorIndex,
|
|
125
|
+
modelUri,
|
|
126
|
+
batchSize: BATCH_SIZE,
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
if (!result.ok) {
|
|
130
|
+
console.error("[embed-scheduler] Embed failed:", result.error.message);
|
|
131
|
+
return { embedded: 0, errors: 0 };
|
|
132
|
+
}
|
|
133
|
+
if (result.value.embedded > 0) onEmbedded?.(result.value);
|
|
134
|
+
return result.value;
|
|
135
|
+
} finally {
|
|
136
|
+
lease?.release();
|
|
117
137
|
}
|
|
118
|
-
|
|
119
|
-
return result.value;
|
|
120
138
|
}
|
|
121
139
|
|
|
122
140
|
/**
|
|
@@ -166,41 +184,53 @@ export function createEmbedScheduler(deps: EmbedSchedulerDeps): EmbedScheduler {
|
|
|
166
184
|
/**
|
|
167
185
|
* Execute the embed run with concurrency guard.
|
|
168
186
|
*/
|
|
169
|
-
|
|
187
|
+
function executeRun(): Promise<EmbedResult | null> {
|
|
170
188
|
if (disposed || running) {
|
|
171
189
|
needsRerun = true;
|
|
172
|
-
return null;
|
|
190
|
+
return Promise.resolve(null);
|
|
173
191
|
}
|
|
174
192
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
// Clear pending state at START so new notifications accumulate
|
|
180
|
-
pendingCount = 0;
|
|
181
|
-
firstPendingAt = null;
|
|
193
|
+
const operation = (async (): Promise<EmbedResult | null> => {
|
|
194
|
+
running = true;
|
|
195
|
+
timer = null;
|
|
196
|
+
nextRunAt = null;
|
|
182
197
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
198
|
+
// Clear pending state at START so new notifications accumulate
|
|
199
|
+
pendingCount = 0;
|
|
200
|
+
firstPendingAt = null;
|
|
201
|
+
|
|
202
|
+
let result: EmbedResult;
|
|
203
|
+
try {
|
|
204
|
+
result = await runEmbed();
|
|
205
|
+
lastRunAt = Date.now();
|
|
206
|
+
lastResult = result;
|
|
207
|
+
} finally {
|
|
208
|
+
running = false;
|
|
209
|
+
}
|
|
191
210
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
211
|
+
// Check if we need to rerun (notifications arrived while running)
|
|
212
|
+
// Must be AFTER running=false so scheduleRun() actually schedules
|
|
213
|
+
if ((needsRerun || pendingCount > 0) && !disposed) {
|
|
214
|
+
needsRerun = false;
|
|
215
|
+
// Set firstPendingAt if we have pending work
|
|
216
|
+
if (pendingCount > 0 && firstPendingAt === null) {
|
|
217
|
+
firstPendingAt = Date.now();
|
|
218
|
+
}
|
|
219
|
+
scheduleRun();
|
|
199
220
|
}
|
|
200
|
-
scheduleRun();
|
|
201
|
-
}
|
|
202
221
|
|
|
203
|
-
|
|
222
|
+
return result;
|
|
223
|
+
})();
|
|
224
|
+
currentRun = operation;
|
|
225
|
+
void operation.then(
|
|
226
|
+
() => {
|
|
227
|
+
if (currentRun === operation) currentRun = null;
|
|
228
|
+
},
|
|
229
|
+
() => {
|
|
230
|
+
if (currentRun === operation) currentRun = null;
|
|
231
|
+
}
|
|
232
|
+
);
|
|
233
|
+
return operation;
|
|
204
234
|
}
|
|
205
235
|
|
|
206
236
|
return {
|
|
@@ -263,13 +293,14 @@ export function createEmbedScheduler(deps: EmbedSchedulerDeps): EmbedScheduler {
|
|
|
263
293
|
return state;
|
|
264
294
|
},
|
|
265
295
|
|
|
266
|
-
dispose(): void {
|
|
296
|
+
async dispose(): Promise<void> {
|
|
267
297
|
disposed = true;
|
|
268
298
|
if (timer) {
|
|
269
299
|
clearTimeout(timer);
|
|
270
300
|
timer = null;
|
|
271
301
|
nextRunAt = null;
|
|
272
302
|
}
|
|
303
|
+
await currentRun;
|
|
273
304
|
},
|
|
274
305
|
};
|
|
275
306
|
}
|
package/src/serve/index.ts
CHANGED
|
@@ -11,3 +11,12 @@ export {
|
|
|
11
11
|
type BackgroundRuntimeResult,
|
|
12
12
|
startBackgroundRuntime,
|
|
13
13
|
} from "./background-runtime";
|
|
14
|
+
export {
|
|
15
|
+
type ResidentGeneration,
|
|
16
|
+
type ResidentMode,
|
|
17
|
+
type ResidentRequestHandle,
|
|
18
|
+
type ResidentRuntime,
|
|
19
|
+
type ResidentRuntimeOptions,
|
|
20
|
+
type ResidentRuntimeResult,
|
|
21
|
+
startResidentRuntime,
|
|
22
|
+
} from "./resident-runtime";
|