@coswic/aipay 0.2.0 → 0.3.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 +1 -1
- package/dist/agent-guide.d.ts +18 -0
- package/dist/agent-guide.js +60 -0
- package/dist/args.d.ts +15 -0
- package/dist/args.js +47 -0
- package/dist/auth-store.d.ts +47 -0
- package/dist/auth-store.js +176 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +8 -0
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +18 -0
- package/dist/commands/apps.d.ts +15 -0
- package/dist/commands/apps.js +53 -0
- package/dist/commands/doctor.d.ts +17 -0
- package/dist/commands/doctor.js +153 -0
- package/dist/commands/init.d.ts +18 -0
- package/dist/commands/init.js +269 -0
- package/dist/commands/login.d.ts +34 -0
- package/dist/commands/login.js +189 -0
- package/dist/commands/session.d.ts +6 -0
- package/dist/commands/session.js +63 -0
- package/dist/commands/status.d.ts +3 -0
- package/dist/commands/status.js +24 -0
- package/dist/commands/test.d.ts +12 -0
- package/dist/commands/test.js +162 -0
- package/dist/commands/unlink.d.ts +5 -0
- package/dist/commands/unlink.js +73 -0
- package/dist/detect.d.ts +27 -0
- package/dist/detect.js +83 -0
- package/dist/developer-api.d.ts +14 -0
- package/dist/developer-api.js +74 -0
- package/dist/device.d.ts +38 -0
- package/dist/device.js +122 -0
- package/dist/gate.d.ts +13 -0
- package/dist/gate.js +29 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +22 -0
- package/dist/loopback.d.ts +37 -0
- package/dist/loopback.js +95 -0
- package/dist/main.d.ts +5 -0
- package/dist/main.js +91 -0
- package/dist/output.d.ts +36 -0
- package/dist/output.js +52 -0
- package/dist/project.d.ts +61 -0
- package/dist/project.js +127 -0
- package/dist/remote.d.ts +21 -0
- package/dist/remote.js +41 -0
- package/dist/setup-events.d.ts +3 -0
- package/dist/setup-events.js +28 -0
- package/dist/templates.d.ts +24 -0
- package/dist/templates.js +333 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scaffold templates: one `aipay` helper module (sign-in flow + first completion) and a
|
|
3
|
+
* thin wiring file per framework. Authored twice (TypeScript / JavaScript) so nothing is
|
|
4
|
+
* generated by stripping types. The callback path is the one registered in the Console
|
|
5
|
+
* for the app (default /auth/aipay/callback); the start path sits next to it.
|
|
6
|
+
*
|
|
7
|
+
* Sessions are the developer's business — the template keeps pending flows and signed-in
|
|
8
|
+
* users in memory with a loud comment, which is correct for a dev server and obviously
|
|
9
|
+
* wrong for production.
|
|
10
|
+
*/
|
|
11
|
+
import type { Framework } from './project.js';
|
|
12
|
+
export interface TemplateFile {
|
|
13
|
+
/** Path relative to the project root. */
|
|
14
|
+
path: string;
|
|
15
|
+
content: string;
|
|
16
|
+
}
|
|
17
|
+
export interface TemplateInput {
|
|
18
|
+
framework: Framework;
|
|
19
|
+
typescript: boolean;
|
|
20
|
+
srcDir: string;
|
|
21
|
+
callbackPath: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function startPathFor(callbackPath: string): string;
|
|
24
|
+
export declare function scaffoldFiles(input: TemplateInput): TemplateFile[];
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
export function startPathFor(callbackPath) {
|
|
2
|
+
return callbackPath.replace(/\/[^/]*$/, '/start');
|
|
3
|
+
}
|
|
4
|
+
export function scaffoldFiles(input) {
|
|
5
|
+
const ext = input.typescript ? 'ts' : 'js';
|
|
6
|
+
const startPath = startPathFor(input.callbackPath);
|
|
7
|
+
const core = {
|
|
8
|
+
path: `${prefix(input.srcDir)}aipay.${ext}`,
|
|
9
|
+
content: coreModule(input.typescript),
|
|
10
|
+
};
|
|
11
|
+
switch (input.framework) {
|
|
12
|
+
case 'next':
|
|
13
|
+
return [
|
|
14
|
+
core,
|
|
15
|
+
{ path: `app${startPath}/route.${ext}`, content: nextStart(input) },
|
|
16
|
+
{ path: `app${input.callbackPath}/route.${ext}`, content: nextCallback(input) },
|
|
17
|
+
];
|
|
18
|
+
case 'express':
|
|
19
|
+
return [
|
|
20
|
+
core,
|
|
21
|
+
{
|
|
22
|
+
path: `${prefix(input.srcDir)}aipay-routes.${ext}`,
|
|
23
|
+
content: expressRoutes(input, startPath),
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
case 'fastify':
|
|
27
|
+
return [
|
|
28
|
+
core,
|
|
29
|
+
{
|
|
30
|
+
path: `${prefix(input.srcDir)}aipay-routes.${ext}`,
|
|
31
|
+
content: fastifyRoutes(input, startPath),
|
|
32
|
+
},
|
|
33
|
+
];
|
|
34
|
+
case 'hono':
|
|
35
|
+
return [
|
|
36
|
+
core,
|
|
37
|
+
{
|
|
38
|
+
path: `${prefix(input.srcDir)}aipay-routes.${ext}`,
|
|
39
|
+
content: honoRoutes(input, startPath),
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
case 'vite':
|
|
43
|
+
// Browser SPA: no server, so the helper module itself runs in the page (PKCE public
|
|
44
|
+
// client; tokens live in sessionStorage for the tab) and a boot file handles the callback.
|
|
45
|
+
return [
|
|
46
|
+
{ path: `${prefix(input.srcDir)}aipay.${ext}`, content: browserModule(input) },
|
|
47
|
+
{ path: `${prefix(input.srcDir)}aipay-boot.${ext}`, content: viteBoot(input) },
|
|
48
|
+
];
|
|
49
|
+
default:
|
|
50
|
+
return [
|
|
51
|
+
core,
|
|
52
|
+
{
|
|
53
|
+
path: `${prefix(input.srcDir)}aipay-routes.${ext}`,
|
|
54
|
+
content: nodeRoutes(input, startPath),
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function prefix(srcDir) {
|
|
60
|
+
return srcDir === '.' ? '' : `${srcDir.replace(/\/$/, '')}/`;
|
|
61
|
+
}
|
|
62
|
+
const ENV_HEADER = `// Reads the public values \`aipay init\` wrote to .env.local (load them with your usual
|
|
63
|
+
// dotenv/framework mechanism). No secret exists: the app is a public PKCE client.`;
|
|
64
|
+
function coreModule(ts) {
|
|
65
|
+
const t = (typed, plain) => (ts ? typed : plain);
|
|
66
|
+
return `${ENV_HEADER}
|
|
67
|
+
import {
|
|
68
|
+
AIPayClient,
|
|
69
|
+
createConnectUrl,
|
|
70
|
+
getUserInfo,
|
|
71
|
+
handleCallback,${t('\n type TokenSet,', '')}
|
|
72
|
+
} from '@coswic/aipay-sdk'
|
|
73
|
+
|
|
74
|
+
const cfg = {
|
|
75
|
+
hydraPublicUrl: process.env.AIPAY_AUTH_ORIGIN${t(' as string', '')},
|
|
76
|
+
clientId: process.env.AIPAY_CLIENT_ID${t(' as string', '')},
|
|
77
|
+
redirectUri: process.env.AIPAY_CALLBACK_URL${t(' as string', '')},
|
|
78
|
+
}
|
|
79
|
+
const apiBase = process.env.AIPAY_API_BASE${t(' as string', '')}
|
|
80
|
+
|
|
81
|
+
// DEV ONLY: in-memory stores. Replace both with your session store before shipping.
|
|
82
|
+
const pendingFlows = new Map${t('<string, { codeVerifier: string; expires: number }>', '')}()
|
|
83
|
+
const sessions = new Map${t('<string, TokenSet>', '')}()
|
|
84
|
+
|
|
85
|
+
/** Step 1 — build the AI-Pay sign-in URL; remember the PKCE verifier under the state. */
|
|
86
|
+
export async function beginSignIn()${t(': Promise<string>', '')} {
|
|
87
|
+
const { url, state, codeVerifier } = await createConnectUrl(cfg)
|
|
88
|
+
pendingFlows.set(state, { codeVerifier, expires: Date.now() + 10 * 60_000 })
|
|
89
|
+
return url
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Step 2 — on the callback, exchange the code (state + PKCE are verified by the SDK). */
|
|
93
|
+
export async function finishSignIn(callbackUrl${t(': string', '')})${t(': Promise<{ sessionId: string; sub: string; email: string | null }>', '')} {
|
|
94
|
+
const state = new URL(callbackUrl).searchParams.get('state') ?? ''
|
|
95
|
+
const pending = pendingFlows.get(state)
|
|
96
|
+
pendingFlows.delete(state)
|
|
97
|
+
const saved = pending && pending.expires > Date.now() ? { state, codeVerifier: pending.codeVerifier } : null
|
|
98
|
+
const tokens = await handleCallback(cfg, callbackUrl, saved)
|
|
99
|
+
const user = await getUserInfo(cfg, tokens.accessToken)
|
|
100
|
+
const sessionId = crypto.randomUUID()
|
|
101
|
+
sessions.set(sessionId, tokens)
|
|
102
|
+
// user.sub is the stable per-app id of this user — key your accounts on it, not on email.
|
|
103
|
+
return { sessionId, sub: user.sub, email: user.email ?? null }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Step 3 — call a model on the user's behalf; the response carries the billing receipt. */
|
|
107
|
+
export async function firstCompletion(sessionId${t(': string', '')}, model${t(': string', '')}) {
|
|
108
|
+
const tokens = sessions.get(sessionId)
|
|
109
|
+
if (!tokens) throw new Error('not signed in')
|
|
110
|
+
const client = new AIPayClient({ baseUrl: apiBase, accessToken: tokens.accessToken })
|
|
111
|
+
const res = await client.chat.completions.create({
|
|
112
|
+
model,
|
|
113
|
+
messages: [{ role: 'user', content: 'Say hello in five words.' }],
|
|
114
|
+
max_tokens: 32,
|
|
115
|
+
})
|
|
116
|
+
return { text: res.choices[0]?.message.content ?? '', receipt: res.aipay }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function sessionCookie(sessionId${t(': string', '')})${t(': string', '')} {
|
|
120
|
+
return \`aipay_session=\${sessionId}; Path=/; HttpOnly; SameSite=Lax\`
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function sessionFromCookie(cookieHeader${t(': string | null | undefined', '')})${t(': string | null', '')} {
|
|
124
|
+
const m = /(?:^|;\\s*)aipay_session=([^;]+)/.exec(cookieHeader ?? '')
|
|
125
|
+
return m ? m[1]${t(' ?? null', '')} : null
|
|
126
|
+
}
|
|
127
|
+
`;
|
|
128
|
+
}
|
|
129
|
+
function nextStart(input) {
|
|
130
|
+
const imp = importPath(input, 'app/x/y/z'); // depth: app/<a>/<b>/<start>/route
|
|
131
|
+
return `import { beginSignIn } from '${imp}'
|
|
132
|
+
|
|
133
|
+
export async function GET() {
|
|
134
|
+
const url = await beginSignIn()
|
|
135
|
+
return Response.redirect(url, 302)
|
|
136
|
+
}
|
|
137
|
+
`;
|
|
138
|
+
}
|
|
139
|
+
function nextCallback(input) {
|
|
140
|
+
const imp = importPath(input, 'app/x/y/z');
|
|
141
|
+
return `import { finishSignIn, sessionCookie } from '${imp}'
|
|
142
|
+
|
|
143
|
+
export async function GET(request${input.typescript ? ': Request' : ''}) {
|
|
144
|
+
const { sessionId, email } = await finishSignIn(request.url)
|
|
145
|
+
return new Response(\`Signed in as \${email ?? 'user'}. You can now call firstCompletion(sessionId, model).\`, {
|
|
146
|
+
status: 200,
|
|
147
|
+
headers: { 'set-cookie': sessionCookie(sessionId), 'content-type': 'text/plain; charset=utf-8' },
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
`;
|
|
151
|
+
}
|
|
152
|
+
/** Relative import from a route file to the core module (depth-aware, extension-less). */
|
|
153
|
+
function importPath(input, routeDir) {
|
|
154
|
+
const depth = routeDir.split('/').length;
|
|
155
|
+
const up = '../'.repeat(depth);
|
|
156
|
+
const core = `${prefix(input.srcDir)}aipay`;
|
|
157
|
+
return `${up}${core}${input.typescript ? '' : '.js'}`;
|
|
158
|
+
}
|
|
159
|
+
function expressRoutes(input, startPath) {
|
|
160
|
+
const ts = input.typescript;
|
|
161
|
+
return `import { Router } from 'express'
|
|
162
|
+
import { beginSignIn, finishSignIn, sessionCookie } from './aipay${ts ? '' : '.js'}'
|
|
163
|
+
|
|
164
|
+
export const aipayRouter = Router()
|
|
165
|
+
|
|
166
|
+
aipayRouter.get('${startPath}', async (_req, res) => {
|
|
167
|
+
res.redirect(await beginSignIn())
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
aipayRouter.get('${input.callbackPath}', async (req, res) => {
|
|
171
|
+
const full = new URL(req.originalUrl, \`\${req.protocol}://\${req.get('host')}\`).toString()
|
|
172
|
+
const { sessionId, email } = await finishSignIn(full)
|
|
173
|
+
res.setHeader('set-cookie', sessionCookie(sessionId))
|
|
174
|
+
res.type('text/plain').send(\`Signed in as \${email ?? 'user'}.\`)
|
|
175
|
+
})
|
|
176
|
+
// app.use(aipayRouter)
|
|
177
|
+
`;
|
|
178
|
+
}
|
|
179
|
+
function fastifyRoutes(input, startPath) {
|
|
180
|
+
const ts = input.typescript;
|
|
181
|
+
return `${ts ? "import type { FastifyInstance } from 'fastify'\n" : ''}import { beginSignIn, finishSignIn, sessionCookie } from './aipay${ts ? '' : '.js'}'
|
|
182
|
+
|
|
183
|
+
export async function aipayRoutes(app${ts ? ': FastifyInstance' : ''}) {
|
|
184
|
+
app.get('${startPath}', async (_req, reply) => reply.redirect(await beginSignIn()))
|
|
185
|
+
app.get('${input.callbackPath}', async (req, reply) => {
|
|
186
|
+
const full = new URL(req.url, \`\${req.protocol}://\${req.headers.host}\`).toString()
|
|
187
|
+
const { sessionId, email } = await finishSignIn(full)
|
|
188
|
+
reply.header('set-cookie', sessionCookie(sessionId))
|
|
189
|
+
return \`Signed in as \${email ?? 'user'}.\`
|
|
190
|
+
})
|
|
191
|
+
}
|
|
192
|
+
// await app.register(aipayRoutes)
|
|
193
|
+
`;
|
|
194
|
+
}
|
|
195
|
+
function honoRoutes(input, startPath) {
|
|
196
|
+
const ts = input.typescript;
|
|
197
|
+
return `import { Hono } from 'hono'
|
|
198
|
+
import { beginSignIn, finishSignIn, sessionCookie } from './aipay${ts ? '' : '.js'}'
|
|
199
|
+
|
|
200
|
+
export const aipay = new Hono()
|
|
201
|
+
|
|
202
|
+
aipay.get('${startPath}', async (c) => c.redirect(await beginSignIn()))
|
|
203
|
+
|
|
204
|
+
aipay.get('${input.callbackPath}', async (c) => {
|
|
205
|
+
const { sessionId, email } = await finishSignIn(c.req.url)
|
|
206
|
+
c.header('set-cookie', sessionCookie(sessionId))
|
|
207
|
+
return c.text(\`Signed in as \${email ?? 'user'}.\`)
|
|
208
|
+
})
|
|
209
|
+
// app.route('/', aipay)
|
|
210
|
+
`;
|
|
211
|
+
}
|
|
212
|
+
function nodeRoutes(input, startPath) {
|
|
213
|
+
const ts = input.typescript;
|
|
214
|
+
return `${ts ? "import type { IncomingMessage, ServerResponse } from 'node:http'\n" : ''}import { beginSignIn, finishSignIn, sessionCookie } from './aipay${ts ? '' : '.js'}'
|
|
215
|
+
|
|
216
|
+
/** Plug into your http server: \`if (await handleAIPay(req, res)) return\` */
|
|
217
|
+
export async function handleAIPay(req${ts ? ': IncomingMessage' : ''}, res${ts ? ': ServerResponse' : ''})${ts ? ': Promise<boolean>' : ''} {
|
|
218
|
+
const url = new URL(req.url ?? '/', \`http://\${req.headers.host}\`)
|
|
219
|
+
if (url.pathname === '${startPath}') {
|
|
220
|
+
res.writeHead(302, { location: await beginSignIn() })
|
|
221
|
+
res.end()
|
|
222
|
+
return true
|
|
223
|
+
}
|
|
224
|
+
if (url.pathname === '${input.callbackPath}') {
|
|
225
|
+
const { sessionId, email } = await finishSignIn(url.toString())
|
|
226
|
+
res.writeHead(200, { 'set-cookie': sessionCookie(sessionId), 'content-type': 'text/plain; charset=utf-8' })
|
|
227
|
+
res.end(\`Signed in as \${email ?? 'user'}.\`)
|
|
228
|
+
return true
|
|
229
|
+
}
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
`;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Vite/browser variant of the helper module. Same three steps as the server module, but the
|
|
236
|
+
* pending flow and the tokens live in sessionStorage (tab-scoped, gone when the tab closes) and
|
|
237
|
+
* the config comes from `import.meta.env.VITE_AIPAY_*` — Vite only exposes VITE_-prefixed values.
|
|
238
|
+
*/
|
|
239
|
+
function browserModule(input) {
|
|
240
|
+
const ts = input.typescript;
|
|
241
|
+
const t = (typed, plain) => (ts ? typed : plain);
|
|
242
|
+
return `${ts ? '/// <reference types="vite/client" />\n' : ''}// Browser (Vite) build of the AI-Pay helper. Reads the VITE_AIPAY_* values \`aipay init\`
|
|
243
|
+
// wrote to .env.local (Vite exposes only VITE_-prefixed values to the page). No secret
|
|
244
|
+
// exists: the app is a public PKCE client and the token endpoint accepts browser calls.
|
|
245
|
+
import {
|
|
246
|
+
AIPayClient,
|
|
247
|
+
createConnectUrl,
|
|
248
|
+
getUserInfo,
|
|
249
|
+
handleCallback,${t('\n type TokenSet,', '')}
|
|
250
|
+
} from '@coswic/aipay-sdk'
|
|
251
|
+
|
|
252
|
+
const cfg = {
|
|
253
|
+
hydraPublicUrl: import.meta.env.VITE_AIPAY_AUTH_ORIGIN${t(' as string', '')},
|
|
254
|
+
clientId: import.meta.env.VITE_AIPAY_CLIENT_ID${t(' as string', '')},
|
|
255
|
+
redirectUri: import.meta.env.VITE_AIPAY_CALLBACK_URL${t(' as string', '')},
|
|
256
|
+
}
|
|
257
|
+
const apiBase = import.meta.env.VITE_AIPAY_API_BASE${t(' as string', '')}
|
|
258
|
+
/** Path part of the registered callback — the page that must call completeSignInIfCallback(). */
|
|
259
|
+
export const CALLBACK_PATH = ${JSON.stringify(input.callbackPath)}
|
|
260
|
+
|
|
261
|
+
const PENDING_KEY = 'aipay:pending' // { state, codeVerifier } while the user is at AI-Pay
|
|
262
|
+
const TOKENS_KEY = 'aipay:tokens' // TokenSet for this tab (sessionStorage: cleared when the tab closes)
|
|
263
|
+
|
|
264
|
+
/** Step 1 — send the user to AI-Pay. The PKCE verifier is kept under the state for the callback. */
|
|
265
|
+
export async function beginSignIn()${t(': Promise<void>', '')} {
|
|
266
|
+
const { url, state, codeVerifier } = await createConnectUrl(cfg)
|
|
267
|
+
sessionStorage.setItem(PENDING_KEY, JSON.stringify({ state, codeVerifier }))
|
|
268
|
+
location.assign(url)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Step 2 — call once at startup. On the callback page it exchanges the code (state + PKCE are
|
|
273
|
+
* verified by the SDK), stores the tokens for this tab and cleans the URL; elsewhere it is a no-op.
|
|
274
|
+
*/
|
|
275
|
+
export async function completeSignInIfCallback()${t(': Promise<{ sub: string; email: string | null } | null>', '')} {
|
|
276
|
+
if (location.pathname !== CALLBACK_PATH) return null
|
|
277
|
+
const raw = sessionStorage.getItem(PENDING_KEY)
|
|
278
|
+
sessionStorage.removeItem(PENDING_KEY)
|
|
279
|
+
const saved = raw ? ${t('(JSON.parse(raw) as { state: string; codeVerifier: string })', 'JSON.parse(raw)')} : null
|
|
280
|
+
const tokens = await handleCallback(cfg, location.href, saved)
|
|
281
|
+
sessionStorage.setItem(TOKENS_KEY, JSON.stringify(tokens))
|
|
282
|
+
history.replaceState(null, '', '/')
|
|
283
|
+
const user = await getUserInfo(cfg, tokens.accessToken)
|
|
284
|
+
// user.sub is the stable per-app id of this user — key your accounts on it, not on email.
|
|
285
|
+
return { sub: user.sub, email: user.email ?? null }
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function currentTokens()${t(': TokenSet | null', '')} {
|
|
289
|
+
const raw = sessionStorage.getItem(TOKENS_KEY)
|
|
290
|
+
return raw ? ${t('(JSON.parse(raw) as TokenSet)', 'JSON.parse(raw)')} : null
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function signOut()${t(': void', '')} {
|
|
294
|
+
sessionStorage.removeItem(TOKENS_KEY)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Step 3 — call a model on the user's behalf; the response carries the billing receipt. */
|
|
298
|
+
export async function firstCompletion(model${t(': string', '')}) {
|
|
299
|
+
const tokens = currentTokens()
|
|
300
|
+
if (!tokens) throw new Error('not signed in')
|
|
301
|
+
const client = new AIPayClient({ baseUrl: apiBase, accessToken: tokens.accessToken })
|
|
302
|
+
const res = await client.chat.completions.create({
|
|
303
|
+
model,
|
|
304
|
+
messages: [{ role: 'user', content: 'Say hello in five words.' }],
|
|
305
|
+
max_tokens: 32,
|
|
306
|
+
})
|
|
307
|
+
return { text: res.choices[0]?.message.content ?? '', receipt: res.aipay }
|
|
308
|
+
}
|
|
309
|
+
`;
|
|
310
|
+
}
|
|
311
|
+
/** Startup wiring for a Vite app: handle the callback page, expose the two actions to your UI. */
|
|
312
|
+
function viteBoot(input) {
|
|
313
|
+
const ts = input.typescript;
|
|
314
|
+
const from = ts ? './aipay' : './aipay.js';
|
|
315
|
+
return `// Call bootAIPay() once when your app starts (top of main.${ts ? 'ts' : 'js'} / before rendering).
|
|
316
|
+
// It completes the sign-in when the page is the AI-Pay callback (${input.callbackPath}) and
|
|
317
|
+
// otherwise does nothing. Vite's dev server serves index.html for that path (SPA fallback),
|
|
318
|
+
// so no route configuration is needed; with a client-side router, keep the path unrouted.
|
|
319
|
+
import { beginSignIn, completeSignInIfCallback, currentTokens, firstCompletion } from '${from}'
|
|
320
|
+
|
|
321
|
+
export async function bootAIPay() {
|
|
322
|
+
const justSignedIn = await completeSignInIfCallback()
|
|
323
|
+
const signedIn = justSignedIn !== null || currentTokens() !== null
|
|
324
|
+
return {
|
|
325
|
+
signedIn,
|
|
326
|
+
/** Wire this to your "Sign in with AI-Pay" button. */
|
|
327
|
+
beginSignIn,
|
|
328
|
+
/** After sign-in: one model call; the receipt (cost, request id) is on the result. */
|
|
329
|
+
firstCompletion,
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
`;
|
|
333
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function cliVersion(): string;
|
package/dist/version.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coswic/aipay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "AI-Pay CLI — init (link an app + scaffold the integration), doctor, test (real sign-in + first receipt), status, unlink. For developers and their coding agents.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|