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
5 changes: 5 additions & 0 deletions src/models/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface GraphRagResponse {
message_type?: "chunk" | "explain";
explain_id?: string;
explain_graph?: string; // Named graph where explain data is stored (e.g., urn:graph:retrieval)
explain_triples?: Triple[];
end_of_session?: boolean;
}

Expand Down Expand Up @@ -115,18 +116,21 @@ export interface DocumentRagResponse {
message_type?: "chunk" | "explain";
explain_id?: string;
explain_graph?: string;
explain_triples?: Triple[];
end_of_session?: boolean;
}

export interface AgentRequest {
question: string;
user?: string;
collection?: string;
streaming?: boolean;
}

export interface AgentResponse {
// Streaming response format (new protocol)
chunk_type?: "thought" | "action" | "observation" | "answer" | "final-answer" | "explain" | "error";
message_id?: string;
content?: string;
end_of_message?: boolean;
end_of_dialog?: boolean;
Expand All @@ -146,6 +150,7 @@ export interface AgentResponse {
message_type?: "chunk" | "explain";
explain_id?: string;
explain_graph?: string;
explain_triples?: Triple[];
}

export interface EmbeddingsRequest {
Expand Down
11 changes: 8 additions & 3 deletions src/socket/service-call-multi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,21 @@ export class ServiceCallMulti {
if (this.complete == true)
console.log(this.mid, "should not happen, request is already complete");

// Reset the timeout on every chunk — the server is still active as long
// as data keeps arriving. Without this, long-running streaming requests
// (e.g. agent queries) would time out and retry mid-dialog.
clearTimeout(this.timeoutId);

const fin = this.receiver(resp);

if (fin) {
this.complete = true;

// console.log("Received for", this.mid);
clearTimeout(this.timeoutId);
this.timeoutId = undefined;
delete this.socket.inflight[this.mid];
this.success(resp);
} else {
// Not done yet — restart the inactivity timer
this.timeoutId = setTimeout(this.onTimeout.bind(this), this.timeout);
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/socket/trustgraph-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
export interface ExplainEvent {
explainId: string;
explainGraph: string; // Named graph where explain data is stored (e.g., urn:graph:retrieval)
explainTriples?: Triple[]; // Inline provenance triples (avoids graph round-trip)
}

// Configuration constants
Expand Down Expand Up @@ -728,7 +729,7 @@
},
60000,
)
.then((r) => r["processing-metadata"] || []);
.then((r) => r["processing-metadatas"] || r["processing-metadata"] || []);

Check failure on line 732 in src/socket/trustgraph-socket.ts

View workflow job for this annotation

GitHub Actions / build

Property 'processing-metadatas' does not exist on type 'LibraryResponse'. Did you mean 'processing-metadata'?
}

/**
Expand Down Expand Up @@ -1377,11 +1378,12 @@
*/
agent(
question: string,
think: (chunk: string, complete: boolean, metadata?: StreamingMetadata) => void,
observe: (chunk: string, complete: boolean, metadata?: StreamingMetadata) => void,
answer: (chunk: string, complete: boolean, metadata?: StreamingMetadata) => void,
think: (chunk: string, complete: boolean, messageId?: string, metadata?: StreamingMetadata) => void,
observe: (chunk: string, complete: boolean, messageId?: string, metadata?: StreamingMetadata) => void,
answer: (chunk: string, complete: boolean, messageId?: string, metadata?: StreamingMetadata) => void,
error: (s: string) => void,
onExplain?: (event: ExplainEvent) => void,
collection?: string,
) {
const receiver = (message: unknown) => {
const msg = message as { response?: AgentResponse; complete?: boolean; error?: string };
Expand All @@ -1405,12 +1407,14 @@
onExplain?.({
explainId: resp.explain_id,
explainGraph: resp.explain_graph,
explainTriples: resp.explain_triples,
});
return false;
}

// Handle streaming chunks by chunk_type
const content = resp.content || "";
const messageId = resp.message_id;
const messageComplete = !!resp.end_of_message;
const dialogComplete = !!msg.complete;

Expand All @@ -1421,14 +1425,14 @@

switch (resp.chunk_type) {
case "thought":
think(content, messageComplete, metadata);
think(content, messageComplete, messageId, metadata);
break;
case "observation":
observe(content, messageComplete, metadata);
observe(content, messageComplete, messageId, metadata);
break;
case "answer":
case "final-answer":
answer(content, messageComplete, metadata);
answer(content, messageComplete, messageId, metadata);
break;
case "action":
// Actions are typically not streamed incrementally, just logged
Expand All @@ -1445,6 +1449,7 @@
{
question: question,
user: this.api.user,
collection: collection || "default",
streaming: true, // Always use streaming mode
},
receiver,
Expand Down Expand Up @@ -1498,6 +1503,7 @@
onExplain?.({
explainId: resp.explain_id,
explainGraph: resp.explain_graph,
explainTriples: resp.explain_triples,
});
// Don't return true - more messages may follow
return false;
Expand Down Expand Up @@ -1574,6 +1580,7 @@
onExplain?.({
explainId: resp.explain_id,
explainGraph: resp.explain_graph,
explainTriples: resp.explain_triples,
});
return false;
}
Expand Down
Loading