Skip to content

Commit edd459e

Browse files
author
Frank
committed
Share: og image
1 parent 177875f commit edd459e

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

packages/function/src/api.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DurableObject } from "cloudflare:workers"
22
import { randomUUID } from "node:crypto"
3+
import { Base64 } from "js-base64"
34

45
type Env = {
56
SYNC_SERVER: DurableObjectNamespace<SyncServer>
@@ -180,8 +181,12 @@ export default {
180181
return new Response("Error: Share ID is required", { status: 400 })
181182
const stub = env.SYNC_SERVER.get(env.SYNC_SERVER.idFromName(id))
182183
const data = await stub.getData()
184+
183185
let info
184-
const messages = {}
186+
const messages: Record<string, any> = {}
187+
let cost = 0
188+
const models: Set<string> = new Set()
189+
const version = "v0.1.1"
185190
data.forEach((d) => {
186191
const [root, type, ...splits] = d.key.split("/")
187192
if (root !== "session") return
@@ -192,12 +197,36 @@ export default {
192197
if (type === "message") {
193198
const [, messageID] = splits
194199
messages[messageID] = d.content
200+
201+
const assistant = d.content.metadata?.assistant
202+
if (assistant) {
203+
cost += assistant.cost
204+
models.add(assistant.modelID)
205+
}
195206
}
196207
})
197208

198-
return new Response(JSON.stringify({ info, messages }), {
199-
headers: { "Content-Type": "application/json" },
200-
})
209+
const encodedTitle = encodeURIComponent(
210+
Base64.encode(
211+
// Convert to ASCII
212+
encodeURIComponent(
213+
// Truncate to fit S3's max key size
214+
info.title.substring(0, 700),
215+
),
216+
),
217+
)
218+
const encodedCost = encodeURIComponent(`$${cost.toFixed(2)}`)
219+
220+
return new Response(
221+
JSON.stringify({
222+
info,
223+
messages,
224+
ogImage: `https://social-cards.sst.dev/opencode-share/${encodedTitle}.png?cost=${encodedCost}&model=${Array.from(models).join(",")}&version=${version}&id=${id}`,
225+
}),
226+
{
227+
headers: { "Content-Type": "application/json" },
228+
},
229+
)
201230
}
202231
},
203232
}

packages/web/src/pages/s/index.astro

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
import { Base64 } from "js-base64";
32
import config from "virtual:starlight/user-config";
43
54
import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
@@ -12,23 +11,7 @@ const res = await fetch(`${apiUrl}/share_data?id=${id}`);
1211
const data = await res.json();
1312
1413
const title = data.info.title;
15-
16-
const encodedTitle = encodeURIComponent(
17-
Base64.encode(
18-
// Convert to ASCII
19-
encodeURIComponent(
20-
// Truncate to fit S3's max key size
21-
title.substring(0, 700)
22-
)
23-
)
24-
)
25-
26-
const cardService = "https://social-cards.sst.dev";
27-
const cost = "$0.12";
28-
const model = "claude-sonnet-4-20250514,claude-opus-4-20250514";
29-
const version = "v0.1.1";
30-
// ?cost=$0.12&model=claude-sonnet-4-20250514,claude-opus-4-20250514&version=v0.1.1&id=43120e6b
31-
const ogImageUrl = `${cardService}/opencode-share/${encodedTitle}.png?cost=${cost}&model=${model}&version=${version}&id=${id}`;
14+
const ogImage = data.ogImage;
3215
3316
---
3417
<StarlightPage
@@ -43,14 +26,14 @@ const ogImageUrl = `${cardService}/opencode-share/${encodedTitle}.png?cost=${cos
4326
tag: "meta",
4427
attrs: {
4528
property: "og:image",
46-
content: ogImageUrl,
29+
content: ogImage,
4730
},
4831
},
4932
{
5033
tag: "meta",
5134
attrs: {
5235
name: "twitter:image",
53-
content: ogImageUrl,
36+
content: ogImage,
5437
},
5538
},
5639
],

0 commit comments

Comments
 (0)