Skip to content

Commit fe4d6a4

Browse files
authored
Merge pull request #239 from takker99:smart-context
feat(api): add smart context endpoints for 1-hop and 2-hop links
2 parents ce95916 + fa9d801 commit fe4d6a4

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./api/pages.ts";
22
export * from "./api/projects.ts";
33
export * from "./api/users.ts";
4+
export * from "./api/smart-context.ts";
45
export type { HTTPError, TypedError } from "./error.ts";
56
export type { BaseOptions, ExtendedOptions, OAuthOptions } from "./util.ts";

api/smart-context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./smart-context/export-1hop-links/project.ts";
2+
export * from "./smart-context/export-2hop-links/project.ts";
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type {
2+
BadRequestError,
3+
NotFoundError,
4+
NotLoggedInError,
5+
NotMemberError,
6+
} from "@cosense/types/rest";
7+
import { type BaseOptions, setDefaults } from "../../../util.ts";
8+
import { cookie } from "../../../rest/auth.ts";
9+
import type { ResponseOfEndpoint } from "../../../targeted_response.ts";
10+
11+
/** Options for {@linkcode export1HopLinks}
12+
*
13+
* @experimental **UNSTABLE**: New API, yet to be vetted.
14+
*/
15+
export type Export1HopLinksOptions<R extends Response | undefined> =
16+
BaseOptions<R>;
17+
18+
/** Constructs a request for the `/api/smart-context/export-1hop-links/:project.txt` endpoint
19+
*
20+
* @experimental **UNSTABLE**: New API, yet to be vetted.
21+
*
22+
* @param project The project name to export 1-hop links for
23+
* @param title The title of the page to export 1-hop links for
24+
* @param options - Configuration options
25+
* @returns A {@linkcode Request} object for the API endpoint
26+
*/
27+
export const makeExport1HopLinksRequest = <R extends Response | undefined>(
28+
project: string,
29+
title: string,
30+
options?: Export1HopLinksOptions<R>,
31+
): Request => {
32+
const { sid, baseURL } = setDefaults(options ?? {});
33+
const params = new URLSearchParams({ title: title });
34+
35+
return new Request(
36+
`${baseURL}api/smart-context/export-1hop-links/${project}.txt?${params}`,
37+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
38+
);
39+
};
40+
41+
/** Exports 1-hop links for a given page in a project as AI-readable text format.
42+
*
43+
* @experimental **UNSTABLE**: New API, yet to be vetted.
44+
*
45+
* @param project The project name to export 1-hop links for
46+
* @param title The title of the page to export 1-hop links for
47+
* @param options - Configuration options. **Make sure to set `sid` or you will never get the 200 OK response.**
48+
*/
49+
export const export1HopLinks = <R extends Response | undefined = Response>(
50+
project: string,
51+
title: string,
52+
options?: Export1HopLinksOptions<R>,
53+
): Promise<
54+
ResponseOfEndpoint<{
55+
200: string;
56+
404: NotFoundError;
57+
400: BadRequestError;
58+
401: NotLoggedInError;
59+
403: NotMemberError;
60+
}, R>
61+
> =>
62+
setDefaults(options ?? {}).fetch(
63+
makeExport1HopLinksRequest(project, title, options),
64+
) as Promise<
65+
ResponseOfEndpoint<{
66+
200: string;
67+
400: BadRequestError;
68+
404: NotFoundError;
69+
401: NotLoggedInError;
70+
403: NotMemberError;
71+
}, R>
72+
>;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type {
2+
BadRequestError,
3+
NotFoundError,
4+
NotLoggedInError,
5+
NotMemberError,
6+
} from "@cosense/types/rest";
7+
import { type BaseOptions, setDefaults } from "../../../util.ts";
8+
import { cookie } from "../../../rest/auth.ts";
9+
import type { ResponseOfEndpoint } from "../../../targeted_response.ts";
10+
11+
/** Options for {@linkcode export2HopLinks}
12+
*
13+
* @experimental **UNSTABLE**: New API, yet to be vetted.
14+
*/
15+
export type Export2HopLinksOptions<R extends Response | undefined> =
16+
BaseOptions<R>;
17+
18+
/** Constructs a request for the `/api/smart-context/export-2hop-links/:project.txt` endpoint
19+
*
20+
* @experimental **UNSTABLE**: New API, yet to be vetted.
21+
*
22+
* @param project The project name to export 2-hop links for
23+
* @param title The title of the page to export 2-hop links for
24+
* @param options - Configuration options
25+
* @returns A {@linkcode Request} object for the API endpoint
26+
*/
27+
export const makeExport2HopLinksRequest = <R extends Response | undefined>(
28+
project: string,
29+
title: string,
30+
options?: Export2HopLinksOptions<R>,
31+
): Request => {
32+
const { sid, baseURL } = setDefaults(options ?? {});
33+
const params = new URLSearchParams({ title: title });
34+
35+
return new Request(
36+
`${baseURL}api/smart-context/export-2hop-links/${project}.txt?${params}`,
37+
sid ? { headers: { Cookie: cookie(sid) } } : undefined,
38+
);
39+
};
40+
41+
/** Exports 2-hop links for a given page in a project as AI-readable text format.
42+
*
43+
* @experimental **UNSTABLE**: New API, yet to be vetted.
44+
*
45+
* @param project The project name to export 2-hop links for
46+
* @param title The title of the page to export 2-hop links for
47+
* @param options - Configuration options. **Make sure to set `sid` or you will never get the 200 OK response.**
48+
*/
49+
export const export2HopLinks = <R extends Response | undefined = Response>(
50+
project: string,
51+
title: string,
52+
options?: Export2HopLinksOptions<R>,
53+
): Promise<
54+
ResponseOfEndpoint<{
55+
200: string;
56+
404: NotFoundError;
57+
400: BadRequestError;
58+
401: NotLoggedInError;
59+
403: NotMemberError;
60+
}, R>
61+
> =>
62+
setDefaults(options ?? {}).fetch(
63+
makeExport2HopLinksRequest(project, title, options),
64+
) as Promise<
65+
ResponseOfEndpoint<{
66+
200: string;
67+
400: BadRequestError;
68+
404: NotFoundError;
69+
401: NotLoggedInError;
70+
403: NotMemberError;
71+
}, R>
72+
>;

deno.jsonc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
"./unstable-api/pages/projects/project": "./api/projects/project.ts",
3535
"./unstable-api/pages/project/title/text": "./api/pages/project/title/text.ts",
3636
"./unstable-api/pages/project/title/icon": "./api/pages/project/title/icon.ts",
37+
"./unstable-api/pages/smart-context": "./api/smart-context.ts",
38+
"./unstable-api/pages/smart-context/export-1hop-links": "./api/smart-context/export-1hop-links/project.ts",
39+
"./unstable-api/pages/smart-context/export-2hop-links": "./api/smart-context/export-2hop-links/project.ts",
3740
"./unstable-api/users": "./api/users.ts",
3841
"./unstable-api/users/me": "./api/users/me.ts"
3942
},

0 commit comments

Comments
 (0)