Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions apps/web/trigger/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ const provider = new GrokProvider()
export const enrichPaperMetadata = task({
id: "enrich-paper-metadata",
run: async (payload: { title: string; abstract: string }) => {
const tags = await provider.complete(
"tag-extraction",
`Title: ${payload.title}\nAbstract: ${payload.abstract}`,
)
const result = await provider.extractTags(payload.title, payload.abstract)

return {
tags,
}
return result
},
})

Expand Down
64 changes: 63 additions & 1 deletion packages/ai/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { getPapersConfig } from "@papers/config"
import { type ProviderTaskKind, type SafeAiPayload, safeAiPayloadSchema } from "@papers/contracts"
import {
type ProviderTaskKind,
type SafeAiPayload,
safeAiPayloadSchema,
type TagExtractionResult,
tagExtractionJsonSchema,
tagExtractionResultSchema,
} from "@papers/contracts"

export class GrokProvider {
readonly #config = getPapersConfig()
Expand Down Expand Up @@ -61,4 +68,59 @@ export class GrokProvider {
const payload = (await response.json()) as { output_text?: string }
return payload.output_text ?? ""
}

async extractTags(title: string, abstract: string): Promise<TagExtractionResult> {
const text = `Title: ${title}\nAbstract: ${abstract}`

this.assertSafe({
task: "tag-extraction",
isBlindContent: false,
containsPrivateDraft: false,
text,
})

if (!this.enabled || !this.#config.PAPERS_XAI_API_KEY) {
return { tags: [] }
}

const response = await fetch(`${this.#config.PAPERS_XAI_BASE_URL}/responses`, {
method: "POST",
headers: {
authorization: `Bearer ${this.#config.PAPERS_XAI_API_KEY}`,
"content-type": "application/json",
},
body: JSON.stringify({
model: this.#config.PAPERS_XAI_MODEL,
input: [
{
role: "system",
content: [
{
type: "input_text",
text: "Extract up to 8 research topic tags from the paper. Each tag needs a human-readable label and a URL-safe slug. Never infer hidden identity.",
},
],
},
{
role: "user",
content: [{ type: "input_text", text }],
},
],
text: {
format: {
type: "json_schema",
...tagExtractionJsonSchema,
},
},
}),
})

if (!response.ok) {
throw new Error(`Grok tag extraction failed with ${response.status}`)
}

const raw = (await response.json()) as { output_text?: string }
const parsed: unknown = JSON.parse(raw.output_text ?? "{}")
return tagExtractionResultSchema.parse(parsed)
}
}
37 changes: 37 additions & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,43 @@ export const safeAiPayloadSchema = z.object({
})
export type SafeAiPayload = z.infer<typeof safeAiPayloadSchema>

export const tagExtractionResultSchema = z.object({
tags: z
.array(
z.object({
label: z.string(),
slug: z.string(),
}),
)
.max(8),
})
export type TagExtractionResult = z.infer<typeof tagExtractionResultSchema>

/** JSON Schema sent to the Grok Responses API for structured output. */
export const tagExtractionJsonSchema = {
name: "tag_extraction",
strict: true,
schema: {
type: "object",
properties: {
tags: {
type: "array",
items: {
type: "object",
properties: {
label: { type: "string" },
slug: { type: "string" },
},
required: ["label", "slug"],
additionalProperties: false,
},
},
},
required: ["tags"],
additionalProperties: false,
},
} as const

export const opportunityIdeaSchema = z.object({
id: z.string(),
label: z.string(),
Expand Down
Loading