@yaks/connections 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +609 -2
- package/_dist/authorize.d.ts +38 -0
- package/_dist/authorize.d.ts.map +1 -0
- package/_dist/authorize_cli.d.ts +12 -0
- package/_dist/authorize_cli.d.ts.map +1 -0
- package/_dist/brokers.d.ts +66 -0
- package/_dist/brokers.d.ts.map +1 -0
- package/_dist/catalog.d.ts +33 -0
- package/_dist/catalog.d.ts.map +1 -0
- package/_dist/cli.d.ts +3 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/clients.d.ts +18 -0
- package/_dist/clients.d.ts.map +1 -0
- package/_dist/connections.d.ts +296 -0
- package/_dist/connections.d.ts.map +1 -0
- package/_dist/effects.d.ts +15 -0
- package/_dist/effects.d.ts.map +1 -0
- package/_dist/field.d.ts +17 -0
- package/_dist/field.d.ts.map +1 -0
- package/_dist/form.d.ts +49 -0
- package/_dist/form.d.ts.map +1 -0
- package/_dist/given.d.ts +10 -0
- package/_dist/given.d.ts.map +1 -0
- package/_dist/graph.d.ts +15 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/history.d.ts +4 -0
- package/_dist/history.d.ts.map +1 -0
- package/_dist/identity.d.ts +9 -0
- package/_dist/identity.d.ts.map +1 -0
- package/_dist/integrations.d.ts +196 -0
- package/_dist/integrations.d.ts.map +1 -0
- package/_dist/loopback.d.ts +7 -0
- package/_dist/loopback.d.ts.map +1 -0
- package/_dist/mcp-retirement.d.ts +33 -0
- package/_dist/mcp-retirement.d.ts.map +1 -0
- package/_dist/mcp.d.ts +179 -0
- package/_dist/mcp.d.ts.map +1 -0
- package/_dist/mod.d.ts +70 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/paste.d.ts +13 -0
- package/_dist/paste.d.ts.map +1 -0
- package/_dist/requests.d.ts +30 -0
- package/_dist/requests.d.ts.map +1 -0
- package/_dist/services.d.ts +25 -0
- package/_dist/services.d.ts.map +1 -0
- package/_dist/sign-in.d.ts +9 -0
- package/_dist/sign-in.d.ts.map +1 -0
- package/_dist/signin-mail.d.ts +12 -0
- package/_dist/signin-mail.d.ts.map +1 -0
- package/_dist/signin.d.ts +19 -0
- package/_dist/signin.d.ts.map +1 -0
- package/_dist/sources.d.ts +61 -0
- package/_dist/sources.d.ts.map +1 -0
- package/_dist/tools.d.ts +3 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/views.d.ts +3 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/yaks-app.d.ts +29 -0
- package/_dist/yaks-app.d.ts.map +1 -0
- package/anthropic.json +12 -0
- package/auth_command.js +69 -0
- package/auth_command.js.map +1 -0
- package/auth_command.ts +74 -0
- package/authorize.js +89 -0
- package/authorize.js.map +1 -0
- package/authorize.ts +138 -0
- package/authorize_cli.js +167 -0
- package/authorize_cli.js.map +1 -0
- package/authorize_cli.ts +211 -0
- package/brokers.js +318 -0
- package/brokers.js.map +1 -0
- package/brokers.ts +423 -0
- package/canva.json +32 -0
- package/catalog.js +178 -0
- package/catalog.js.map +1 -0
- package/catalog.ts +216 -0
- package/cli.js +30 -0
- package/cli.js.map +1 -0
- package/cli.ts +26 -0
- package/clients.js +34 -0
- package/clients.js.map +1 -0
- package/clients.ts +65 -0
- package/connections.js +1961 -0
- package/connections.js.map +1 -0
- package/connections.ts +2561 -0
- package/deno.json +1 -0
- package/discord-account.json +22 -0
- package/discord-bot.json +20 -0
- package/discord.json +26 -0
- package/effects.js +168 -0
- package/effects.js.map +1 -0
- package/effects.ts +160 -0
- package/elevenlabs.json +18 -0
- package/facebook.json +18 -0
- package/field.js +19 -0
- package/field.js.map +1 -0
- package/field.ts +31 -0
- package/form.js +112 -0
- package/form.js.map +1 -0
- package/form.ts +178 -0
- package/git-config.js +44 -0
- package/git-config.js.map +1 -0
- package/git-config.ts +26 -0
- package/given.js +83 -0
- package/given.js.map +1 -0
- package/given.ts +88 -0
- package/gmail-mailbox.json +29 -0
- package/gmail.json +31 -0
- package/google-calendar.json +31 -0
- package/google-drive.json +31 -0
- package/google-search-console.json +29 -0
- package/graph.js +36 -0
- package/graph.js.map +1 -0
- package/graph.ts +52 -0
- package/history.js +31 -0
- package/history.js.map +1 -0
- package/history.ts +32 -0
- package/identity.js +15 -0
- package/identity.js.map +1 -0
- package/identity.ts +18 -0
- package/integrations.js +493 -0
- package/integrations.js.map +1 -0
- package/integrations.ts +725 -0
- package/loopback.js +72 -0
- package/loopback.js.map +1 -0
- package/loopback.ts +89 -0
- package/lulu.json +32 -0
- package/mcp-retirement.js +133 -0
- package/mcp-retirement.js.map +1 -0
- package/mcp-retirement.ts +200 -0
- package/mcp-signin.js +14 -0
- package/mcp-signin.js.map +1 -0
- package/mcp-signin.ts +13 -0
- package/mcp.js +1036 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +1283 -0
- package/mod.js +70 -0
- package/mod.js.map +1 -0
- package/mod.ts +216 -0
- package/openai-api.json +12 -0
- package/openai.json +25 -0
- package/openrouter.json +14 -0
- package/package.json +89 -4
- package/paste.js +36 -0
- package/paste.js.map +1 -0
- package/paste.ts +55 -0
- package/requests.js +137 -0
- package/requests.js.map +1 -0
- package/requests.ts +200 -0
- package/services.js +84 -0
- package/services.js.map +1 -0
- package/services.ts +111 -0
- package/sign-in.js +36 -0
- package/sign-in.js.map +1 -0
- package/sign-in.ts +53 -0
- package/signin-mail.js +29 -0
- package/signin-mail.js.map +1 -0
- package/signin-mail.ts +56 -0
- package/signin.js +68 -0
- package/signin.js.map +1 -0
- package/signin.ts +109 -0
- package/slack.json +90 -0
- package/sources.js +178 -0
- package/sources.js.map +1 -0
- package/sources.ts +237 -0
- package/tools.js +39 -0
- package/tools.js.map +1 -0
- package/tools.ts +46 -0
- package/views.js +85 -0
- package/views.js.map +1 -0
- package/views.ts +89 -0
- package/visitor-defaults.json +8 -0
- package/vocab.js +17 -0
- package/vocab.js.map +1 -0
- package/vocab.json +863 -0
- package/vocab.ts +18 -0
- package/voipms.json +32 -0
- package/yaks-app.js +159 -0
- package/yaks-app.js.map +1 -0
- package/yaks-app.ts +177 -0
- package/youtube.json +26 -0
package/signin.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/** Private sign-in attempts; completed grants belong to connections in the vault. */
|
|
2
|
+
import {
|
|
3
|
+
begin,
|
|
4
|
+
connect,
|
|
5
|
+
credential,
|
|
6
|
+
type Ctx,
|
|
7
|
+
need,
|
|
8
|
+
pick,
|
|
9
|
+
refresh,
|
|
10
|
+
settle,
|
|
11
|
+
} from './connections.js'
|
|
12
|
+
import { CONNECTION } from './identity.js'
|
|
13
|
+
import type { Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import type { Vault } from 'jsr:@yaks/secrets@0.2.36'
|
|
15
|
+
import type { Attempt } from 'jsr:@yaks/oauth@0.2.36'
|
|
16
|
+
|
|
17
|
+
/** The registered browser return address for a local sign-in. */
|
|
18
|
+
export const REDIRECT = 'http://localhost:8765/oauth/callback'
|
|
19
|
+
|
|
20
|
+
export type SignIns = {
|
|
21
|
+
key: (owner: Eid, integration: string) => Promise<string | undefined>
|
|
22
|
+
refresh: (
|
|
23
|
+
owner: Eid,
|
|
24
|
+
integration: string,
|
|
25
|
+
stale: string,
|
|
26
|
+
) => Promise<string | undefined>
|
|
27
|
+
begin: (
|
|
28
|
+
owner: Eid,
|
|
29
|
+
integration: string,
|
|
30
|
+
redirect?: string,
|
|
31
|
+
) => Promise<{ url: string; redirectUrl: string }>
|
|
32
|
+
complete: (
|
|
33
|
+
owner: Eid,
|
|
34
|
+
integration: string,
|
|
35
|
+
callback: string,
|
|
36
|
+
session?: string,
|
|
37
|
+
) => Promise<void>
|
|
38
|
+
cancel: (owner?: Eid) => void
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const signins = (h: {
|
|
42
|
+
g: Graph
|
|
43
|
+
vault: Vault
|
|
44
|
+
fetch?: typeof fetch
|
|
45
|
+
}): SignIns => {
|
|
46
|
+
const ctx = (redirect: string): Ctx => ({
|
|
47
|
+
graph: h.g,
|
|
48
|
+
vault: h.vault,
|
|
49
|
+
fetch: h.fetch,
|
|
50
|
+
redirect,
|
|
51
|
+
})
|
|
52
|
+
const pending = new Map<
|
|
53
|
+
string,
|
|
54
|
+
{ eid: Eid; attempt?: Attempt; redirect: string }
|
|
55
|
+
>()
|
|
56
|
+
const slot = (owner: Eid, integration: string) =>
|
|
57
|
+
JSON.stringify([owner, integration])
|
|
58
|
+
const held = async (owner: Eid, integration: string) =>
|
|
59
|
+
(await pick(h.g.read, owner, integration))?.entity.eid
|
|
60
|
+
return {
|
|
61
|
+
/** The credential `owner` signed in with, once it has. */
|
|
62
|
+
key: async (owner: Eid, integration: string) => {
|
|
63
|
+
const eid = await held(owner, integration)
|
|
64
|
+
return eid ? await credential(ctx(REDIRECT), eid) : undefined
|
|
65
|
+
},
|
|
66
|
+
/** Rotate a rejected bearer unless another caller already replaced it. */
|
|
67
|
+
refresh: async (owner: Eid, integration: string, stale: string) => {
|
|
68
|
+
const eid = await held(owner, integration)
|
|
69
|
+
return eid ? await refresh(ctx(REDIRECT), eid, stale) : undefined
|
|
70
|
+
},
|
|
71
|
+
begin: async (owner: Eid, integration: string, redirect = REDIRECT) => {
|
|
72
|
+
const eid =
|
|
73
|
+
(await h.g.apply(await need(h.g.read, { owner, integration })))
|
|
74
|
+
.find((b) => b[CONNECTION])!.entity.eid
|
|
75
|
+
const { url, attempt } = await begin(ctx(redirect), eid)
|
|
76
|
+
pending.set(slot(owner, integration), { eid, attempt, redirect })
|
|
77
|
+
return { url, redirectUrl: redirect }
|
|
78
|
+
},
|
|
79
|
+
complete: async (
|
|
80
|
+
owner: Eid,
|
|
81
|
+
integration: string,
|
|
82
|
+
callback: string,
|
|
83
|
+
session?: string,
|
|
84
|
+
) => {
|
|
85
|
+
const was = pending.get(slot(owner, integration))
|
|
86
|
+
if (!was) {
|
|
87
|
+
throw new Error('Authorization expired or not started; begin again')
|
|
88
|
+
}
|
|
89
|
+
// A brokered sign-in returns to its broker, which holds the grant.
|
|
90
|
+
if (!was.attempt) {
|
|
91
|
+
let made = await settle(ctx(was.redirect), was.eid)
|
|
92
|
+
if (!made.length) throw new Error('Finish signing in at the link first')
|
|
93
|
+
} else {
|
|
94
|
+
await connect(ctx(was.redirect), was.eid, {
|
|
95
|
+
attempt: was.attempt,
|
|
96
|
+
callback: callback.trim(),
|
|
97
|
+
session,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
pending.delete(slot(owner, integration))
|
|
101
|
+
},
|
|
102
|
+
cancel: (owner?: Eid) => {
|
|
103
|
+
if (!owner) return pending.clear()
|
|
104
|
+
for (const key of pending.keys()) {
|
|
105
|
+
if (JSON.parse(key)[0] == owner) pending.delete(key)
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
package/slack.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "slack",
|
|
3
|
+
"visitors": "never",
|
|
4
|
+
"title": "Slack",
|
|
5
|
+
"tagline": "Talk to your Operator in Slack. Mention it in a channel or send it a direct message.",
|
|
6
|
+
"site": "https://slack.com",
|
|
7
|
+
"image": "https://app.nango.dev/images/template-logos/slack.svg",
|
|
8
|
+
"authorize": "https://slack.com/oauth/v2/authorize",
|
|
9
|
+
"token": "https://slack.com/api/oauth.v2.access",
|
|
10
|
+
"client": "slack",
|
|
11
|
+
"pkce": false,
|
|
12
|
+
"scope_separator": ",",
|
|
13
|
+
"account": "team.id",
|
|
14
|
+
"account_title": "team.name",
|
|
15
|
+
"chat": "slack",
|
|
16
|
+
"chat_grant": true,
|
|
17
|
+
"hosts": [
|
|
18
|
+
"slack.com"
|
|
19
|
+
],
|
|
20
|
+
"scopes": [
|
|
21
|
+
"app_mentions:read",
|
|
22
|
+
"bookmarks:read",
|
|
23
|
+
"bookmarks:write",
|
|
24
|
+
"calls:read",
|
|
25
|
+
"calls:write",
|
|
26
|
+
"canvases:read",
|
|
27
|
+
"canvases:write",
|
|
28
|
+
"channels:history",
|
|
29
|
+
"channels:join",
|
|
30
|
+
"channels:manage",
|
|
31
|
+
"channels:read",
|
|
32
|
+
"channels:write.invites",
|
|
33
|
+
"channels:write.topic",
|
|
34
|
+
"chat:write",
|
|
35
|
+
"chat:write.customize",
|
|
36
|
+
"chat:write.public",
|
|
37
|
+
"conversations.connect:manage",
|
|
38
|
+
"conversations.connect:read",
|
|
39
|
+
"conversations.connect:write",
|
|
40
|
+
"dnd:read",
|
|
41
|
+
"emoji:read",
|
|
42
|
+
"files:read",
|
|
43
|
+
"files:write",
|
|
44
|
+
"groups:history",
|
|
45
|
+
"groups:read",
|
|
46
|
+
"groups:write",
|
|
47
|
+
"groups:write.invites",
|
|
48
|
+
"groups:write.topic",
|
|
49
|
+
"im:history",
|
|
50
|
+
"im:read",
|
|
51
|
+
"im:write",
|
|
52
|
+
"im:write.topic",
|
|
53
|
+
"lists:read",
|
|
54
|
+
"lists:write",
|
|
55
|
+
"metadata.message:read",
|
|
56
|
+
"mpim:history",
|
|
57
|
+
"mpim:read",
|
|
58
|
+
"mpim:write.topic",
|
|
59
|
+
"pins:read",
|
|
60
|
+
"pins:write",
|
|
61
|
+
"reactions:read",
|
|
62
|
+
"reactions:write",
|
|
63
|
+
"reminders:read",
|
|
64
|
+
"reminders:write",
|
|
65
|
+
"remote_files:read",
|
|
66
|
+
"remote_files:share",
|
|
67
|
+
"remote_files:write",
|
|
68
|
+
"team.preferences:read",
|
|
69
|
+
"team:read",
|
|
70
|
+
"usergroups:read",
|
|
71
|
+
"usergroups:write",
|
|
72
|
+
"users.profile:read",
|
|
73
|
+
"users:read",
|
|
74
|
+
"users:read.email",
|
|
75
|
+
"users:write"
|
|
76
|
+
],
|
|
77
|
+
"api": {
|
|
78
|
+
"base": "https://slack.com/api",
|
|
79
|
+
"docs": "https://docs.slack.dev/reference/methods/",
|
|
80
|
+
"reads": [
|
|
81
|
+
"POST /auth.test",
|
|
82
|
+
"POST /bookmarks.list",
|
|
83
|
+
"POST /calls.info",
|
|
84
|
+
"POST /canvases.sections.lookup",
|
|
85
|
+
"POST /slackLists.download.get",
|
|
86
|
+
"POST /slackLists.items.info",
|
|
87
|
+
"POST /slackLists.items.list"
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
}
|
package/sources.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Sources: what a person lets an app reach as them.
|
|
2
|
+
//
|
|
3
|
+
// A person's tools come from three kinds of source, each named by an address:
|
|
4
|
+
// an MCP server they connected (`mcp:<server>`), a connection of theirs sent
|
|
5
|
+
// requests through (`connection:<connection>`), and a published app service
|
|
6
|
+
// they connected for an app (`service:<service app>/<app>`). An agent acting
|
|
7
|
+
// for the person reaches them by the person's own rules; an app's code
|
|
8
|
+
// reaches one only where the person granted it to that app.
|
|
9
|
+
//
|
|
10
|
+
// A grant is a `uses` link from the app to the source, the same link a
|
|
11
|
+
// service grant and an app's per-person ask already are. It says `each`, so
|
|
12
|
+
// only the person who holds the source spends it (the egress reads it so),
|
|
13
|
+
// and names no binding, so the app's code is handed nothing for it. It says
|
|
14
|
+
// yes or no to the source alone: which tools the source offers stays the
|
|
15
|
+
// source's own (`mcp_server.allow`, a service's granted operations). It is
|
|
16
|
+
// read on every call, so deleting the link, or the source, stops the next.
|
|
17
|
+
import { line } from "@yaks/query";
|
|
18
|
+
import { edgeEid, link } from "@yaks/edge";
|
|
19
|
+
import { CallError } from "@yaks/tools/errors";
|
|
20
|
+
import { CONNECTION, USES } from "./identity.js";
|
|
21
|
+
import { known } from "./integrations.js";
|
|
22
|
+
let MCP = 'mcp_server';
|
|
23
|
+
let EDGE = 'edge';
|
|
24
|
+
let comp = (b, name)=>b?.[name] ?? {};
|
|
25
|
+
/** A service's address for the app holding its connection. */ export let serviceAt = (service, app)=>`service:${service}/${app}`;
|
|
26
|
+
/** The kind an address names, and the rest of it.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { assertEquals } from '@std/assert'
|
|
30
|
+
* assertEquals(addressed('mcp:abc'), { kind: 'mcp', rest: 'abc' })
|
|
31
|
+
* assertEquals(addressed('connection:'), { kind: 'connection', rest: '' })
|
|
32
|
+
* assertEquals(addressed('recipes'), undefined)
|
|
33
|
+
* ```
|
|
34
|
+
*/ export let addressed = (at)=>{
|
|
35
|
+
let m = /^(mcp|connection|service):(.*)$/s.exec(at);
|
|
36
|
+
return m ? {
|
|
37
|
+
kind: m[1],
|
|
38
|
+
rest: m[2]
|
|
39
|
+
} : undefined;
|
|
40
|
+
};
|
|
41
|
+
// The address a held entity is reached at, where it is a source: an MCP
|
|
42
|
+
// server, a connection sent requests through, or the connection behind an app
|
|
43
|
+
// service, whose address names the app holding it. A connection that is an
|
|
44
|
+
// MCP server's credential is that server's already.
|
|
45
|
+
let addressOf = (b, i, app)=>{
|
|
46
|
+
if (b[MCP]) return `mcp:${b.entity.eid}`;
|
|
47
|
+
let c = comp(b, CONNECTION);
|
|
48
|
+
if (typeof c.integration != 'string' || c.integration.startsWith('mcp:')) {
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
if (i?.service) return serviceAt(i.service.app, app);
|
|
52
|
+
return `connection:${b.entity.eid}`;
|
|
53
|
+
};
|
|
54
|
+
let holder = (b)=>b[MCP] ? comp(b, MCP).owner : comp(b, CONNECTION).owner;
|
|
55
|
+
// The integrations a set of connections go through, by name.
|
|
56
|
+
let integrations = async (read, held)=>{
|
|
57
|
+
let names = [
|
|
58
|
+
...new Set(held.map((b)=>comp(b, CONNECTION).integration).filter((n)=>typeof n == 'string'))
|
|
59
|
+
];
|
|
60
|
+
let found = await Promise.all(names.map((n)=>known(read, n)));
|
|
61
|
+
return new Map(names.map((n, i)=>[
|
|
62
|
+
n,
|
|
63
|
+
found[i]
|
|
64
|
+
]));
|
|
65
|
+
};
|
|
66
|
+
let addresses = async (read, held, app)=>{
|
|
67
|
+
let by = await integrations(read, held);
|
|
68
|
+
return held.map((b)=>addressOf(b, by.get(String(comp(b, CONNECTION).integration)), app));
|
|
69
|
+
};
|
|
70
|
+
/** Every source of `person`'s that `app` holds their grant to: the far end
|
|
71
|
+
* of each `uses` link from the app that the person holds, whole. */ export let grantedTo = async (read, a)=>{
|
|
72
|
+
let links = await read(line`.${EDGE}.from=${a.app}&.${USES}&*`);
|
|
73
|
+
if (!links.length) return [];
|
|
74
|
+
return (await read(line`.entity.eid=${links.map((l)=>String(comp(l, EDGE).to))}&*`)).filter((b)=>holder(b) == a.person);
|
|
75
|
+
};
|
|
76
|
+
/** Every source of `person`'s that `app` holds their grant to, by address. */ export let grantsOf = async (read, a)=>{
|
|
77
|
+
let held = await grantedTo(read, a);
|
|
78
|
+
return (await addresses(read, held, a.app)).filter((at)=>at != null);
|
|
79
|
+
};
|
|
80
|
+
// A link that is a person's grant of their own source: spent by them alone
|
|
81
|
+
// and naming no binding, since the app's code is handed nothing for it.
|
|
82
|
+
let granting = (l)=>{
|
|
83
|
+
let u = comp(l, USES);
|
|
84
|
+
return u.each == true && u.binding == null && u.lender == null;
|
|
85
|
+
};
|
|
86
|
+
/** The apps each of `eids` (sources a person holds) is granted to, by the
|
|
87
|
+
* source's eid. */ export let grantees = async (read, eids)=>{
|
|
88
|
+
let out = new Map(eids.map((e)=>[
|
|
89
|
+
e,
|
|
90
|
+
[]
|
|
91
|
+
]));
|
|
92
|
+
if (!eids.length) return out;
|
|
93
|
+
for (let l of (await read(line`.${EDGE}.to=${eids}&.${USES}&*`))){
|
|
94
|
+
let e = comp(l, EDGE);
|
|
95
|
+
if (granting(l)) out.get(String(e.to))?.push(String(e.from));
|
|
96
|
+
}
|
|
97
|
+
return out;
|
|
98
|
+
};
|
|
99
|
+
/** Whether `person` granted `app` the source at `at`, read now. */ export let granted = async (read, a)=>(await grantsOf(read, a)).includes(a.at);
|
|
100
|
+
// The entity behind an MCP server's or a connection's address.
|
|
101
|
+
let eidOf = (at)=>{
|
|
102
|
+
let said = addressed(at);
|
|
103
|
+
if (!said || said.kind == 'service' || !said.rest) {
|
|
104
|
+
throw new CallError('arguments', `${at} is not one source: mcp:<server> or connection:<connection>`);
|
|
105
|
+
}
|
|
106
|
+
return said.rest;
|
|
107
|
+
};
|
|
108
|
+
/** The source the link from an app to `eid` reaches, and the link, where the
|
|
109
|
+
* person holds it and the link stands. */ export let grantOf = async (read, a)=>{
|
|
110
|
+
let rows = await read(line`(.entity.eid=${a.eid}&*)|(.entity.eid=${edgeEid(a.app, USES, a.eid)}&.${USES}&?${EDGE})`);
|
|
111
|
+
let source = rows.find((b)=>b.entity.eid == a.eid);
|
|
112
|
+
let held = rows.find((b)=>b[USES] && comp(b, EDGE).from == a.app && comp(b, EDGE).to == a.eid);
|
|
113
|
+
return source && held && holder(source) == a.person ? {
|
|
114
|
+
source,
|
|
115
|
+
link: held
|
|
116
|
+
} : undefined;
|
|
117
|
+
};
|
|
118
|
+
/** The person's grant of one of their sources to an app: the link, spent by
|
|
119
|
+
* them alone and naming no binding. Only the person who holds the source
|
|
120
|
+
* grants it. */ export let grant = async (read, a)=>{
|
|
121
|
+
let eid = eidOf(a.at);
|
|
122
|
+
let [b] = await read(line`.entity.eid=${eid}&*`);
|
|
123
|
+
let [at] = b ? await addresses(read, [
|
|
124
|
+
b
|
|
125
|
+
], a.app) : [];
|
|
126
|
+
if (!b || holder(b) != a.person || at != a.at) {
|
|
127
|
+
throw new CallError('access', 'Only the person who holds this source grants it to an app');
|
|
128
|
+
}
|
|
129
|
+
return [
|
|
130
|
+
{
|
|
131
|
+
...link(a.app, USES, eid),
|
|
132
|
+
[USES]: {
|
|
133
|
+
each: true
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
];
|
|
137
|
+
};
|
|
138
|
+
/** Take back a grant: the link goes, and the next call through it is refused. */ export let revoke = (a)=>[
|
|
139
|
+
{
|
|
140
|
+
entity: {
|
|
141
|
+
eid: edgeEid(a.app, USES, eidOf(a.at))
|
|
142
|
+
},
|
|
143
|
+
tombstone: {}
|
|
144
|
+
}
|
|
145
|
+
];
|
|
146
|
+
/** What `person` may grant at `at`: the one source it names, or each of its
|
|
147
|
+
* kind they hold (`mcp:`, `connection:`), with what it is and where it
|
|
148
|
+
* reaches. A connection is offered once connected with hosts to send to. */ export let grantable = async (read, person, at)=>{
|
|
149
|
+
let said = addressed(at);
|
|
150
|
+
if (!said || said.kind == 'service') return [];
|
|
151
|
+
let held = said.kind == 'mcp' ? await read(line`.${MCP}.owner=${person}&*`) : await read(line`.${CONNECTION}.owner=${person}&.${CONNECTION}.status=${'connected'}&*`);
|
|
152
|
+
let by = await integrations(read, held);
|
|
153
|
+
let out = [];
|
|
154
|
+
for (let b of held){
|
|
155
|
+
let one = `${said.kind}:${b.entity.eid}`;
|
|
156
|
+
if (said.rest && one != at) continue;
|
|
157
|
+
if (said.kind == 'mcp') {
|
|
158
|
+
let s = comp(b, MCP);
|
|
159
|
+
if (s.enabled === false) continue;
|
|
160
|
+
out.push({
|
|
161
|
+
at: one,
|
|
162
|
+
title: String(s.name),
|
|
163
|
+
about: String(s.url)
|
|
164
|
+
});
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
let c = comp(b, CONNECTION);
|
|
168
|
+
let i = by.get(String(c.integration));
|
|
169
|
+
if (addressOf(b, i, '') != one || !i?.hosts.length) continue;
|
|
170
|
+
out.push({
|
|
171
|
+
at: one,
|
|
172
|
+
title: `${i.title || i.name}${c.account ? ` (${c.account})` : ''}`,
|
|
173
|
+
about: `requests to ${i.hosts.join(', ')}`
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
return out;
|
|
177
|
+
};
|
|
178
|
+
//# sourceMappingURL=sources.js.map
|
package/sources.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.js","sources":["./sources.ts"],"names":[],"mappings":"AAAA,oDAA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AAK,IAAI,IAAI,QACpB,MAAM,KAAK,IAAI,CAAC,CAAC,EAAE,IAAI,OAAO,EAAE,OAAO,EAAE,CAAC,IAC1C,MAAM,KACN,IAAI,CAAC,CAAC,EAAE,WAAW,OAAO,EAAE,OAAO,EAAE,EAAE,WAAW,QAAQ,EAAE,YAAY,EAAE,CAAC;EAE/E,IAAI,KAAK,MAAM,aAAa,MAAM;EAClC,IAAI,MAAmB,EAAE;EACzB,KAAK,IAAI,KAAK,KAAM;IAClB,IAAI,MAAM,GAAG,KAAK,IAAI,CAAC,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,EAAE;IACxC,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI;IAC5B,IAAI,KAAK,IAAI,IAAI,OAAO;MACtB,IAAI,IAAI,KAAK,GAAG;MAChB,IAAI,EAAE,OAAO,KAAK,OAAO;MACzB,IAAI,IAAI,CAAC;QAAE,IAAI;QAAK,OAAO,OAAO,EAAE,IAAI;QAAG,OAAO,OAAO,EAAE,GAAG;MAAE;MAChE;IACF;IACA,IAAI,IAAI,KAAK,GAAG;IAChB,IAAI,IAAI,GAAG,GAAG,CAAC,OAAO,EAAE,WAAW;IACnC,IAAI,UAAU,GAAG,GAAG,OAAO,OAAO,CAAC,GAAG,MAAM,QAAQ;IACpD,IAAI,IAAI,CAAC;MACP,IAAI;MACJ,OAAO,GAAG,EAAE,KAAK,IAAI,EAAE,IAAI,GAAG,EAAE,OAAO,GAAG,CAAC,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI;MAClE,OAAO,CAAC,YAAY,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO;IAC5C;EACF;EACA,OAAO;AACT,EAAC"}
|
package/sources.ts
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Sources: what a person lets an app reach as them.
|
|
2
|
+
//
|
|
3
|
+
// A person's tools come from three kinds of source, each named by an address:
|
|
4
|
+
// an MCP server they connected (`mcp:<server>`), a connection of theirs sent
|
|
5
|
+
// requests through (`connection:<connection>`), and a published app service
|
|
6
|
+
// they connected for an app (`service:<service app>/<app>`). An agent acting
|
|
7
|
+
// for the person reaches them by the person's own rules; an app's code
|
|
8
|
+
// reaches one only where the person granted it to that app.
|
|
9
|
+
//
|
|
10
|
+
// A grant is a `uses` link from the app to the source, the same link a
|
|
11
|
+
// service grant and an app's per-person ask already are. It says `each`, so
|
|
12
|
+
// only the person who holds the source spends it (the egress reads it so),
|
|
13
|
+
// and names no binding, so the app's code is handed nothing for it. It says
|
|
14
|
+
// yes or no to the source alone: which tools the source offers stays the
|
|
15
|
+
// source's own (`mcp_server.allow`, a service's granted operations). It is
|
|
16
|
+
// read on every call, so deleting the link, or the source, stops the next.
|
|
17
|
+
|
|
18
|
+
import { line } from '@yaks/query'
|
|
19
|
+
import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
20
|
+
import { edgeEid, link } from '@yaks/edge'
|
|
21
|
+
import { CallError } from '@yaks/tools/errors'
|
|
22
|
+
import { CONNECTION, USES } from './identity.js'
|
|
23
|
+
import { type Integration, known, type Read } from './integrations.js'
|
|
24
|
+
|
|
25
|
+
/** The kinds of source a person grants an app by address. */
|
|
26
|
+
export type Kind = 'mcp' | 'connection' | 'service'
|
|
27
|
+
|
|
28
|
+
/** One source a person can grant, as the page asking them shows it. */
|
|
29
|
+
export type Grantable = { at: string; title: string; about: string }
|
|
30
|
+
|
|
31
|
+
let MCP = 'mcp_server'
|
|
32
|
+
let EDGE = 'edge'
|
|
33
|
+
|
|
34
|
+
let comp = (b: Bundle | undefined, name: string): Comp =>
|
|
35
|
+
(b?.[name] ?? {}) as Comp
|
|
36
|
+
|
|
37
|
+
/** A service's address for the app holding its connection. */
|
|
38
|
+
export let serviceAt = (service: string, app: string): string =>
|
|
39
|
+
`service:${service}/${app}`
|
|
40
|
+
|
|
41
|
+
/** The kind an address names, and the rest of it.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { assertEquals } from '@std/assert'
|
|
45
|
+
* assertEquals(addressed('mcp:abc'), { kind: 'mcp', rest: 'abc' })
|
|
46
|
+
* assertEquals(addressed('connection:'), { kind: 'connection', rest: '' })
|
|
47
|
+
* assertEquals(addressed('recipes'), undefined)
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export let addressed = (
|
|
51
|
+
at: string,
|
|
52
|
+
): { kind: Kind; rest: string } | undefined => {
|
|
53
|
+
let m = /^(mcp|connection|service):(.*)$/s.exec(at)
|
|
54
|
+
return m ? { kind: m[1] as Kind, rest: m[2] } : undefined
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The address a held entity is reached at, where it is a source: an MCP
|
|
58
|
+
// server, a connection sent requests through, or the connection behind an app
|
|
59
|
+
// service, whose address names the app holding it. A connection that is an
|
|
60
|
+
// MCP server's credential is that server's already.
|
|
61
|
+
let addressOf = (b: Bundle, i: Integration | undefined, app: Eid) => {
|
|
62
|
+
if (b[MCP]) return `mcp:${b.entity.eid}`
|
|
63
|
+
let c = comp(b, CONNECTION)
|
|
64
|
+
if (typeof c.integration != 'string' || c.integration.startsWith('mcp:')) {
|
|
65
|
+
return undefined
|
|
66
|
+
}
|
|
67
|
+
if (i?.service) return serviceAt(i.service.app, app)
|
|
68
|
+
return `connection:${b.entity.eid}`
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let holder = (b: Bundle) =>
|
|
72
|
+
b[MCP] ? comp(b, MCP).owner : comp(b, CONNECTION).owner
|
|
73
|
+
|
|
74
|
+
// The integrations a set of connections go through, by name.
|
|
75
|
+
let integrations = async (read: Read, held: Bundle[]) => {
|
|
76
|
+
let names = [
|
|
77
|
+
...new Set(
|
|
78
|
+
held.map((b) => comp(b, CONNECTION).integration).filter((n) =>
|
|
79
|
+
typeof n == 'string'
|
|
80
|
+
),
|
|
81
|
+
),
|
|
82
|
+
] as string[]
|
|
83
|
+
let found = await Promise.all(names.map((n) => known(read, n)))
|
|
84
|
+
return new Map(names.map((n, i) => [n, found[i]]))
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let addresses = async (read: Read, held: Bundle[], app: Eid) => {
|
|
88
|
+
let by = await integrations(read, held)
|
|
89
|
+
return held.map((b) =>
|
|
90
|
+
addressOf(b, by.get(String(comp(b, CONNECTION).integration)), app)
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Every source of `person`'s that `app` holds their grant to: the far end
|
|
95
|
+
* of each `uses` link from the app that the person holds, whole. */
|
|
96
|
+
export let grantedTo = async (
|
|
97
|
+
read: Read,
|
|
98
|
+
a: { app: Eid; person: Eid },
|
|
99
|
+
): Promise<Bundle[]> => {
|
|
100
|
+
let links = await read(line`.${EDGE}.from=${a.app}&.${USES}&*`)
|
|
101
|
+
if (!links.length) return []
|
|
102
|
+
return (await read(
|
|
103
|
+
line`.entity.eid=${links.map((l) => String(comp(l, EDGE).to))}&*`,
|
|
104
|
+
)).filter((b) => holder(b) == a.person)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Every source of `person`'s that `app` holds their grant to, by address. */
|
|
108
|
+
export let grantsOf = async (
|
|
109
|
+
read: Read,
|
|
110
|
+
a: { app: Eid; person: Eid },
|
|
111
|
+
): Promise<string[]> => {
|
|
112
|
+
let held = await grantedTo(read, a)
|
|
113
|
+
return (await addresses(read, held, a.app)).filter((at) => at != null)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// A link that is a person's grant of their own source: spent by them alone
|
|
117
|
+
// and naming no binding, since the app's code is handed nothing for it.
|
|
118
|
+
let granting = (l: Bundle) => {
|
|
119
|
+
let u = comp(l, USES)
|
|
120
|
+
return u.each == true && u.binding == null && u.lender == null
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The apps each of `eids` (sources a person holds) is granted to, by the
|
|
124
|
+
* source's eid. */
|
|
125
|
+
export let grantees = async (
|
|
126
|
+
read: Read,
|
|
127
|
+
eids: Eid[],
|
|
128
|
+
): Promise<Map<Eid, Eid[]>> => {
|
|
129
|
+
let out = new Map(eids.map((e) => [e, [] as Eid[]]))
|
|
130
|
+
if (!eids.length) return out
|
|
131
|
+
for (let l of await read(line`.${EDGE}.to=${eids}&.${USES}&*`)) {
|
|
132
|
+
let e = comp(l, EDGE)
|
|
133
|
+
if (granting(l)) out.get(String(e.to))?.push(String(e.from))
|
|
134
|
+
}
|
|
135
|
+
return out
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Whether `person` granted `app` the source at `at`, read now. */
|
|
139
|
+
export let granted = async (
|
|
140
|
+
read: Read,
|
|
141
|
+
a: { app: Eid; person: Eid; at: string },
|
|
142
|
+
): Promise<boolean> => (await grantsOf(read, a)).includes(a.at)
|
|
143
|
+
|
|
144
|
+
// The entity behind an MCP server's or a connection's address.
|
|
145
|
+
let eidOf = (at: string): Eid => {
|
|
146
|
+
let said = addressed(at)
|
|
147
|
+
if (!said || said.kind == 'service' || !said.rest) {
|
|
148
|
+
throw new CallError(
|
|
149
|
+
'arguments',
|
|
150
|
+
`${at} is not one source: mcp:<server> or connection:<connection>`,
|
|
151
|
+
)
|
|
152
|
+
}
|
|
153
|
+
return said.rest
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** The source the link from an app to `eid` reaches, and the link, where the
|
|
157
|
+
* person holds it and the link stands. */
|
|
158
|
+
export let grantOf = async (
|
|
159
|
+
read: Read,
|
|
160
|
+
a: { app: Eid; person: Eid; eid: Eid },
|
|
161
|
+
): Promise<{ source: Bundle; link: Bundle } | undefined> => {
|
|
162
|
+
let rows = await read(
|
|
163
|
+
line`(.entity.eid=${a.eid}&*)|(.entity.eid=${
|
|
164
|
+
edgeEid(a.app, USES, a.eid)
|
|
165
|
+
}&.${USES}&?${EDGE})`,
|
|
166
|
+
)
|
|
167
|
+
let source = rows.find((b) => b.entity.eid == a.eid)
|
|
168
|
+
let held = rows.find((b) =>
|
|
169
|
+
b[USES] && comp(b, EDGE).from == a.app && comp(b, EDGE).to == a.eid
|
|
170
|
+
)
|
|
171
|
+
return source && held && holder(source) == a.person
|
|
172
|
+
? { source, link: held }
|
|
173
|
+
: undefined
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** The person's grant of one of their sources to an app: the link, spent by
|
|
177
|
+
* them alone and naming no binding. Only the person who holds the source
|
|
178
|
+
* grants it. */
|
|
179
|
+
export let grant = async (
|
|
180
|
+
read: Read,
|
|
181
|
+
a: { app: Eid; person: Eid; at: string },
|
|
182
|
+
): Promise<Bundle[]> => {
|
|
183
|
+
let eid = eidOf(a.at)
|
|
184
|
+
let [b] = await read(line`.entity.eid=${eid}&*`)
|
|
185
|
+
let [at] = b ? await addresses(read, [b], a.app) : []
|
|
186
|
+
if (!b || holder(b) != a.person || at != a.at) {
|
|
187
|
+
throw new CallError(
|
|
188
|
+
'access',
|
|
189
|
+
'Only the person who holds this source grants it to an app',
|
|
190
|
+
)
|
|
191
|
+
}
|
|
192
|
+
return [{ ...link(a.app, USES, eid), [USES]: { each: true } }]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Take back a grant: the link goes, and the next call through it is refused. */
|
|
196
|
+
export let revoke = (a: { app: Eid; at: string }): Bundle[] => [{
|
|
197
|
+
entity: { eid: edgeEid(a.app, USES, eidOf(a.at)) },
|
|
198
|
+
tombstone: {},
|
|
199
|
+
}]
|
|
200
|
+
|
|
201
|
+
/** What `person` may grant at `at`: the one source it names, or each of its
|
|
202
|
+
* kind they hold (`mcp:`, `connection:`), with what it is and where it
|
|
203
|
+
* reaches. A connection is offered once connected with hosts to send to. */
|
|
204
|
+
export let grantable = async (
|
|
205
|
+
read: Read,
|
|
206
|
+
person: Eid,
|
|
207
|
+
at: string,
|
|
208
|
+
): Promise<Grantable[]> => {
|
|
209
|
+
let said = addressed(at)
|
|
210
|
+
if (!said || said.kind == 'service') return []
|
|
211
|
+
let held = said.kind == 'mcp'
|
|
212
|
+
? await read(line`.${MCP}.owner=${person}&*`)
|
|
213
|
+
: await read(
|
|
214
|
+
line`.${CONNECTION}.owner=${person}&.${CONNECTION}.status=${'connected'}&*`,
|
|
215
|
+
)
|
|
216
|
+
let by = await integrations(read, held)
|
|
217
|
+
let out: Grantable[] = []
|
|
218
|
+
for (let b of held) {
|
|
219
|
+
let one = `${said.kind}:${b.entity.eid}`
|
|
220
|
+
if (said.rest && one != at) continue
|
|
221
|
+
if (said.kind == 'mcp') {
|
|
222
|
+
let s = comp(b, MCP)
|
|
223
|
+
if (s.enabled === false) continue
|
|
224
|
+
out.push({ at: one, title: String(s.name), about: String(s.url) })
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
let c = comp(b, CONNECTION)
|
|
228
|
+
let i = by.get(String(c.integration))
|
|
229
|
+
if (addressOf(b, i, '') != one || !i?.hosts.length) continue
|
|
230
|
+
out.push({
|
|
231
|
+
at: one,
|
|
232
|
+
title: `${i.title || i.name}${c.account ? ` (${c.account})` : ''}`,
|
|
233
|
+
about: `requests to ${i.hosts.join(', ')}`,
|
|
234
|
+
})
|
|
235
|
+
}
|
|
236
|
+
return out
|
|
237
|
+
}
|
package/tools.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// What anybody may ask about connections: the `@yaks/connections/tools` entry
|
|
2
|
+
// point, the implementations behind the two `tool: true` declarations in
|
|
3
|
+
// ./vocab.json. Neither touches a credential: `need` makes a connection with
|
|
4
|
+
// none, and `list` shows the handle where one is kept. Keys and grants arrive
|
|
5
|
+
// through trusted code (`connect`), never through a tool's arguments, which a
|
|
6
|
+
// graph keeps as the text of a call.
|
|
7
|
+
import { argsOf } from "@yaks/graph/call";
|
|
8
|
+
import { list, need } from "./connections.js";
|
|
9
|
+
import { ABOUT } from "./integrations.js";
|
|
10
|
+
let str = (v)=>v == null ? '' : String(v);
|
|
11
|
+
let strs = (v)=>Array.isArray(v) ? v.map(String) : [];
|
|
12
|
+
// What was said about a custom integration, each property as given.
|
|
13
|
+
let about = (args)=>Object.fromEntries(ABOUT.filter((k)=>args[k] != null).map((k)=>[
|
|
14
|
+
k,
|
|
15
|
+
str(args[k])
|
|
16
|
+
]));
|
|
17
|
+
/** The implementations of the tools ./vocab.json declares. */ export let runs = ()=>({
|
|
18
|
+
connection_need: (call, graph)=>{
|
|
19
|
+
let args = argsOf(call);
|
|
20
|
+
return need(graph.read, {
|
|
21
|
+
app: args.app == null ? undefined : str(args.app),
|
|
22
|
+
owner: str(args.owner),
|
|
23
|
+
integration: str(args.integration),
|
|
24
|
+
scopes: strs(args.scopes),
|
|
25
|
+
hosts: Array.isArray(args.hosts) ? strs(args.hosts) : undefined,
|
|
26
|
+
fields: Array.isArray(args.fields) ? args.fields : undefined,
|
|
27
|
+
each: args.each == true,
|
|
28
|
+
binding: args.binding == null ? undefined : str(args.binding),
|
|
29
|
+
separate: args.separate == true,
|
|
30
|
+
direct: args.direct == null ? undefined : args.direct == true,
|
|
31
|
+
task: args.task == null ? undefined : str(args.task),
|
|
32
|
+
keep: Array.isArray(args.keep) ? args.keep : undefined,
|
|
33
|
+
...about(args),
|
|
34
|
+
image: args.image == null ? undefined : str(args.image)
|
|
35
|
+
}, true);
|
|
36
|
+
},
|
|
37
|
+
connection_list: (call, graph)=>list(graph.read, str(argsOf(call).owner))
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=tools.js.map
|
package/tools.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AACzE,6EAA6E;AAC7E,8EAA8E;AAC9E,8EAA8E;AAC9E,qCAAqC;AAErC,SAAS,MAAM,gCAAqC;AAEpD,SAAS,IAAI,EAAE,IAAI,2BAA0B;AAC7C,SAAS,KAAK,4BAAuC;AAErD,IAAI,MAAM,CAAC,IAAuB,KAAK,OAAO,KAAK,OAAO;AAE1D,IAAI,OAAO,CAAC,IAAyB,MAAM,OAAO,CAAC,KAAK,EAAE,GAAG,CA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