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
46 changes: 45 additions & 1 deletion server/api/routes/firebaseAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,49 @@ const ARTIST_COLLECTION = "artist";
const ARTIST_SURVEY_COLLECTION = "artistSurvey";
const POEM_COLLECTION = "poem";
const INCOMPLETE_SESSION_COLLECTION = "artistIncompleteSession";
const ASSIGNMENT_COLLECTION = "artistAssignment";

router.post("/artist-assignment", async (req, res) => {
try {
const { sessionId, passageId, prolificPid } = req.body;
if (!sessionId || !passageId) {
return res.status(400).json({ error: "Missing sessionId or passageId" });
}

const assignmentRef = db.collection(ASSIGNMENT_COLLECTION).doc(sessionId);
const assignment = await db.runTransaction(async (transaction) => {
const existingAssignment = await transaction.get(assignmentRef);
if (existingAssignment.exists) {
const existing = existingAssignment.data()!;
return {
passageId: existing.passageId as string,
condition: existing.condition as "LLM" | "NO_AI",
strategy: existing.strategy as string,
};
}

const condition: "LLM" | "NO_AI" =
Math.random() < 0.5 ? "LLM" : "NO_AI";
const strategy = "INDEPENDENT_RANDOM_1_TO_1";

transaction.set(assignmentRef, {
sessionId,
prolificPid: prolificPid || null,
passageId: String(passageId),
condition,
strategy,
assignedAt: FieldValue.serverTimestamp(),
});

return { passageId: String(passageId), condition, strategy };
});

res.json(assignment);
} catch (error) {
console.error(error);
res.status(500).json({ error: "Failed to assign study condition" });
}
});

router.post("/autosave", async (req, res) => {
try {
Expand Down Expand Up @@ -80,8 +123,9 @@ router.post("/commit-session", async (req, res) => {
.collection(INCOMPLETE_SESSION_COLLECTION)
.doc(sessionId);

const artist: Record<string, any> = {
const artist: Record<string, unknown> = {
condition: artistData.condition,
assignment: artistData.assignment ?? null,
surveyResponse: surveyRef,
poem: poemRef,
timestamps: [...(artistData.timeStamps ?? []), new Date()],
Expand Down
44 changes: 38 additions & 6 deletions server/api/routes/llmAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import dotenv from "dotenv";
dotenv.config();

const openai = new OpenAI({ apiKey: process.env.LLM_KEY || "" });
const LLM_MODEL = "gpt-5.6-sol";
const GENERATION_PARAMETERS = {
text: { verbosity: "low" as const },
stream: true as const,
};

const router = express.Router();

Expand All @@ -14,17 +19,34 @@ router.post("/query", async (req: express.Request, res: express.Response) => {
res.setHeader("Connection", "keep-alive");

try {
const { messages } = req.body;
const { messages, promptVersion } = req.body;

const stream = await openai.responses.create({
model: "gpt-5.6-sol",
model: LLM_MODEL,
input: messages,
text: { verbosity: "low" },
stream: true,
...GENERATION_PARAMETERS,
});

const writeMetadata = (modelVersion: string) => {
res.write(
`data: ${JSON.stringify({
type: "metadata",
metadata: {
model: LLM_MODEL,
modelVersion,
promptVersion: promptVersion || "unversioned",
generationParameters: GENERATION_PARAMETERS,
},
})}\n\n`,
);
};

writeMetadata(LLM_MODEL);

for await (const event of stream) {
if (event.type === "response.output_text.delta") {
if (event.type === "response.created") {
writeMetadata(event.response.model || LLM_MODEL);
} else if (event.type === "response.output_text.delta") {
res.write(`data: ${JSON.stringify({ content: event.delta })}\n\n`);

process.stdout.write(event.delta);
Expand All @@ -36,7 +58,17 @@ router.post("/query", async (req: express.Request, res: express.Response) => {
res.end();
} catch (err) {
console.error("Error fetching from OpenAI:", err);
res.status(500).json({ error: "Something went wrong." });
if (res.headersSent) {
res.write(
`data: ${JSON.stringify({
type: "error",
error: "The model request failed.",
})}\n\n`,
);
res.end();
} else {
res.status(500).json({ error: "Something went wrong." });
}
}
});

Expand Down
3 changes: 3 additions & 0 deletions src/components/blackout/Blackout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const BlackoutPoetry: React.FC<BlackoutProps> = ({
action: isSelected ? "REMOVE" : "ADD",
index,
timestamp: new Date(),
source: "DIRECT",
};
setPoemSnapshots((prev) => [...prev, newSnapshot]);

Expand Down Expand Up @@ -66,6 +67,7 @@ const BlackoutPoetry: React.FC<BlackoutProps> = ({
action: actionType,
index: snapshot.index,
timestamp: new Date(),
source: "UNDO",
},
]);

Expand Down Expand Up @@ -99,6 +101,7 @@ const BlackoutPoetry: React.FC<BlackoutProps> = ({
action: actionType,
index: snapshot.index,
timestamp: new Date(),
source: "REDO",
},
]);

Expand Down
Loading