@vite-hub/source 0.0.2 → 0.0.3

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 CHANGED
@@ -31,6 +31,38 @@ const keys = await docs.keys()
31
31
  const first = await docs.read(keys[0]!)
32
32
  ```
33
33
 
34
+ Structured sources preserve record and metadata types through `useSource()`:
35
+
36
+ ```ts
37
+ import type { Source } from "@vite-hub/source"
38
+
39
+ interface Article {
40
+ author: string
41
+ cover: { key: string, mediaType: string }
42
+ title: string
43
+ }
44
+
45
+ interface ArticleMetadata {
46
+ revision: string
47
+ }
48
+
49
+ declare global {
50
+ interface ViteHubSourceMap {
51
+ articles: Source<`article_${string}`, Article, ArticleMetadata>
52
+ }
53
+ }
54
+
55
+ const article = await useSource("articles").get("article_123")
56
+ article.data?.title
57
+ article.metadata?.revision
58
+
59
+ const articles = await useSource("articles").items()
60
+ ```
61
+
62
+ Keep binary assets behind the Blob boundary and store a serializable reference in
63
+ the record. This keeps ordinary record reads lazy while treating the structured
64
+ data and its assets as one logical item.
65
+
34
66
  ## Used by
35
67
 
36
68
  [`@vite-hub/workspace`](../workspace/README.md) materializes Source output into workspace files. Use this package directly when you only need typed source loading and not a workspace file tree.
package/dist/index.d.ts CHANGED
@@ -1,13 +1,89 @@
1
- import { a as SourceContent, c as SourceKey, d as SourceName, f as SourceReader, i as SourceCacheOptions, l as SourceListEntry, m as SourceSearchQuery, n as ReadSourceResult, o as SourceContext, p as SourceSearchHit, r as Source, s as SourceItem, t as ReadSourceOptions, u as SourceMap } from "./types-f6Lyjq8g.js";
2
- import { t as custom } from "./custom-EnZvMLiy.js";
3
- import { i as file, n as FileSourceOptions } from "./file-slFAccIZ.js";
4
- import { n as GitHubSourceOptions, t as github } from "./index-CJpN2j6c.js";
5
- import { n as glob, t as GlobSourceOptions } from "./glob-CuxZwoC3.js";
6
- import { t as markdown } from "./markdown-Beo-0cqc.js";
7
- import { a as McpResourcesRequestOptions, i as McpResourcesClientConfig, l as mcpResources, n as McpResourceDescriptor, o as McpResourcesServer, r as McpResourcesClient, s as McpResourcesSourceOptions, t as McpResourceContent } from "./mcp-resources-C7-5Qeh4.js";
8
- import { t as index_d_exports } from "./sources/index.js";
9
1
  import { ViteHubError } from "@vite-hub/runtime";
2
+ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
3
+ import { SSEClientTransportOptions } from "@modelcontextprotocol/sdk/client/sse.js";
4
+ import { StreamableHTTPClientTransportOptions } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
5
+ import { RequestOptions } from "@modelcontextprotocol/sdk/shared/protocol.js";
6
+ import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
7
+ import { BlobResourceContents, Resource, TextResourceContents } from "@modelcontextprotocol/sdk/types.js";
10
8
 
9
+ //#region src/core/types.d.ts
10
+ type SourceContent = string | Uint8Array;
11
+ interface ReadSourceOptions {
12
+ encoding?: "utf8" | "binary";
13
+ }
14
+ type ReadSourceResult<TOptions extends ReadSourceOptions | undefined = undefined> = TOptions extends {
15
+ encoding: "binary";
16
+ } ? Uint8Array : string;
17
+ interface SourceContext {
18
+ abortSignal?: AbortSignal;
19
+ rootDir: string;
20
+ sourceRootDir?: string;
21
+ source?: string;
22
+ workspace?: string;
23
+ }
24
+ interface SourceCacheOptions {
25
+ maxAge?: number;
26
+ }
27
+ interface SourceSearchQuery {
28
+ pattern: string;
29
+ cwd?: string;
30
+ paths?: string[];
31
+ regex?: boolean;
32
+ caseSensitive?: boolean;
33
+ limit?: number;
34
+ }
35
+ interface SourceSearchHit {
36
+ path: string;
37
+ line: number;
38
+ column: number;
39
+ text: string;
40
+ }
41
+ interface SourceItem<TKey extends string = string, TData = unknown, TMetadata extends object = Record<string, unknown>> {
42
+ key: TKey;
43
+ path?: string;
44
+ content?: SourceContent;
45
+ data?: TData;
46
+ mediaType?: string;
47
+ metadata?: TMetadata;
48
+ }
49
+ interface Source<TKey extends string = string, TData = unknown, TMetadata extends object = Record<string, unknown>> {
50
+ name: string;
51
+ cache?: false | SourceCacheOptions;
52
+ fingerprint?: unknown;
53
+ prepare?(ctx: SourceContext): Promise<void>;
54
+ getKeys(ctx: SourceContext): Promise<TKey[]>;
55
+ getItem(key: TKey, ctx: SourceContext): Promise<SourceItem<TKey, TData, TMetadata>>;
56
+ getItems?(ctx: SourceContext): Promise<SourceItem<TKey, TData, TMetadata>[]>;
57
+ getMeta?(key: TKey, ctx: SourceContext): Promise<TMetadata | undefined>;
58
+ search?(query: SourceSearchQuery, ctx: SourceContext): Promise<SourceSearchHit[]>;
59
+ watch?: unknown[];
60
+ }
61
+ interface SourceListEntry<TKey extends string = string> {
62
+ key: TKey;
63
+ type: "file" | "directory";
64
+ }
65
+ declare global {
66
+ interface ViteHubSourceMap {}
67
+ }
68
+ interface SourceMap extends ViteHubSourceMap {}
69
+ type SourceKeyOf<TSource> = TSource extends Source<infer TKey, any, any> ? TKey : string;
70
+ type SourceDataOf<TSource> = TSource extends Source<any, infer TData, any> ? TData : unknown;
71
+ type SourceMetadataOf<TSource> = TSource extends Source<any, any, infer TMetadata> ? TMetadata : Record<string, unknown>;
72
+ type RegisteredSource<TName extends SourceName> = TName extends keyof ViteHubSourceMap ? ViteHubSourceMap[TName] : Source;
73
+ type SourceName = [keyof ViteHubSourceMap] extends [never] ? string : Extract<keyof ViteHubSourceMap, string>;
74
+ type SourceKey<TName extends SourceName = SourceName> = Extract<SourceKeyOf<RegisteredSource<TName>>, string>;
75
+ type SourceData<TName extends SourceName = SourceName> = SourceDataOf<RegisteredSource<TName>>;
76
+ type SourceMetadata<TName extends SourceName = SourceName> = SourceMetadataOf<RegisteredSource<TName>>;
77
+ interface SourceReader<TName extends SourceName = SourceName> {
78
+ keys(): Promise<SourceKey<TName>[]>;
79
+ get(key: SourceKey<TName>): Promise<SourceItem<SourceKey<TName>, SourceData<TName>, SourceMetadata<TName>>>;
80
+ items(): Promise<Array<SourceItem<SourceKey<TName>, SourceData<TName>, SourceMetadata<TName>>>>;
81
+ read<TOptions extends ReadSourceOptions | undefined = undefined>(key: SourceKey<TName>, options?: TOptions): Promise<ReadSourceResult<TOptions>>;
82
+ meta(key: SourceKey<TName>): Promise<SourceMetadata<TName> | undefined>;
83
+ exists(key: SourceKey<TName>): Promise<boolean>;
84
+ list(prefix?: SourceKey<TName> | (string & {}) | ""): Promise<SourceListEntry<SourceKey<TName>>[]>;
85
+ }
86
+ //#endregion
11
87
  //#region src/core/define.d.ts
12
88
  declare function defineSource<const TSource extends Source>(source: TSource): TSource;
13
89
  declare function defineSources<const TSources extends Record<string, Source>>(sources: TSources): TSources;
@@ -15,11 +91,92 @@ declare function defineSources<const TSources extends Record<string, Source>>(so
15
91
  //#region src/core/registry.d.ts
16
92
  declare function registerSource<const TName extends string, const TSource extends Source>(name: TName, source: TSource): TSource;
17
93
  declare function registerSources<const TSources extends Record<string, Source>>(sources: TSources): TSources;
18
- declare function getRegisteredSource<TName extends SourceName>(name: TName): Source<SourceKey<TName>>;
94
+ declare function getRegisteredSource<TName extends SourceName>(name: TName): Source<SourceKey<TName>, SourceData<TName>, SourceMetadata<TName>>;
19
95
  declare function clearSources(): void;
20
96
  declare function useSource<TName extends SourceName>(name: TName, context?: Partial<SourceContext>): SourceReader<TName>;
21
97
  //#endregion
22
98
  //#region src/core/errors.d.ts
23
99
  type SourceErrorCode = "SOURCE_FAILED" | "SOURCE_NOT_FOUND" | "SOURCE_PATH_INVALID";
24
100
  //#endregion
25
- export { type FileSourceOptions, type GitHubSourceOptions, type GlobSourceOptions, type McpResourceContent, type McpResourceDescriptor, type McpResourcesClient, type McpResourcesClientConfig, type McpResourcesRequestOptions, type McpResourcesServer, type McpResourcesSourceOptions, type ReadSourceOptions, type ReadSourceResult, type Source, type SourceCacheOptions, type SourceContent, type SourceContext, type SourceErrorCode, type SourceItem, type SourceKey, type SourceListEntry, type SourceMap, type SourceName, type SourceReader, type SourceSearchHit, type SourceSearchQuery, clearSources, custom, defineSource, defineSources, file, getRegisteredSource, github, glob, markdown, mcpResources, registerSource, registerSources, index_d_exports as source, useSource };
101
+ //#region src/sources/custom.d.ts
102
+ declare function custom<const TSource extends Source>(source: TSource): TSource;
103
+ //#endregion
104
+ //#region src/sources/file.d.ts
105
+ interface FileSourcePathOptions<TKey extends string = string> {
106
+ path: string;
107
+ workspacePath?: TKey;
108
+ content?: never;
109
+ mediaType?: string;
110
+ }
111
+ interface FileSourceInlineOptions<TKey extends string = string> {
112
+ workspacePath: TKey;
113
+ content: SourceContent;
114
+ mediaType?: string;
115
+ path?: never;
116
+ }
117
+ type FileSourceOptions<TKey extends string = string> = FileSourcePathOptions<TKey> | FileSourceInlineOptions<TKey>;
118
+ type FileSourceInput<TKey extends string = string> = FileSourceOptions<TKey> | TKey;
119
+ declare function file<const TKey extends string = string>(input: FileSourceInput<TKey>): Source<TKey>;
120
+ //#endregion
121
+ //#region src/sources/github/types.d.ts
122
+ interface GitHubSourceOptions {
123
+ repo: string;
124
+ ref?: string;
125
+ root?: string;
126
+ auth?: false | string | (() => string | undefined);
127
+ include?: string | string[];
128
+ exclude?: string | string[];
129
+ cache?: false | SourceCacheOptions;
130
+ }
131
+ //#endregion
132
+ //#region src/sources/github/index.d.ts
133
+ declare function github<const TKey extends string = string>(options: GitHubSourceOptions): Source<TKey>;
134
+ //#endregion
135
+ //#region src/sources/glob.d.ts
136
+ interface GlobSourceOptions {
137
+ cwd?: string;
138
+ dot?: boolean;
139
+ followSymlinks?: boolean;
140
+ include: string | string[];
141
+ ignore?: string | string[];
142
+ keyCache?: boolean;
143
+ prefix?: string;
144
+ }
145
+ declare function glob<const TKey extends string = string>(options: GlobSourceOptions): Source<TKey>;
146
+ //#endregion
147
+ //#region src/sources/markdown.d.ts
148
+ declare function markdown<const TKey extends string = string>(options: FileSourceOptions<TKey>): Source<TKey>;
149
+ //#endregion
150
+ //#region src/sources/mcp-resources.d.ts
151
+ type McpResourcesRequestOptions = RequestOptions;
152
+ type McpResourceDescriptor = Resource;
153
+ type McpResourceContent = TextResourceContents | BlobResourceContents;
154
+ interface McpResourcesClient {
155
+ close?: () => void | Promise<void>;
156
+ getServerVersion?: Client["getServerVersion"];
157
+ listResources: Client["listResources"];
158
+ readResource: Client["readResource"];
159
+ serverInfo?: unknown;
160
+ }
161
+ type McpResourcesTransportConfig = Transport | ({
162
+ type?: "http";
163
+ url: string | URL;
164
+ } & StreamableHTTPClientTransportOptions) | ({
165
+ type: "sse";
166
+ url: string | URL;
167
+ } & SSEClientTransportOptions);
168
+ interface McpResourcesClientConfig {
169
+ transport: McpResourcesTransportConfig;
170
+ }
171
+ type McpResourcesServer = McpResourcesClient | McpResourcesClientConfig | ((ctx: SourceContext) => McpResourcesClient | McpResourcesClientConfig | Promise<McpResourcesClient | McpResourcesClientConfig>);
172
+ interface McpResourcesSourceOptions<TKey extends string = string> {
173
+ cache?: false | SourceCacheOptions;
174
+ exclude?: string | string[];
175
+ include?: string | string[];
176
+ path?: (resource: McpResourceDescriptor) => TKey | string | undefined;
177
+ request?: McpResourcesRequestOptions;
178
+ server: McpResourcesServer;
179
+ }
180
+ declare function mcpResources<const TKey extends string = string>(options: McpResourcesSourceOptions<TKey>): Source<TKey>;
181
+ //#endregion
182
+ export { type FileSourceInlineOptions, type FileSourceOptions, type FileSourcePathOptions, type GitHubSourceOptions, type GlobSourceOptions, type McpResourceContent, type McpResourceDescriptor, type McpResourcesClient, type McpResourcesClientConfig, type McpResourcesRequestOptions, type McpResourcesServer, type McpResourcesSourceOptions, type McpResourcesTransportConfig, type ReadSourceOptions, type ReadSourceResult, type Source, type SourceCacheOptions, type SourceContent, type SourceContext, type SourceData, type SourceErrorCode, type SourceItem, type SourceKey, type SourceListEntry, type SourceMap, type SourceMetadata, type SourceName, type SourceReader, type SourceSearchHit, type SourceSearchQuery, clearSources, custom, defineSource, defineSources, file, getRegisteredSource, github, glob, markdown, mcpResources, registerSource, registerSources, useSource };