Skip to content

Commit

Permalink
add Interaction and LastInteraction components
Browse files Browse the repository at this point in the history
  • Loading branch information
zapaz committed Nov 4, 2024
1 parent ee6b8e9 commit 3813b5e
Show file tree
Hide file tree
Showing 17 changed files with 493 additions and 483 deletions.
2 changes: 1 addition & 1 deletion common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Kredeum",
"description": "OnChainAI Common package",
"devDependencies": {
"@types/node": "^22.8.6",
"@types/node": "^22.8.7",
"eslint-plugin-json": "^4.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
Expand Down
4 changes: 2 additions & 2 deletions foundry/addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"11155420": {
"Counter": "0x8354Ef7b78012151c276f51F8d19147FF8C47288",
"OnChainAI": "0xd1ca741de2d2975822ADf4646Cf0A8AE3Df51c78",
"OnChainAIv1": "0x432beBA9B2Fc71180FF9ba3fA93E1Ae88beDF402",
"OnChainAIv1": "0xa043949B6984D8704501EC64519E2b6e00a4329A",
"chainName": "optimism-sepolia"
},
"31337": {
Expand All @@ -19,7 +19,7 @@
"84532": {
"Counter": "0x1bC96A82609F23419065FCEf120E16E2A6Bd3981",
"OnChainAI": "0xCB60F4a4e578CDDBe89e4a27B2126f54BaeF3500",
"OnChainAIv1": "0xBf196B3732880a51Eaa6Cfc38d82De1A61582B06",
"OnChainAIv1": "0xc308300b222bf2C6B0C81D8C1a64909D0a32b5Ec",
"chainName": "base-sepolia"
}
}
32 changes: 14 additions & 18 deletions foundry/src/OnChainAIv1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ contract OnChainAIv1 is FunctionsClient, ConfirmedOwner {
string response;
}

mapping(address => bytes32) internal _lastRequestId;
mapping(bytes32 => Interaction) internal _interactions;
/// @notice lastInteraction mapping of each sender.
mapping(address => Interaction) public lastInteraction;
/// @notice interaction mapping of each requestId.
mapping(bytes32 => Interaction) internal _interactionRequests;

bytes32 internal _donId;
uint32 internal _gasLimit;
Expand Down Expand Up @@ -93,13 +95,6 @@ contract OnChainAIv1 is FunctionsClient, ConfirmedOwner {
setPrice(price_);
}

/// @notice Retrieves the last interaction for a given sender address.
/// @param sender The address of the sender.
/// @return The last Interaction struct associated with the sender.
function lastInteraction(address sender) external view returns (Interaction memory) {
return _interactions[_lastRequestId[sender]];
}

/// @notice Sets the JavaScript code to be used in the Chainlink Function request.
/// @dev Only the contract owner can call this function.
/// @param javascript_ The JavaScript code as a string.
Expand Down Expand Up @@ -162,9 +157,9 @@ contract OnChainAIv1 is FunctionsClient, ConfirmedOwner {

requestId = _sendRequest(req.encodeCBOR(), _subscriptionId, _gasLimit, _donId);

delete( _interactions[_lastRequestId[msg.sender]]);
_lastRequestId[msg.sender] = requestId;
_interactions[requestId] = Interaction(requestId, msg.sender, userPrompt, "");
Interaction memory interactionRequest = Interaction(requestId, msg.sender, userPrompt, "");
_interactionRequests[requestId] = interactionRequest;
lastInteraction[msg.sender] = interactionRequest;

emit InteractionLog(requestId, msg.sender, false, userPrompt, "");
}
Expand All @@ -175,21 +170,22 @@ contract OnChainAIv1 is FunctionsClient, ConfirmedOwner {
/// @param response The response data from the Chainlink Function.
/// @param err Any error messages from the Chainlink Function.
function fulfillRequest(bytes32 requestId, bytes memory response, bytes memory err) internal override {
Interaction memory interaction = _interactions[requestId];

Interaction memory interactionResponse = _interactionRequests[requestId];
require(
(requestId != 0) && (requestId == interaction.requestId),
(requestId != 0) && (requestId == interactionResponse.requestId),
UnexpectedFullfillRequest(requestId, string(response), string(err))
);
delete _interactionRequests[requestId];

// Concatenate response and/or error
/// @dev Concatenate response and/or error
string memory responseError = (err.length == 0)
? (response.length == 0) ? "Empty response" : string(response)
: string.concat("Error: ", string(err), " | ", string(response));

_interactions[requestId].response = responseError;
interactionResponse.response = responseError;
lastInteraction[interactionResponse.sender] = interactionResponse;

emit InteractionLog(requestId, interaction.sender, true, interaction.prompt, responseError);
emit InteractionLog(requestId, interactionResponse.sender, true, interactionResponse.prompt, responseError);
}

/// @notice Withdraws the contract's balance to the owner's address.
Expand Down
Loading

0 comments on commit 3813b5e

Please sign in to comment.